Skip to content

Commit

Permalink
refactored main entry
Browse files Browse the repository at this point in the history
  • Loading branch information
BeierKevin committed Apr 20, 2024
1 parent f472d8e commit 8e73768
Showing 1 changed file with 57 additions and 27 deletions.
84 changes: 57 additions & 27 deletions src/KeyVaultCli.Presentation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using KeyVaultCli.Application.Vault.Commands.CreateVault;
using KeyVaultCli.Application.Vault.Commands.DeleteVault;
using KeyVaultCli.Application.Vault.Commands.RestoreVault;
using KeyVaultCli.Domain.Common.Interfaces;
using KeyVaultCli.Infrastructure.Services;
using KeyVaultCli.Presentation.Services;
using KeyVaultCli.Presentation.UserInterface;
Expand All @@ -30,14 +31,26 @@ private static void Main()
{ CommandFlag.CreateVault, new CreateVaultCommand(vaultFactory, vaultConsoleService) }
});

Console.Title = "KeyVaultCli";
PrintLogo(vaultConsoleService);

vaultConsoleService.WriteInfo(Logo.asciiStandard, true);
createVaultCommand.ExecuteCommand(CommandFlag.CreateVault, out var error);
var vault = vaultFactory.GetVault();

var listOfCommands = RegisterCommands(vaultConsoleService, vaultFactory, vault);
ICommandService commandService = new CommandService(listOfCommands);

// Command Pattern
var commands = new Dictionary<CommandFlag, ICommand>
ProcessInputCommands(vaultConsoleService, commandService);
}

private static void PrintLogo(IConsoleService vaultConsoleService)
{
Console.Title = "KeyVaultCli";
vaultConsoleService.WriteInfo(Logo.asciiStandard, true);
}

private static Dictionary<CommandFlag, ICommand> RegisterCommands(IConsoleService vaultConsoleService, IVaultFactory vaultFactory, IVault vault)
{
return new Dictionary<CommandFlag, ICommand>
{
{ CommandFlag.CreatePassword, new CreatePasswordCommand(vault, vaultConsoleService) },
{ CommandFlag.CreatePasswordGenerated, new CreatePasswordGenerateCommand(vault, vaultConsoleService) },
Expand All @@ -54,18 +67,28 @@ private static void Main()
{ CommandFlag.DeleteAllPasswords, new DeleteAllPasswordsCommand(vault, vaultConsoleService) },
{ CommandFlag.DeleteVault, new DeleteVaultCommand(vaultFactory, vaultConsoleService) }
};

ICommandService commandService = new CommandService(commands);

}

private static void ProcessInputCommands(IConsoleService vaultConsoleService, ICommandService commandService)
{
string command;
do
{
vaultConsoleService.WriteText("Enter a command:", true);
command = PrintPrompt(vaultConsoleService);

ExecuteCommand(vaultConsoleService, commandService, command);
}
while (!IsExitCommand(commandService, command));
}

private static string PrintPrompt(IConsoleService vaultConsoleService)
{
vaultConsoleService.WriteText("Enter a command:", true);

// General commands
vaultConsoleService.WriteInfo("---- General commands ----", true);
vaultConsoleService.WriteInfo((int)CommandFlag.Exit + ". Exit");

// Password related commands
vaultConsoleService.WriteInfo("---- Password related commands ----", true);
vaultConsoleService.WriteInfo((int)CommandFlag.CreatePassword + ". Create password entry");
Expand All @@ -77,37 +100,44 @@ private static void Main()
vaultConsoleService.WriteInfo((int)CommandFlag.DeletePassword + ". Delete password entry");
vaultConsoleService.WriteInfo((int)CommandFlag.DeleteAllPasswords + ". Delete all passwords in vault");
vaultConsoleService.WriteInfo((int)CommandFlag.SearchPasswordEntries + ". Search password entries");

// Vault related commands
vaultConsoleService.WriteInfo("---- Vault related commands ----", true);
vaultConsoleService.WriteInfo((int)CommandFlag.CreateVault + ". Create vault");
vaultConsoleService.WriteInfo((int)CommandFlag.BackupVault + ". Backup vault / Export Vault");
vaultConsoleService.WriteInfo((int)CommandFlag.RestoreVault + ". Restore vault / Import Vault");
vaultConsoleService.WriteInfo((int)CommandFlag.DeleteVault + ". Delete Vault");

// Master password
vaultConsoleService.WriteInfo("---- Master password ----", true);
vaultConsoleService.WriteInfo((int)CommandFlag.UpdateMasterPassword + ". Update Master Password");

return vaultConsoleService.GetInputFromPrompt("Enter your choice: ", true);
}

command = vaultConsoleService.GetInputFromPrompt("Enter your choice: ", true);

var validationErrorMessage = commandService.GetCommandValidationErrorMessage(command);
if (validationErrorMessage == null)
private static void ExecuteCommand(IConsoleService vaultConsoleService, ICommandService commandService, string command)
{
var validationErrorMessage = commandService.GetCommandValidationErrorMessage(command);
if (validationErrorMessage == null)
{
var commandFlag = (CommandFlag) Enum.Parse(typeof(CommandFlag), command);
if (commandService.ExecuteCommand(commandFlag, out var executionError))
{
var commandFlag = Enum.Parse<CommandFlag>(command);
if (commandService.ExecuteCommand(commandFlag, out var executionError))
{
vaultConsoleService.WriteText("------------------------");
}
else
{
vaultConsoleService.WriteError(executionError);
}
vaultConsoleService.WriteText("------------------------");
}
else
else
{
vaultConsoleService.WriteError(validationErrorMessage);
vaultConsoleService.WriteError(executionError);
}
} while (commandService.IsExitCommand(command) == false);
}
else
{
vaultConsoleService.WriteError(validationErrorMessage);
}
}

private static bool IsExitCommand(ICommandService commandService, string command)
{
return commandService.IsExitCommand(command);
}
}

0 comments on commit 8e73768

Please sign in to comment.