Skip to content
Merged
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
@@ -0,0 +1,76 @@
using System.CommandLine.Invocation;
using ChilliCream.Nitro.CommandLine.Client;
using ChilliCream.Nitro.CommandLine.Commands.Apis.Inputs;
using ChilliCream.Nitro.CommandLine.Commands.Stages.Components;
using ChilliCream.Nitro.CommandLine.Configuration;
using ChilliCream.Nitro.CommandLine.Helpers;
using ChilliCream.Nitro.CommandLine.Options;
using ChilliCream.Nitro.CommandLine.Results;
using ChilliCream.Nitro.CommandLine.Services.Sessions;
using static ChilliCream.Nitro.CommandLine.ThrowHelper;

namespace ChilliCream.Nitro.CommandLine.Commands.Stages;

internal sealed class DeleteStageCommand : Command
{
public DeleteStageCommand() : base("delete")
{
Description = "Deletes a stage by name";

AddOption(Opt<OptionalApiIdOption>.Instance);
AddOption(Opt<StageNameOption>.Instance);
AddOption(Opt<ForceOption>.Instance);
Comment thread
PascalSenn marked this conversation as resolved.

this.SetHandler(
ExecuteAsync,
Bind.FromServiceProvider<InvocationContext>(),
Bind.FromServiceProvider<IAnsiConsole>(),
Bind.FromServiceProvider<IApiClient>(),
Bind.FromServiceProvider<CancellationToken>());
}

private static async Task<int> ExecuteAsync(
InvocationContext context,
IAnsiConsole console,
IApiClient client,
CancellationToken cancellationToken)
{
const string apiMessage = "For which API do you want to force delete a stage?";
var apiId = await context.GetOrSelectApiId(apiMessage);

var stageName = context.ParseResult.GetValueForOption(Opt<StageNameOption>.Instance)!;

var shouldDelete = await context.ConfirmWhenNotForced(
$"Do you really want to force delete stage {stageName.AsHighlight()}",
cancellationToken);

if (!shouldDelete)
{
throw Exit("Stage was not deleted");
Comment thread
PascalSenn marked this conversation as resolved.
}

var input = new ForceDeleteStageByApiIdInput { ApiId = apiId, StageName = stageName };
var result = await client.ForceDeleteStageByApiIdCommandMutation
.ExecuteAsync(input, cancellationToken);

console.EnsureNoErrors(result);
var data = console.EnsureData(result);
console.PrintErrorsAndExit(data.ForceDeleteStageByApiId.Errors);

var api = data.ForceDeleteStageByApiId.Api;
if (api is null)
{
throw Exit("Could not delete the stage");
}

var items = api.Stages
.Select(x => StageDetailPrompt.From(x).ToObject())
.ToArray();

context.SetResult(new PaginatedListResult<StageDetailPrompt.StageDetailPromptResult>(items, null));

console.OkLine($"Stage {stageName.AsHighlight()} was force deleted");

return ExitCodes.Success;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mutation ForceDeleteStageByApiIdCommandMutation($input: ForceDeleteStageByApiIdInput!) {
forceDeleteStageByApiId(input: $input) {
api {
stages {
...StageDetailPrompt_Stage
}
}
errors {
...Error
...ApiNotFoundError
...StageNotFoundError
...UnauthorizedOperation
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public StageCommand() : base("stage")
this.AddNitroCloudDefaultOptions();

AddCommand(new EditStagesCommand());
AddCommand(new DeleteStageCommand());
AddCommand(new ListStagesCommand());
}
}
Loading
Loading