Skip to content

Commit

Permalink
feat: add global --quiet flag to hide progress indicators
Browse files Browse the repository at this point in the history
Closes #644
  • Loading branch information
phm07 committed Jan 2, 2024
1 parent f636b5e commit 98509d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
11 changes: 11 additions & 0 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions internal/cmd/base/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions internal/state/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 98509d9

Please sign in to comment.