Skip to content

Commit

Permalink
Restore a simple version of shell tool for LiteDB v5 (netcore)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbdavid committed Sep 24, 2019
1 parent 45d4b00 commit 67096a9
Show file tree
Hide file tree
Showing 21 changed files with 903 additions and 4 deletions.
26 changes: 26 additions & 0 deletions LiteDB.Shell/Commands/Close.cs
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;
}
}
}
39 changes: 39 additions & 0 deletions LiteDB.Shell/Commands/Ed.cs
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);
}
}
}
51 changes: 51 additions & 0 deletions LiteDB.Shell/Commands/Help.cs
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);
}
}
}
}
}
13 changes: 13 additions & 0 deletions LiteDB.Shell/Commands/IShellCommand.cs
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);
}
}
37 changes: 37 additions & 0 deletions LiteDB.Shell/Commands/Open.cs
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);
}
}
}
25 changes: 25 additions & 0 deletions LiteDB.Shell/Commands/Pretty.cs
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);
}
}
}
23 changes: 23 additions & 0 deletions LiteDB.Shell/Commands/Quit.cs
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;
}
}
}
33 changes: 33 additions & 0 deletions LiteDB.Shell/Commands/Run.cs
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);
}
}
}
}
30 changes: 30 additions & 0 deletions LiteDB.Shell/Commands/ShowCollections.cs
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));
}
}
}
}
24 changes: 24 additions & 0 deletions LiteDB.Shell/Commands/Version.cs
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);
}
}
}
23 changes: 23 additions & 0 deletions LiteDB.Shell/LiteDB.Shell.csproj
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>
33 changes: 33 additions & 0 deletions LiteDB.Shell/Program.cs
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);
}
}
}
8 changes: 8 additions & 0 deletions LiteDB.Shell/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"LiteDB.Shell": {
"commandName": "Project",
"commandLineArgs": "app.db"
}
}
}
Loading

0 comments on commit 67096a9

Please sign in to comment.