Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Fallout.Cli.Prompts;
using Fallout.Common;
using Fallout.Common.IO;
using Fallout.Common.Utilities;
using Fallout.Common.Utilities.Collections;
using static Fallout.Common.Constants;
using static Fallout.Common.Utilities.EncryptionUtility;

namespace Fallout.Cli;
namespace Fallout.Cli.Commands;

// TODO: unlock prompt
// TODO: environment variable name
// TODO: profile vs. environment
// TODO: nuke :profile <name>
partial class Program

/// <summary>
/// <c>fallout :secrets</c>: interactively encrypts secret parameters into the (optionally profile-scoped)
/// parameters file, managing the keychain password.
/// </summary>
public sealed class SecretsCommand : IFalloutCommand
{
private const string SaveAndExit = "<Save & Exit>";
private const string DiscardAndExit = "<Discard & Exit>";
private const string DeletePasswordAndExit = "<Delete Password & Exit>";

private readonly IConsolePrompts _prompts;

public SecretsCommand(IConsolePrompts prompts) => _prompts = prompts;

public string Name => "secrets";

// ReSharper disable once CognitiveComplexity
public static int Secrets(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
public int Execute(string[] args, AbsolutePath rootDirectory, AbsolutePath buildScript)
{
var secretParameters = CompletionUtility.GetItemsFromSchema(
GetBuildSchemaFile(rootDirectory.NotNull("No root directory")),
Expand Down Expand Up @@ -62,7 +72,7 @@ public static int Secrets(string[] args, AbsolutePath rootDirectory, AbsolutePat

if (EnvironmentInfo.IsOsx && existingSecrets.Count == 0 && !fromCredentialStore)
{
if (generatedPassword || PromptForConfirmation($"Save password to keychain? (associated with '{rootDirectory}')"))
if (generatedPassword || _prompts.PromptForConfirmation($"Save password to keychain? (associated with '{rootDirectory}')"))
CredentialStore.SavePassword(credentialStoreName, password);
}
else if (fromLegacyCredentialStore)
Expand All @@ -78,13 +88,13 @@ public static int Secrets(string[] args, AbsolutePath rootDirectory, AbsolutePat
var addedSecrets = new Dictionary<string, string>();
while (true)
{
var choice = PromptForChoice(
var choice = _prompts.PromptForChoice(
"Choose secret parameter to enter value:",
options.Select(x => (x, addedSecrets.ContainsKey(x) || existingSecrets.ContainsKey(x) ? $"* {x}" : x)).ToArray());

if (!choice.EqualsAnyOrdinalIgnoreCase(SaveAndExit, DiscardAndExit, DeletePasswordAndExit))
{
addedSecrets[choice] = PromptForSecret(choice);
addedSecrets[choice] = _prompts.PromptForSecret(choice);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/Fallout.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ private static void RegisterCommands(IServiceCollection services)
// Legacy handlers still living on Program, adapted until they are extracted into command
// types. Each conversion deletes one line here plus its Program.X.cs partial.
services.AddSingleton<IFalloutCommand, UpdateCommand>();
services.AddSingleton<IFalloutCommand, SecretsCommand>();

// Legacy handlers still living on Program, adapted until they are extracted into command
// types. Each conversion deletes one line here plus its Program.X.cs partial.
services.AddSingleton<IFalloutCommand>(new DelegateCommand("cake-convert", CakeConvert));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("cake-clean", CakeClean));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("secrets", Secrets));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("GetNextDirectory", GetNextDirectory));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("PopDirectory", PopDirectory));
services.AddSingleton<IFalloutCommand>(new DelegateCommand("PushWithCurrentRootDirectory", PushWithCurrentRootDirectory));
Expand Down
22 changes: 22 additions & 0 deletions tests/Fallout.Cli.Tests/Commands/SecretsCommandTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Fallout.Cli.Commands;
using FluentAssertions;
using Xunit;

namespace Fallout.Cli.Tests.Commands;

public class SecretsCommandTests
{
[Fact]
public void Name_IsSecrets()
=> new SecretsCommand(new FakeConsolePrompts()).Name.Should().Be("secrets");

[Fact]
public void Execute_WithoutRootDirectory_Throws()
{
var action = () => new SecretsCommand(new FakeConsolePrompts())
.Execute([], rootDirectory: null, buildScript: null);

action.Should().Throw<Exception>().WithMessage("*No root directory*");
}
}