Skip to content

Commit 6c2c814

Browse files
authored
Adds stage delete command (#9417)
1 parent 72cf734 commit 6c2c814

6 files changed

Lines changed: 71638 additions & 69884 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mutation ForceDeleteStageByApiIdCommandMutation($input: ForceDeleteStageByApiIdInput!) {
2+
forceDeleteStageByApiId(input: $input) {
3+
api {
4+
stages {
5+
...StageDetailPrompt_Stage
6+
}
7+
}
8+
errors {
9+
...Error
10+
...ApiNotFoundError
11+
...StageNotFoundError
12+
...UnauthorizedOperation
13+
}
14+
}
15+
}

src/Nitro/CommandLine/src/CommandLine/Commands/Stages/StageCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public StageCommand() : base("stage")
1717
this.AddNitroCloudDefaultOptions();
1818

1919
AddCommand(new EditStagesCommand());
20+
AddCommand(new DeleteStageCommand());
2021
AddCommand(new ListStagesCommand());
2122
}
2223
}

0 commit comments

Comments
 (0)