From 98509d9590fe779b42e81497e36a39a932bef1fe Mon Sep 17 00:00:00 2001 From: pauhull Date: Tue, 2 Jan 2024 13:57:07 +0100 Subject: [PATCH] feat: add global --quiet flag to hide progress indicators Closes #644 --- internal/cli/root.go | 11 +++++++++++ internal/cmd/base/create.go | 2 -- internal/state/helpers.go | 8 ++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/cli/root.go b/internal/cli/root.go index b3ca29c3..7c4ce494 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -63,6 +63,17 @@ func NewRootCommand(state *state.State, client hcapi2.Client) *cobra.Command { primaryip.NewCommand(state, client), ) cmd.PersistentFlags().Duration("poll-interval", 500*time.Millisecond, "Interval at which to poll information, for example action progress") + cmd.PersistentFlags().Bool("quiet", false, "Only print error messages") cmd.SetOut(os.Stdout) + cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + if quiet, _ := cmd.Flags().GetBool("quiet"); quiet { + f, err := os.Open(os.DevNull) + if err != nil { + return err + } + cmd.SetOut(f) + } + return nil + } return cmd } diff --git a/internal/cmd/base/create.go b/internal/cmd/base/create.go index cbd75019..8690da90 100644 --- a/internal/cmd/base/create.go +++ b/internal/cmd/base/create.go @@ -48,8 +48,6 @@ func (cc *CreateCmd) CobraCommand( isSchema := outputFlags.IsSet("json") || outputFlags.IsSet("yaml") if isSchema { cmd.SetOut(os.Stderr) - } else { - cmd.SetOut(os.Stdout) } resource, schema, err := cc.Run(ctx, client, actionWaiter, cmd, args) diff --git a/internal/state/helpers.go b/internal/state/helpers.go index a08eedfb..73f5b405 100644 --- a/internal/state/helpers.go +++ b/internal/state/helpers.go @@ -66,6 +66,10 @@ func (c *State) ActionProgress(cmd *cobra.Command, ctx context.Context, action * func (c *State) ActionsProgresses(cmd *cobra.Command, ctx context.Context, actions []*hcloud.Action) error { progressCh, errCh := c.Client().Action.WatchOverallProgress(ctx, actions) + if quiet, _ := cmd.Flags().GetBool("quiet"); quiet { + return <-errCh + } + if StdoutIsTerminal() { progress := pb.New(100) progress.SetMaxWidth(50) // width of progress bar is too large by default @@ -131,6 +135,10 @@ func DisplayProgressCircle(cmd *cobra.Command, errCh <-chan error, waitingFor st ellipsis = " ... " ) + if quiet, _ := cmd.Flags().GetBool("quiet"); quiet { + return <-errCh + } + if StdoutIsTerminal() { _, _ = fmt.Fprintln(os.Stderr, waitingFor)