💥 Add --dry-run and --yes to mutating subcommands

This commit is contained in:
OpenWaygate committed 2026-04-21 21:48:22 +08:00
1 parent 31ad273946
commit b55029cb27
40 files changed
+574 -76

No files matched your search

+3
View File
@@ -1,3 +1,6 @@
// Copyright 2026 eat-pray-ai & OpenWaygate
// SPDX-License-Identifier: Apache-2.0
package auth
import (
+8 -3
View File
@@ -7,6 +7,7 @@ import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os/exec"
@@ -99,9 +100,13 @@ func ExtractHl(uri string) string {
return ""
}
// ErrDryRun is returned when --dry-run is set; HandleCmdError silently ignores it.
var ErrDryRun = errors.New("dry run")
func HandleCmdError(err error, cmd *cobra.Command) {
if err != nil {
_ = cmd.Help()
cmd.PrintErrf("Error: %v\n", err)
if err == nil || errors.Is(err, ErrDryRun) {
return
}
_ = cmd.Help()
cmd.PrintErrf("Error: %v\n", err)
}
+10
View File
@@ -331,6 +331,16 @@ func TestRandomStage(t *testing.T) {
}
}
func TestHandleCmdError_DryRun(t *testing.T) {
c := &cobra.Command{Use: "test"}
var stderr bytes.Buffer
c.SetErr(&stderr)
HandleCmdError(ErrDryRun, c)
if stderr.Len() != 0 {
t.Errorf("expected no error output for ErrDryRun, got %q", stderr.String())
}
}
func TestHandleCmdError(t *testing.T) {
tests := []struct {
name string