private void WriteResult(BsonValue result, Display display)
        {
            var index = 0;

            if (result.IsNull) return;

            if (result.IsDocument)
            {
                display.WriteLine(ConsoleColor.DarkCyan, JsonSerializer.Serialize(result, display.Pretty, false));
            }
            else if (result.IsArray)
            {
                foreach (var doc in result.AsArray)
                {
                    display.Write(ConsoleColor.Cyan, string.Format("[{0}]:{1}", ++index, display.Pretty ? Environment.NewLine : " "));
                    display.WriteLine(ConsoleColor.DarkCyan, JsonSerializer.Serialize(doc, display.Pretty, false));
                }

                if (index == 0)
                {
                    display.WriteLine(ConsoleColor.DarkCyan, "no documents");
                }
            }
            else if (result.IsString)
            {
                display.WriteLine(ConsoleColor.DarkCyan, result.AsString);
            }
            else
            {
                display.WriteLine(ConsoleColor.DarkCyan, JsonSerializer.Serialize(result, display.Pretty, false));
            }
        }
Example #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);
                }
            }
        }
Example #3
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);
        }
Example #4
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);
                }
            }
        }
Example #5
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);
                }
            }
        }
        /// <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;
        }
Example #7
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;
        }
Example #8
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);
        }
 public void Run(string command, Display display)
 {
     throw new NotImplementedException("This command does not work in this version");
 }
 public abstract void Execute(ref IShellEngine engine, StringScanner s, Display display, InputCommand input);
        public void Run(string command, Display display)
        {
            var result = _db.RunCommand(command);

            this.WriteResult(result, display);
        }
Example #12
0
 public abstract void Execute(LiteShell shell, StringScanner s, Display display, InputCommand input);
Example #13
0
 public abstract void Execute(ref LiteDatabase db, StringScanner s, Display display, InputCommand input);
Example #14
0
        public void Run(string command, Display display)
        {
            var result = _db.RunCommand(command);

            this.WriteResult(result, display);
        }