Skip to content

Commit

Permalink
added confirmation prompt to console service, have to add it to more tho
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Beier committed Apr 20, 2024
1 parent a220fc5 commit 3aa3a2d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,38 @@ namespace KeyVaultCli.Application.Vault.Commands.RestoreVault;

public class RestoreVaultCommand(IVault vault, IConsole consoleService) : ICommand
{
string restroedSuccessMessage = " ____ _ _ \n | _ \\ ___ ___| |_ ___ _ __ ___ __| |\n | |_) / _ \\/ __| __/ _ \\| '__/ _ \\/ _` |\n | _ < __/\\__ \\ || (_) | | | __/ (_| |\n |_| \\_\\___||___/\\__\\___/|_| \\___|\\__,_|\n ";

private readonly string restoreSuccessMsg = " ____ _ _ \n | _ \\ ___ ___| |_ ___ _ __ ___ __| |\n | |_) / _ \\/ __| __/ _ \\| '__/ _ \\/ _` |\n | _ < __/\\__ \\ || (_) | | | __/ (_| |\n |_| \\_\\___||___/\\__\\___/|_| \\___|\\__,_|\n ";

private string confirmationPrompt = "Are you sure you want to restore the vault?";
private string filePathPrompt = "Enter the full path to the backup file: ";

private readonly string restoreErrorMsg = "Failed to restore vault.";
private readonly string operationCancelMsg = "Operation cancelled.";

public void Execute()
{
var confirmation = consoleService.GetInputFromPrompt("Are you sure you want to restore the vault? (yes/no): ");
if (confirmation.ToLower().Equals("yes"))
if (consoleService.GetUserConfirmation(confirmationPrompt))
{
PerformRestore();
}
else
{
consoleService.WriteError(operationCancelMsg);
}
}

private void PerformRestore()
{
var backupFilePath = consoleService.GetInputFromPrompt(filePathPrompt);
var success = vault.RestoreVault(backupFilePath);
if (success)
{
var backupFilePath = consoleService.GetInputFromPrompt("Enter the full path to the backup file: ");
var success = vault.RestoreVault(backupFilePath);
if (success)
{
consoleService.WriteSuccess(restroedSuccessMessage);
consoleService.WriteSuccess("Restored all Passwords from backup, and added them to the vault.");
}
else
{
consoleService.WriteError("Failed to restore vault.");
}
consoleService.WriteSuccess(restoreSuccessMsg);
consoleService.WriteSuccess("Restored all Passwords from backup, and added them to the vault.");
}
else
{
consoleService.WriteError("Operation cancelled.");
consoleService.WriteError(restoreErrorMsg);
}
}
}
1 change: 1 addition & 0 deletions src/KeyVaultCli.Domain/Common/Interfaces/IConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public interface IConsole
{
string GetInputFromPrompt(string prompt);
bool GetUserConfirmation(string promptMessage);
void WriteText(string message);
void WriteInfo(string message);
void WriteSuccess(string message);
Expand Down
20 changes: 20 additions & 0 deletions src/KeyVaultCli.Presentation/Services/ConsoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ public string GetInputFromPrompt(string prompt)
return input ?? string.Empty;
}

public bool GetUserConfirmation(string promptMessage)
{
while (true)
{
var input = GetInputFromPrompt(promptMessage + " (y/n): ");

if (input.Equals("y", StringComparison.OrdinalIgnoreCase))
{
return true;
}

if (input.Equals("n", StringComparison.OrdinalIgnoreCase))
{
return false;
}

WriteInfo("Invalid input. Please enter 'y' or 'n'.");
}
}

public void WriteText(string message)
{
var regularText = new Text(message, colors.White);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace KeyVaultCli.Domain.UnitTests.Fakes;
public class FakeConsoleService : IConsoleService
{
public string GetInputFromPrompt(string prompt) { return string.Empty; }
public bool GetUserConfirmation(string promptMessage) { return true; }
public void WriteText(string message) { /* do nothing */ }
public void WriteInfo(string message) { /* do nothing */ }
public void WriteSuccess(string message) { /* do nothing */ }
Expand Down

0 comments on commit 3aa3a2d

Please sign in to comment.