|
| 1 | +using System.CommandLine.Invocation; |
| 2 | +using ChilliCream.Nitro.CommandLine.Client; |
| 3 | +using ChilliCream.Nitro.CommandLine.Commands.Apis.Inputs; |
| 4 | +using ChilliCream.Nitro.CommandLine.Commands.Stages.Components; |
| 5 | +using ChilliCream.Nitro.CommandLine.Configuration; |
| 6 | +using ChilliCream.Nitro.CommandLine.Helpers; |
| 7 | +using ChilliCream.Nitro.CommandLine.Options; |
| 8 | +using ChilliCream.Nitro.CommandLine.Results; |
| 9 | +using ChilliCream.Nitro.CommandLine.Services.Sessions; |
| 10 | +using static ChilliCream.Nitro.CommandLine.ThrowHelper; |
| 11 | + |
| 12 | +namespace ChilliCream.Nitro.CommandLine.Commands.Stages; |
| 13 | + |
| 14 | +internal sealed class DeleteStageCommand : Command |
| 15 | +{ |
| 16 | + public DeleteStageCommand() : base("delete") |
| 17 | + { |
| 18 | + Description = "Deletes a stage by name"; |
| 19 | + |
| 20 | + AddOption(Opt<OptionalApiIdOption>.Instance); |
| 21 | + AddOption(Opt<StageNameOption>.Instance); |
| 22 | + AddOption(Opt<ForceOption>.Instance); |
| 23 | + |
| 24 | + this.SetHandler( |
| 25 | + ExecuteAsync, |
| 26 | + Bind.FromServiceProvider<InvocationContext>(), |
| 27 | + Bind.FromServiceProvider<IAnsiConsole>(), |
| 28 | + Bind.FromServiceProvider<IApiClient>(), |
| 29 | + Bind.FromServiceProvider<CancellationToken>()); |
| 30 | + } |
| 31 | + |
| 32 | + private static async Task<int> ExecuteAsync( |
| 33 | + InvocationContext context, |
| 34 | + IAnsiConsole console, |
| 35 | + IApiClient client, |
| 36 | + CancellationToken cancellationToken) |
| 37 | + { |
| 38 | + const string apiMessage = "For which API do you want to force delete a stage?"; |
| 39 | + var apiId = await context.GetOrSelectApiId(apiMessage); |
| 40 | + |
| 41 | + var stageName = context.ParseResult.GetValueForOption(Opt<StageNameOption>.Instance)!; |
| 42 | + |
| 43 | + var shouldDelete = await context.ConfirmWhenNotForced( |
| 44 | + $"Do you really want to force delete stage {stageName.AsHighlight()}", |
| 45 | + cancellationToken); |
| 46 | + |
| 47 | + if (!shouldDelete) |
| 48 | + { |
| 49 | + throw Exit("Stage was not deleted"); |
| 50 | + } |
| 51 | + |
| 52 | + var input = new ForceDeleteStageByApiIdInput { ApiId = apiId, StageName = stageName }; |
| 53 | + var result = await client.ForceDeleteStageByApiIdCommandMutation |
| 54 | + .ExecuteAsync(input, cancellationToken); |
| 55 | + |
| 56 | + console.EnsureNoErrors(result); |
| 57 | + var data = console.EnsureData(result); |
| 58 | + console.PrintErrorsAndExit(data.ForceDeleteStageByApiId.Errors); |
| 59 | + |
| 60 | + var api = data.ForceDeleteStageByApiId.Api; |
| 61 | + if (api is null) |
| 62 | + { |
| 63 | + throw Exit("Could not delete the stage"); |
| 64 | + } |
| 65 | + |
| 66 | + var items = api.Stages |
| 67 | + .Select(x => StageDetailPrompt.From(x).ToObject()) |
| 68 | + .ToArray(); |
| 69 | + |
| 70 | + context.SetResult(new PaginatedListResult<StageDetailPrompt.StageDetailPromptResult>(items, null)); |
| 71 | + |
| 72 | + console.OkLine($"Stage {stageName.AsHighlight()} was force deleted"); |
| 73 | + |
| 74 | + return ExitCodes.Success; |
| 75 | + } |
| 76 | +} |
0 commit comments