diff --git a/internal/cli/pipeline_cancel.go b/internal/cli/pipeline_cancel.go index 2a5c6fd..57c72cd 100644 --- a/internal/cli/pipeline_cancel.go +++ b/internal/cli/pipeline_cancel.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "io" "strings" "github.com/alyraffauf/tg/internal/app" @@ -30,15 +31,7 @@ git origin remote.`, return err } return output(cmd, result, func(result *app.PipelineCancelResult) { - if !result.CancellationRequested { - fmt.Fprintf(cmd.OutOrStdout(), "Pipeline %s has no pending or running workflows.\n", result.Pipeline) - return - } - if len(workflows) == 0 { - fmt.Fprintf(cmd.OutOrStdout(), "Cancellation requested for pipeline %s.\n", result.Pipeline) - return - } - fmt.Fprintf(cmd.OutOrStdout(), "Cancellation requested for workflows %s in pipeline %s.\n", strings.Join(result.Workflows, ", "), result.Pipeline) + renderPipelineCancellation(cmd.OutOrStdout(), result, len(workflows) > 0) }) }, } @@ -46,3 +39,19 @@ git origin remote.`, command.Flags().StringSliceVarP(&workflows, "workflow", "w", nil, "Workflow name to cancel (repeatable)") return command } + +func renderPipelineCancellation(writer io.Writer, result *app.PipelineCancelResult, selectedWorkflows bool) { + if !result.CancellationRequested { + if selectedWorkflows { + fmt.Fprintln(writer, "None of the selected workflows are pending or running.") + return + } + fmt.Fprintf(writer, "Pipeline %s has no pending or running workflows.\n", result.Pipeline) + return + } + if !selectedWorkflows { + fmt.Fprintf(writer, "Cancellation requested for pipeline %s.\n", result.Pipeline) + return + } + fmt.Fprintf(writer, "Cancellation requested for workflows %s in pipeline %s.\n", strings.Join(result.Workflows, ", "), result.Pipeline) +} diff --git a/internal/cli/pipeline_cancel_test.go b/internal/cli/pipeline_cancel_test.go index 55b1165..1cefebf 100644 --- a/internal/cli/pipeline_cancel_test.go +++ b/internal/cli/pipeline_cancel_test.go @@ -1,6 +1,7 @@ package cli import ( + "bytes" "testing" "github.com/alyraffauf/tg/internal/app" @@ -17,3 +18,13 @@ func TestPipelineCancelCommandFlags(t *testing.T) { } } } + +func TestRenderPipelineCancellationForUncancellableSelection(t *testing.T) { + var output bytes.Buffer + renderPipelineCancellation(&output, &app.PipelineCancelResult{Pipeline: "pipeline-123"}, true) + + const want = "None of the selected workflows are pending or running.\n" + if output.String() != want { + t.Fatalf("renderPipelineCancellation() = %q, want %q", output.String(), want) + } +}