Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLI] Quiet mode for all commands #431

Merged
merged 8 commits into from
Jan 6, 2023
Merged

Conversation

mohsenari
Copy link
Collaborator

Summary

  • Did a sweep for fmt.print and changed all instances of to to fmt.Fprint except in debug.
  • Removed custom IO writer and used cobra command's cmd.SetErr(io.Discard) at root level.
  • Changed all os.stdout writers passed to commands to cmd.ErrOrStderr() due to discussion around writing outputs to stderr rather than stdout.
  • Changed PersistentPreRunE to 'PreRunEin subcommands because it was causing the subcommands ignore the root command'sPersistentPreRunE` which has the quiet flag.

How was it tested?

  • Compile
  • run devbox commands with -q and without -q
  • confirm -q silences outputs

Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good, but unless I'm misunderstanding something everything should be stdout instead of stderr right?

Side note, we are currently sending errors to stdout in DebugMiddleware. That's another separate fix but I assume those should go to stderr instead and IMO those should not listen to quiet flag?

func addCmdFunc(_ *cobra.Command, args []string, flags addCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
func addCmdFunc(cmd *cobra.Command, args []string, flags addCmdFlags) error {
box, err := devbox.Open(flags.config.path, cmd.ErrOrStderr())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be OutOrStdout no?

@@ -26,6 +27,11 @@ func RootCmd() *cobra.Command {
command := &cobra.Command{
Use: "devbox",
Short: "Instant, easy, predictable development environments",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if flags.quiet {
cmd.SetErr(io.Discard)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be SetOut ?

PersistentPreRunE: nix.EnsureInstalled,
Use: "add <pkg>...",
Short: "Add a new package to your devbox",
PreRunE: nix.EnsureInstalled,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why this change? can we not have 2 PersistentPreRunE ? (one in root and another in the subcommands?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A found it spf13/cobra#252

looks like others were confused by this as well. Good find!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was causing the subcommands ignore the persistentPreRun at root level which contains -q flag. This was the bug I couldn't solve yesterday but eventually did 😄

Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found Slack conversation where stderr vs out was discussed. IMakes sense

Also found this https://unix.stackexchange.com/questions/331611/do-progress-reports-logging-information-belong-on-stderr-or-stdout

@mohsenari mohsenari merged commit d3494d6 into main Jan 6, 2023
@mohsenari mohsenari deleted the mohsen--quiet-mode-followup branch January 6, 2023 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants