Ejemplo n.º 1
0
        private static async Task MainLoop()
        {
            var storage = new JsonConfigurationStorage();
            var api     = new KeeperEndpoint(storage);
            var auth    = new AuthContext(api, new Ui());

            CliCommands commands = new NotConnectedCliCommands(auth);
            var         conf     = storage.Get();

            if (!string.IsNullOrEmpty(conf.LastLogin))
            {
                commands.CommandQueue.Enqueue(string.Format("login {0}", conf.LastLogin));
            }

            while (commands != null && !commands.Finished)
            {
                string command = null;
                if (commands.CommandQueue.Count > 0)
                {
                    command = commands.CommandQueue.Dequeue();
                }
                else
                {
                    Console.Write(commands.GetPrompt() + "> ");
                    command = Console.ReadLine();
                }
                if (!string.IsNullOrEmpty(command))
                {
                    command = command.Trim();
                    string parameter = "";
                    int    pos       = command.IndexOf(' ');
                    if (pos > 1)
                    {
                        parameter = command.Substring(pos + 1).Trim();
                        parameter = parameter.Trim('"');
                        command   = command.Substring(0, pos).Trim();
                    }
                    command = command.ToLowerInvariant();
                    if (commands.CommandAliases.TryGetValue(command, out string full_command))
                    {
                        command = full_command;
                    }
                    if (commands.Commands.TryGetValue(command, out ICommand cmd))
                    {
                        try
                        {
                            await cmd.ExecuteCommand(parameter);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error: " + e.Message);
                        }
                    }
                    else
                    {
                        if (command != "?")
                        {
                            Console.WriteLine(string.Format("Invalid command: {0}", command));
                        }
                        foreach (var c in commands.Commands.OrderBy(x => x.Value.Order))
                        {
                            Console.WriteLine("    " + c.Key.PadRight(16) + c.Value.Description);
                        }
                    }

                    if (commands.Finished)
                    {
                        commands = commands.NewCommands;
                    }
                    Console.WriteLine();
                }
            }
        }
Ejemplo n.º 2
0
        public ConnectedCommands(Vault vault) : base()
        {
            _vault = vault;

            Commands.Add("list", new ParsableCommand <ListCommandOptions>
            {
                Order       = 10,
                Description = "List folder content",
                Action      = ListCommand
            });

            Commands.Add("cd", new SimpleCommand
            {
                Order       = 11,
                Description = "Change current folder",
                Action      = ChangeDirectoryCommand
            });

            Commands.Add("tree", new ParsableCommand <TreeCommandOptions>
            {
                Order       = 12,
                Description = "Display folder structure",
                Action      = TreeCommand
            });

            Commands.Add("get", new SimpleCommand
            {
                Order       = 13,
                Description = "Display specified Keeper record/folder/team",
                Action      = GetCommand
            });

            Commands.Add("add-record", new ParsableCommand <AddRecordOptions>
            {
                Order       = 20,
                Description = "Add record",
                Action      = AddRecordCommand
            });

            Commands.Add("update-record", new ParsableCommand <UpdateRecordOptions>
            {
                Order       = 21,
                Description = "Update record",
                Action      = UpdateRecordCommand
            });

            Commands.Add("list-sf", new SimpleCommand
            {
                Order       = 22,
                Description = "List shared folders",
                Action      = ListSharedFoldersCommand
            });

            Commands.Add("sync-down", new SimpleCommand
            {
                Order       = 100,
                Description = "Download & decrypt data",
                Action      = async(_) =>
                {
                    Console.WriteLine("Syncing...");
                    await _vault.SyncDown();
                }
            });

            Commands.Add("logout", new SimpleCommand
            {
                Order       = 200,
                Description = "Logout",
                Action      = (_) =>
                {
                    _vault.Auth.Logout();
                    Finished    = true;
                    NewCommands = new NotConnectedCliCommands(_vault.Auth);
                    return(Task.FromResult(false));
                }
            });

            CommandAliases.Add("ls", "list");
            CommandAliases.Add("d", "sync-down");
            CommandAliases.Add("add", "add-record");
            CommandAliases.Add("upd", "update-record");
        }