fix: return non-exit code when uc commands cancelled by not confirming prompt (fixes #354)

This commit is contained in:
Pasha Sviderski
2026-05-05 10:41:53 +10:00
parent 34560470ce
commit 5ecd09aa2b
6 changed files with 28 additions and 9 deletions
+1 -2
View File
@@ -175,8 +175,7 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
return fmt.Errorf("confirm deployment: %w", err)
}
if !confirmed {
fmt.Println("Cancelled. No changes were made.")
return nil
return cli.Cancelled("Caddy deploy cancelled. No changes were made.")
}
err = progress.RunWithTitle(ctx, func(ctx context.Context) error {
+1 -2
View File
@@ -222,8 +222,7 @@ func runDeploy(ctx context.Context, uncli *cli.CLI, opts deployOptions) error {
return fmt.Errorf("confirm deployment: %w", err)
}
if !confirmed {
fmt.Println("Cancelled. No changes were made.")
return nil
return cli.Cancelled("Deploy cancelled. No changes were made.")
}
}
+1 -2
View File
@@ -232,8 +232,7 @@ func add(ctx context.Context, uncli *cli.CLI, remoteMachine *cli.RemoteMachine,
return fmt.Errorf("confirm deployment: %w", err)
}
if !confirmed {
fmt.Println("Cancelled. No changes were made.")
return nil
return cli.Cancelled("Caddy deploy cancelled. The machine has been added to the cluster.")
}
}
+9 -1
View File
@@ -2,8 +2,10 @@ package main
import (
"context"
"errors"
"fmt"
"net/netip"
"os"
"strings"
"charm.land/lipgloss/v2"
@@ -152,5 +154,11 @@ func main() {
volume.NewRootCommand(),
wg.NewRootCommand(),
)
cobra.CheckErr(cmd.Execute())
if err := cmd.Execute(); err != nil {
if cancelled, ok := errors.AsType[*cli.CancelledError](err); ok {
fmt.Fprintln(os.Stderr, cancelled.Error())
os.Exit(1)
}
cobra.CheckErr(err)
}
}
+1 -2
View File
@@ -155,8 +155,7 @@ func scale(ctx context.Context, uncli *cli.CLI, opts scaleOptions) error {
return fmt.Errorf("confirm scaling: %w", err)
}
if !confirmed {
fmt.Println("Cancelled. No changes were made.")
return nil
return cli.Cancelled("Scaling cancelled. No changes were made.")
}
}
+15
View File
@@ -0,0 +1,15 @@
package cli
// CancelledError signals an interactive command was aborted by the user at a confirmation prompt.
type CancelledError struct {
Message string
}
func (e *CancelledError) Error() string {
return e.Message
}
// Cancelled returns a CancelledError carrying the given user-facing message.
func Cancelled(msg string) error {
return &CancelledError{Message: msg}
}