diff --git a/cmd/context.go b/cmd/context.go new file mode 100644 index 0000000..7fd164c --- /dev/null +++ b/cmd/context.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "os" + + "github.com/Snider/Borg/pkg/ui" + "github.com/spf13/cobra" +) + +// ProgressFromCmd returns a Progress based on --quiet flag and TTY detection. +func ProgressFromCmd(cmd *cobra.Command) ui.Progress { + quiet, _ := cmd.Flags().GetBool("quiet") + if quiet { + return ui.NewQuietProgress(os.Stderr) + } + return ui.DefaultProgress() +} diff --git a/cmd/context_test.go b/cmd/context_test.go new file mode 100644 index 0000000..0e54903 --- /dev/null +++ b/cmd/context_test.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "testing" + + "github.com/spf13/cobra" +) + +func TestProgressFromCmd_Good(t *testing.T) { + cmd := &cobra.Command{} + cmd.PersistentFlags().BoolP("quiet", "q", false, "") + + p := ProgressFromCmd(cmd) + if p == nil { + t.Fatal("expected non-nil Progress") + } +} + +func TestProgressFromCmd_Quiet_Good(t *testing.T) { + cmd := &cobra.Command{} + cmd.PersistentFlags().BoolP("quiet", "q", true, "") + _ = cmd.PersistentFlags().Set("quiet", "true") + + p := ProgressFromCmd(cmd) + if p == nil { + t.Fatal("expected non-nil Progress") + } +} diff --git a/cmd/root.go b/cmd/root.go index 9cadb27..dd41d6f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,6 +16,7 @@ packaging their contents into a single file, and managing the data within.`, } rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose logging") + rootCmd.PersistentFlags().BoolP("quiet", "q", false, "Suppress non-error output") return rootCmd }