-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore a simple version of shell tool for LiteDB v5 (netcore)
- Loading branch information
Showing
21 changed files
with
903 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "close", | ||
Syntax = "close", | ||
Description = "Close current datafile" | ||
)] | ||
internal class Close : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Scan(@"close$").Length > 0; | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
env.Database.Dispose(); | ||
env.Database = null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "ed", | ||
Syntax = "ed", | ||
Description = "Open your last command in notepad." | ||
)] | ||
internal class Ed : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Match(@"ed$"); | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
var temp = Path.GetTempPath() + "LiteDB.Shell.txt"; | ||
|
||
// remove "ed" command from history | ||
env.Input.History.RemoveAt(env.Input.History.Count - 1); | ||
|
||
var last = env.Input.History.Count > 0 ? env.Input.History[env.Input.History.Count - 1] : ""; | ||
|
||
File.WriteAllText(temp, last.Replace("\n", Environment.NewLine)); | ||
|
||
Process.Start("notepad.exe", temp).WaitForExit(); | ||
|
||
var text = File.ReadAllText(temp); | ||
|
||
if (text == last) return; | ||
|
||
env.Input.Queue.Enqueue(text); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
internal class HelpAttribute : Attribute | ||
{ | ||
public string Name { get; set; } | ||
public string Syntax { get; set; } | ||
public string Description { get; set; } | ||
public string[] Examples { get; set; } = new string[0]; | ||
} | ||
|
||
internal class Help : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Scan(@"help\s*").Length > 0; | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
var param = s.Scan(".*"); | ||
var d = env.Display; | ||
|
||
// getting all HelpAttributes inside assemblies | ||
var helps = AppDomain.CurrentDomain.GetAssemblies() | ||
.SelectMany(x => x.GetTypes()) | ||
.Select(x => x.GetCustomAttributes(typeof(HelpAttribute), true).FirstOrDefault()) | ||
.Where(x => x != null) | ||
.Select(x => x as HelpAttribute) | ||
.ToArray(); | ||
|
||
d.WriteLine(ConsoleColor.White, "# LiteDB Shell Command Reference"); | ||
|
||
foreach (var help in helps) | ||
{ | ||
d.WriteLine(""); | ||
d.WriteLine(ConsoleColor.Cyan, "> " + help.Syntax); | ||
d.WriteLine(ConsoleColor.DarkCyan, " " + help.Description); | ||
|
||
// show examples only when named help command | ||
foreach (var example in help.Examples) | ||
{ | ||
d.WriteLine(ConsoleColor.Gray, " > " + example); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace LiteDB.Shell | ||
{ | ||
internal interface IShellCommand | ||
{ | ||
bool IsCommand(StringScanner s); | ||
|
||
void Execute(StringScanner s, Env env); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "open", | ||
Syntax = "open <filename|connectionString>", | ||
Description = "Open (or create) a new datafile. Can be used a single filename or a connection string with all supported parameters.", | ||
Examples = new string[] { | ||
"open mydb.db", | ||
"open filename=mydb.db; password=johndoe; initial=100Mb" | ||
} | ||
)] | ||
internal class Open : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Scan(@"open\s+").Length > 0; | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
var connectionString = new ConnectionString(s.Scan(@".+").TrimToNull()); | ||
|
||
if (env.Database != null) | ||
{ | ||
env.Database.Dispose(); | ||
env.Database = null; | ||
} | ||
|
||
env.Database = new LiteDatabase(connectionString); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "pretty", | ||
Syntax = "pretty [on|off]", | ||
Description = "Print all json results with identation/break lines", | ||
Examples = new string[] { | ||
"pretty" | ||
} | ||
)] | ||
internal class Pretty : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Scan(@"pretty\s*").Length > 0; | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
env.Display.Pretty = !(s.Scan(@"off\s*").Length > 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "quit", | ||
Syntax = "quit|exit", | ||
Description = "Close shell application" | ||
)] | ||
internal class Quit : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Match(@"(quit|exit)$"); | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
env.Database?.Dispose(); | ||
env.Input.Running = false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "run", | ||
Syntax = "run <filename>", | ||
Description = "Queue shell commands inside filename to be run in order.", | ||
Examples = new string[] { | ||
"run scripts.txt" | ||
} | ||
)] | ||
internal class Run : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Scan(@"run\s+").Length > 0; | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
if (env.Database == null) throw new Exception("Database not connected"); | ||
|
||
var filename = s.Scan(@".+").Trim(); | ||
|
||
foreach (var line in File.ReadAllLines(filename)) | ||
{ | ||
env.Input.Queue.Enqueue(line); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "show collections", | ||
Syntax = "show collections", | ||
Description = "List all collections inside datafile." | ||
)] | ||
internal class ShowCollections : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Match(@"show\scollections$"); | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
if (env.Database == null) throw new Exception("Database not connected"); | ||
|
||
var cols = env.Database.GetCollectionNames().OrderBy(x => x).ToArray(); | ||
|
||
if (cols.Length > 0) | ||
{ | ||
env.Display.WriteLine(ConsoleColor.Cyan, string.Join(Environment.NewLine, cols)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace LiteDB.Shell.Commands | ||
{ | ||
[Help( | ||
Name = "version", | ||
Syntax = "ver", | ||
Description = "Show LiteDB version" | ||
)] | ||
internal class Version : IShellCommand | ||
{ | ||
public bool IsCommand(StringScanner s) | ||
{ | ||
return s.Scan(@"ver(sion)?$").Length > 0; | ||
} | ||
|
||
public void Execute(StringScanner s, Env env) | ||
{ | ||
var assembly = typeof(LiteDatabase).Assembly.GetName(); | ||
|
||
env.Display.WriteLine(assembly.FullName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.1</TargetFrameworks> | ||
<AssemblyName>LiteDB.Shell</AssemblyName> | ||
<PackageId>LiteDB.Shell</PackageId> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>LiteDB.Shell</RootNamespace> | ||
<AssemblyVersion>5.0.0.0</AssemblyVersion> | ||
<FileVersion>5.0.0</FileVersion> | ||
<VersionPrefix>5.0.0</VersionPrefix> | ||
<Authors>Maurício David</Authors> | ||
<Copyright>MIT</Copyright> | ||
<NeutralLanguage>en-US</NeutralLanguage> | ||
<GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
<NoWarn>1701;1702;1705;1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\LiteDB\LiteDB.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace LiteDB.Shell | ||
{ | ||
internal class Program | ||
{ | ||
/// <summary> | ||
/// Opens console shell app. Usage: | ||
/// LiteDB.Shell [myfile.db] --param1 value1 --params2 "value 2" | ||
/// Parameters: | ||
/// --exec "command" : Execute an shell command (can be multiples --exec) | ||
/// --run script.txt : Run script commands file | ||
/// --pretty : Show JSON in multiline + idented | ||
/// --exit : Exit after last command | ||
/// </summary> | ||
private static void Main(string[] args) | ||
{ | ||
var input = new InputCommand(); | ||
var display = new Display(); | ||
var o = new OptionSet(); | ||
|
||
// default arg | ||
o.Register((v) => input.Queue.Enqueue("open " + v)); | ||
o.Register("pretty", () => display.Pretty = true); | ||
o.Register("exit", () => input.AutoExit = true); | ||
o.Register<string>("run", (v) => input.Queue.Enqueue("run " + v)); | ||
o.Register<string>("exec", (v) => input.Queue.Enqueue(v)); | ||
|
||
// parse command line calling register parameters | ||
o.Parse(args); | ||
|
||
ShellProgram.Start(input, display); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"profiles": { | ||
"LiteDB.Shell": { | ||
"commandName": "Project", | ||
"commandLineArgs": "app.db" | ||
} | ||
} | ||
} |
Oops, something went wrong.