Esempio n. 1
0
        /// <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
        /// --upgrade newdb.db : Upgrade database to lastest version
        /// --exit             : Exit after last command
        /// </summary>
        private static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            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));
            o.Register<string>("upgrade", (v) =>
            {
                var tmp = Path.GetTempFileName();
                input.Queue.Enqueue("dump > " + tmp);
                input.Queue.Enqueue("open " + v);
                input.Queue.Enqueue("dump < " + tmp);
            });

            // parse command line calling register parameters
            o.Parse(args);

            ShellProgram.Start(input, display);
        }
Esempio n. 2
0
        public static void Start(InputCommand input, Display display)
        {
            IShellEngine engine = null;

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd)) continue;

                try
                {
                    var isConsoleCommand = ConsoleCommand.TryExecute(cmd, ref engine, display, input);

                    if (isConsoleCommand == false)
                    {
                        if (engine == null) throw ShellExpcetion.NoDatabase();

                        engine.Run(cmd, display);
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
        public static void Start(InputCommand input, Display display)
        {
            var commands = new List <IShellCommand>();
            var env      = new Env {
                Input = input, Display = display
            };

            // register commands
            RegisterCommands(commands);

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var s = new StringScanner(cmd);

                    var found = false;

                    // first test all shell app commands
                    foreach (var command in commands)
                    {
                        if (!command.IsCommand(s))
                        {
                            continue;
                        }

                        command.Execute(s, env, out var a, out var b);

                        found = true;
                        break;
                    }

                    // if not found, try database command
                    if (!found)
                    {
                        display.WriteResult(env.Engine.Run(cmd));
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex);
                }
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            LiteDatabase db      = null;
            var          input   = new InputCommand();
            var          display = new Display();

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            // if has a argument, its database file - try open
            if (args.Length > 0)
            {
                try
                {
                    db = new LiteDatabase(args[0]);
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }

            while (true)
            {
                // read next command from user
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var isConsoleCommand = ConsoleCommand.TryExecute(cmd, ref db, display, input);

                    if (isConsoleCommand == false)
                    {
                        if (db == null)
                        {
                            throw LiteException.NoDatabase();
                        }

                        var result = db.Run(cmd);

                        display.WriteResult(result);
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
Esempio n. 5
0
        public static void Start(InputCommand input, Display display)
        {
            var commands = new List<ICommand>();
            var env = new Env();

            // register commands
            RegisterCommands(commands);

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd)) continue;

                try
                {
                    var s = new StringScanner(cmd);

                    var found = false;

                    // test all commands
                    foreach (var command in commands)
                    {
                        if (!command.IsCommand(s)) continue;

                        // test if command it's only shell command
                        if (command.Access == DataAccess.None)
                        {
                            command.Execute(null, s, display, input, env);
                        }
                        else
                        {
                            using (var engine = env.CreateEngine(command.Access))
                            {
                                command.Execute(engine, s, display, input, env);
                            }
                        }

                        found = true;
                        break;
                    }

                    if (!found) throw new ShellExpcetion("Command not found");
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            var shell = new LiteShell();
            var input = new InputCommand();

            shell.RegisterAll();
            shell.Display.TextWriters.Add(Console.Out);

            // show welcome message
            shell.Display.WriteWelcome();

            // if has a argument, its database file - try open
            if (args.Length > 0)
            {
                try
                {
                    shell.Engine = new LiteEngine(args[0]);
                }
                catch (Exception ex)
                {
                    shell.Display.WriteError(ex.Message);
                }
            }

            while (true)
            {
                // read next command from user
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    if (cmd.StartsWith("open "))
                    {
                        if (shell.Engine != null)
                        {
                            shell.Engine.Dispose();
                        }

                        shell.Engine = new LiteEngine(cmd.Substring(5));
                    }
                    else
                    {
                        shell.Run(cmd);
                    }
                }
                catch (Exception ex)
                {
                    shell.Display.WriteError(ex.Message);
                }
            }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            LiteDatabase db = null;
            var input = new InputCommand();
            var display = new Display();

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            // if has a argument, its database file - try open
            if (args.Length > 0)
            {
                try
                {
                    db = new LiteDatabase(args[0]);
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }

            while (true)
            {
                // read next command from user
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd)) continue;

                try
                {
                    var isConsoleCommand = ConsoleCommand.TryExecute(cmd, ref db, display, input);

                    if (isConsoleCommand == false)
                    {
                        if(db == null) throw LiteException.NoDatabase();

                        var result = db.Run(cmd);

                        display.WriteResult(result);
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// If command is a console command, execute and returns true - if not, just returns false
        /// </summary>
        public static bool TryExecute(string command, LiteShell shell, Display display, InputCommand input)
        {
            var s = new StringScanner(command);

            foreach (var cmd in Commands)
            {
                if (cmd.IsCommand(s))
                {
                    cmd.Execute(shell, s, display, input);
                    return true;
                }
            }

            return false;
        }
Esempio n. 9
0
        /// <summary>
        /// If command is a console command, execute and returns true - if not, just returns false
        /// </summary>
        public static bool TryExecute(string command, ref IShellEngine engine, Display display, InputCommand input)
        {
            var s = new StringScanner(command);

            foreach (var cmd in _commands)
            {
                if (cmd.IsCommand(s))
                {
                    cmd.Execute(ref engine, s, display, input);
                    return true;
                }
            }

            return false;
        }
Esempio n. 10
0
        public static void Start(InputCommand input, Display display)
        {
            var env = new Env {
                Input = input, Display = display
            };

            // show welcome message
            display.WriteWelcome();

            Console.CancelKeyPress += (o, e) => { e.Cancel = true; env.Running = false; };

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var scmd = GetCommand(cmd);

                    if (scmd != null)
                    {
                        scmd(env);
                        continue;
                    }

                    // if string is not a shell command, try execute as sql command
                    if (env.Database == null)
                    {
                        throw new Exception("Database not connected");
                    }

                    env.Running = true;

                    display.WriteResult(env.Database.Execute(cmd), env);
                }
                catch (Exception ex)
                {
                    display.WriteError(ex);
                }
            }
        }
Esempio n. 11
0
        /// <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);
        }
Esempio n. 12
0
        /// <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 + indented
        /// --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);
        }
Esempio n. 13
0
        public static void Start(InputCommand input, Display display)
        {
            IShellEngine engine = null;

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var isConsoleCommand = ConsoleCommand.TryExecute(cmd, ref engine, display, input);

                    if (isConsoleCommand == false)
                    {
                        if (engine == null)
                        {
                            throw ShellExpcetion.NoDatabase();
                        }

                        engine.Run(cmd, display);
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            var shell = new Shell();
            var input = new InputCommand();

            shell.Display.TextWriters.Add(Console.Out);

            // show welcome message
            shell.Welcome();

            // if has a argument, its database file - try open
            if (args.Length > 0)
            {
                try
                {
                    shell.Engine = new LiteEngine(args[0]);
                }
                catch (Exception ex)
                {
                    shell.Display.WriteError(ex.Message);
                }
            }

            while (true)
            {
                // read next command from user
                var cmd = input.ReadCommand();

                try
                {
                    // run it
                    shell.Run(cmd);
                }
                catch (Exception ex)
                {
                    shell.Display.WriteError(ex.Message);
                }
            }
        }
Esempio n. 15
0
        public static void Start(InputCommand input, Display display)
        {
            var commands = new List <ICommand>();
            var env      = new Env();

            LiteEngine engine = null;

            // register commands
            RegisterCommands(commands);

            display.TextWriters.Add(Console.Out);

            // show welcome message
            display.WriteWelcome();

            while (input.Running)
            {
                // read next command from user or queue
                var cmd = input.ReadCommand();

                if (string.IsNullOrEmpty(cmd))
                {
                    continue;
                }

                try
                {
                    var s = new StringScanner(cmd);

                    var found = false;

                    // test all commands
                    foreach (var command in commands)
                    {
                        if (!command.IsCommand(s))
                        {
                            continue;
                        }

                        command.Execute(engine, s, display, input, env);

                        if (env.Filename != null && engine == null)
                        {
                            engine = env.CreateEngine(DataAccess.Write);
                        }

                        found = true;
                        break;
                    }

                    if (!found)
                    {
                        throw new ShellException("Command not found");
                    }
                }
                catch (Exception ex)
                {
                    display.WriteError(ex.Message);
                }
            }
        }
Esempio n. 16
0
        /// <summary>
        /// If command is a console command, execute and returns true - if not, just returns false
        /// </summary>
        public static bool TryExecute(string command, ref LiteDatabase db, Display display, InputCommand input)
        {
            var s = new StringScanner(command);

            foreach (var cmd in _commands)
            {
                if (cmd.IsCommand(s))
                {
                    cmd.Execute(ref db, s, display, input);
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 17
0
 public abstract void Execute(ref LiteDatabase db, StringScanner s, Display display, InputCommand input);
Esempio n. 18
0
 public abstract void Execute(ref LiteDatabase db, StringScanner s, Display display, InputCommand input);
Esempio n. 19
0
 public abstract void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input);
Esempio n. 20
0
 public abstract void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input);
Esempio n. 21
0
        /// <summary>
        /// If command is a console command, execute and returns true - if not, just returns false
        /// </summary>
        public static bool TryExecute(string command, LiteShell shell, Display display, InputCommand input)
        {
            var s = new StringScanner(command);

            foreach (var cmd in Commands)
            {
                if (cmd.IsCommand(s))
                {
                    cmd.Execute(shell, s, display, input);
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 22
0
 public abstract void Execute(LiteShell shell, StringScanner s, Display display, InputCommand input);
Esempio n. 23
0
 public abstract void Execute(LiteShell shell, StringScanner s, Display display, InputCommand input);