|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.CommandLine;
using System.Globalization;
using Aspire.Cli.Configuration;
using Aspire.Cli.Interaction;
using Aspire.Cli.Resources;
using Aspire.Cli.Secrets;
using Aspire.Cli.Telemetry;
using Aspire.Cli.Utils;
using Spectre.Console;
namespace Aspire.Cli.Commands;
/// <summary>
/// Gets a secret value from an AppHost project.
/// </summary>
internal sealed class SecretGetCommand : BaseCommand
{
private static readonly Argument<string> s_keyArgument = new("key")
{
Description = SecretCommandStrings.KeyRetrieveArgumentDescription
};
private readonly SecretStoreResolver _secretStoreResolver;
public SecretGetCommand(
IInteractionService interactionService,
SecretStoreResolver secretStoreResolver,
IFeatures features,
ICliUpdateNotifier updateNotifier,
CliExecutionContext executionContext,
AspireCliTelemetry telemetry)
: base("get", SecretCommandStrings.GetDescription, features, updateNotifier, executionContext, interactionService, telemetry)
{
_secretStoreResolver = secretStoreResolver;
Arguments.Add(s_keyArgument);
Options.Add(SecretCommand.s_appHostOption);
}
protected override async Task<int> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken)
{
// Argument arity guarantees non-null
var key = parseResult.GetValue(s_keyArgument)!;
var projectFile = parseResult.GetValue(SecretCommand.s_appHostOption);
var result = await _secretStoreResolver.ResolveAsync(projectFile, autoInit: false, cancellationToken);
if (result is null)
{
InteractionService.DisplayError(SecretCommandStrings.CouldNotFindAppHost);
return ExitCodeConstants.FailedToFindProject;
}
var value = result.Store.Get(key);
if (value is null)
{
InteractionService.DisplayError(string.Format(CultureInfo.CurrentCulture, SecretCommandStrings.SecretNotFound, key.EscapeMarkup()));
return ExitCodeConstants.ConfigNotFound;
}
// Write value to stdout (machine-readable)
InteractionService.DisplayPlainText(value);
return ExitCodeConstants.Success;
}
}
|