| File: Commands\SecretGetCommand.cs | Web Access |
| Project: src\src\Aspire.Cli\Aspire.Cli.csproj (aspire) |
// 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.Interaction; using Aspire.Cli.Resources; using Aspire.Cli.Secrets; 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 AspireSecretsStoreResolver _secretsStoreResolver; public SecretGetCommand( AspireSecretsStoreResolver secretsStoreResolver, CommonCommandServices services) : base("get", SecretCommandStrings.GetDescription, services) { _secretsStoreResolver = secretsStoreResolver; Arguments.Add(s_keyArgument); Options.Add(SecretCommand.s_appHostOption); Options.Add(SecretCommand.s_environmentOption); } protected override async Task<CommandResult> ExecuteAsync(ParseResult parseResult, CancellationToken cancellationToken) { // Argument arity guarantees non-null var key = parseResult.GetValue(s_keyArgument)!; var projectFile = parseResult.GetValue(SecretCommand.s_appHostOption); var environment = parseResult.GetValue(SecretCommand.s_environmentOption); var result = await _secretsStoreResolver.ResolveAsync(projectFile, environment, cancellationToken); if (result is null) { return CommandResult.Failure(CliExitCodes.FailedToFindProject, SecretCommandStrings.CouldNotFindAppHost); } var value = result.GetReadStore().Get(key); if (value is null) { return CommandResult.Failure(CliExitCodes.ConfigNotFound, string.Format(CultureInfo.CurrentCulture, SecretCommandStrings.SecretNotFound, key.EscapeMarkup())); } // Write value to stdout (machine-readable) InteractionService.DisplayRawText(value, consoleOverride: ConsoleOutput.Standard); return CommandResult.Success(); } }