Skip to content

Commit

Permalink
[CLI] Quiet mode for all commands (#431)
Browse files Browse the repository at this point in the history
## 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 'PreRunE` in subcommands because it was
causing the subcommands ignore the root command's `PersistentPreRunE`
which has the quiet flag.

## How was it tested?
- Compile
- run devbox commands with -q and without -q
- confirm -q silences outputs
  • Loading branch information
mohsenari authored Jan 6, 2023
1 parent 0dd27e1 commit d3494d6
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 108 deletions.
11 changes: 5 additions & 6 deletions internal/boxcli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package boxcli

import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -24,9 +23,9 @@ func AddCmd() *cobra.Command {
flags := addCmdFlags{}

command := &cobra.Command{
Use: "add <pkg>...",
Short: "Add a new package to your devbox",
PersistentPreRunE: nix.EnsureInstalled,
Use: "add <pkg>...",
Short: "Add a new package to your devbox",
PreRunE: nix.EnsureInstalled,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
fmt.Fprintf(
Expand All @@ -49,8 +48,8 @@ func AddCmd() *cobra.Command {
return command
}

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())
if err != nil {
return errors.WithStack(err)
}
Expand Down
10 changes: 4 additions & 6 deletions internal/boxcli/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package boxcli

import (
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
Expand Down Expand Up @@ -36,18 +34,18 @@ func cloudShellCmd() *cobra.Command {
Use: "shell",
Short: "Shell into a cloud environment that matches your local devbox environment",
RunE: func(cmd *cobra.Command, args []string) error {
return runCloudShellCmd(&flags)
return runCloudShellCmd(cmd, &flags)
},
}

flags.config.register(command)
return command
}

func runCloudShellCmd(flags *cloudShellCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
func runCloudShellCmd(cmd *cobra.Command, flags *cloudShellCmdFlags) error {
box, err := devbox.Open(flags.config.path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
return cloud.Shell(box.ConfigDir())
return cloud.Shell(box.ConfigDir(), cmd.ErrOrStderr())
}
16 changes: 7 additions & 9 deletions internal/boxcli/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package boxcli

import (
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
Expand All @@ -21,11 +19,11 @@ type infoCmdFlags struct {
func InfoCmd() *cobra.Command {
flags := infoCmdFlags{}
command := &cobra.Command{
Use: "info <pkg>",
Hidden: !featureflag.PKGConfig.Enabled(),
Short: "Display package info",
Args: cobra.ExactArgs(1),
PersistentPreRunE: nix.EnsureInstalled,
Use: "info <pkg>",
Hidden: !featureflag.PKGConfig.Enabled(),
Short: "Display package info",
Args: cobra.ExactArgs(1),
PreRunE: nix.EnsureInstalled,
RunE: func(cmd *cobra.Command, args []string) error {
return infoCmdFunc(cmd, args[0], flags)
},
Expand All @@ -36,8 +34,8 @@ func InfoCmd() *cobra.Command {
return command
}

func infoCmdFunc(_ *cobra.Command, pkg string, flags infoCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
func infoCmdFunc(cmd *cobra.Command, pkg string, flags infoCmdFlags) error {
box, err := devbox.Open(flags.config.path, cmd.OutOrStdout())
if err != nil {
return errors.WithStack(err)
}
Expand Down
4 changes: 1 addition & 3 deletions internal/boxcli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
"go.jetpack.io/devbox/internal/boxcli/writer"
)

func InitCmd() *cobra.Command {
Expand All @@ -27,8 +26,7 @@ func InitCmd() *cobra.Command {
func runInitCmd(cmd *cobra.Command, args []string) error {
path := pathArg(args)

w := writer.New(cmd)
_, err := devbox.InitConfig(path, w)
_, err := devbox.InitConfig(path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
Expand Down
7 changes: 3 additions & 4 deletions internal/boxcli/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package boxcli

import (
"encoding/json"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,18 +32,18 @@ func PlanCmd() *cobra.Command {
return command
}

func runPlanCmd(_ *cobra.Command, args []string, flags planCmdFlags) error {
func runPlanCmd(cmd *cobra.Command, args []string, flags planCmdFlags) error {
path, err := configPathFromUser(args, &flags.config)
if err != nil {
return err
}

// Check the directory exists.
box, err := devbox.Open(path, os.Stdout)
box, err := devbox.Open(path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
enc := json.NewEncoder(os.Stdout)
enc := json.NewEncoder(cmd.ErrOrStderr())
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)

Expand Down
6 changes: 2 additions & 4 deletions internal/boxcli/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package boxcli

import (
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
Expand All @@ -30,8 +28,8 @@ func RemoveCmd() *cobra.Command {
return command
}

func runRemoveCmd(_ *cobra.Command, args []string, flags removeCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
func runRemoveCmd(cmd *cobra.Command, args []string, flags removeCmdFlags) error {
box, err := devbox.Open(flags.config.path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
Expand Down
6 changes: 6 additions & 0 deletions internal/boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package boxcli

import (
"context"
"io"
"os"
"strings"

Expand All @@ -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)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
Expand Down
14 changes: 6 additions & 8 deletions internal/boxcli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
"go.jetpack.io/devbox/internal/boxcli/writer"
"go.jetpack.io/devbox/internal/nix"
"golang.org/x/exp/slices"
)
Expand All @@ -22,11 +21,11 @@ type runCmdFlags struct {
func RunCmd() *cobra.Command {
flags := runCmdFlags{}
command := &cobra.Command{
Use: "run <script>",
Short: "Starts a new devbox shell and runs the target script",
Long: "Starts a new interactive shell and runs your target script in it. The shell will exit once your target script is completed or when it is terminated via CTRL-C. Scripts can be defined in your `devbox.json`",
Args: cobra.MaximumNArgs(1),
PersistentPreRunE: nix.EnsureInstalled,
Use: "run <script>",
Short: "Starts a new devbox shell and runs the target script",
Long: "Starts a new interactive shell and runs your target script in it. The shell will exit once your target script is completed or when it is terminated via CTRL-C. Scripts can be defined in your `devbox.json`",
Args: cobra.MaximumNArgs(1),
PreRunE: nix.EnsureInstalled,
RunE: func(cmd *cobra.Command, args []string) error {
return runScriptCmd(cmd, args, flags)
},
Expand All @@ -39,14 +38,13 @@ func RunCmd() *cobra.Command {

func runScriptCmd(cmd *cobra.Command, args []string, flags runCmdFlags) error {

w := writer.New(cmd)
path, script, err := parseScriptArgs(args, flags)
if err != nil {
return err
}

// Check the directory exists.
box, err := devbox.Open(path, w)
box, err := devbox.Open(path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
Expand Down
8 changes: 3 additions & 5 deletions internal/boxcli/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
package boxcli

import (
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
Expand Down Expand Up @@ -66,7 +64,7 @@ func ServicesCmd() *cobra.Command {
}

func listServices(cmd *cobra.Command, flags servicesCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
box, err := devbox.Open(flags.config.path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -81,7 +79,7 @@ func listServices(cmd *cobra.Command, flags servicesCmdFlags) error {
}

func startServices(cmd *cobra.Command, services []string, flags servicesCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
box, err := devbox.Open(flags.config.path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -99,7 +97,7 @@ func startServices(cmd *cobra.Command, services []string, flags servicesCmdFlags
}

func stopServices(cmd *cobra.Command, services []string, flags servicesCmdFlags) error {
box, err := devbox.Open(flags.config.path, os.Stdout)
box, err := devbox.Open(flags.config.path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/boxcli/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func SetupCmd() *cobra.Command {
func runInstallNixCmd(cmd *cobra.Command) error {
if nix.NixBinaryInstalled() {
color.New(color.FgYellow).Fprint(
cmd.OutOrStdout(),
cmd.ErrOrStderr(),
"Nix is already installed. If this is incorrect please remove the "+
"nix-shell binary from your path.\n",
)
return nil
}
return nix.Install()
return nix.Install(cmd.ErrOrStderr())
}
9 changes: 3 additions & 6 deletions internal/boxcli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/devbox"
"go.jetpack.io/devbox/internal/boxcli/writer"
"go.jetpack.io/devbox/internal/nix"
)

Expand All @@ -29,16 +28,15 @@ func ShellCmd() *cobra.Command {
"If invoked with a `cmd`, devbox will run the command in a shell and then exit.\n" +
"In both cases, the shell will be started using the devbox.json found in the --config flag directory. " +
"If --config isn't set, then devbox recursively searches the current directory and its parents.",
Args: validateShellArgs,
PersistentPreRunE: nix.EnsureInstalled,
Args: validateShellArgs,
PreRunE: nix.EnsureInstalled,
RunE: func(cmd *cobra.Command, args []string) error {
return runShellCmd(cmd, args, flags)
},
}

command.Flags().BoolVar(
&flags.PrintEnv, "print-env", false, "Print script to setup shell environment")

flags.config.register(command)
return command
}
Expand All @@ -48,9 +46,8 @@ func runShellCmd(cmd *cobra.Command, args []string, flags shellCmdFlags) error {
if err != nil {
return err
}
w := writer.New(cmd)
// Check the directory exists.
box, err := devbox.Open(path, w)
box, err := devbox.Open(path, cmd.ErrOrStderr())
if err != nil {
return errors.WithStack(err)
}
Expand Down
15 changes: 8 additions & 7 deletions internal/boxcli/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ func VersionCmd() *cobra.Command {
return command
}

func versionCmdFunc(_ *cobra.Command, _ []string, flags versionFlags) error {
func versionCmdFunc(cmd *cobra.Command, _ []string, flags versionFlags) error {
w := cmd.OutOrStdout()
v := getVersionInfo()
if flags.verbose {
fmt.Printf("Version: %v\n", v.Version)
fmt.Printf("Platform: %v\n", v.Platform)
fmt.Printf("Commit: %v\n", v.Commit)
fmt.Printf("Commit Time: %v\n", v.CommitDate)
fmt.Printf("Go Version: %v\n", v.GoVersion)
fmt.Fprintf(w, "Version: %v\n", v.Version)
fmt.Fprintf(w, "Platform: %v\n", v.Platform)
fmt.Fprintf(w, "Commit: %v\n", v.Commit)
fmt.Fprintf(w, "Commit Time: %v\n", v.CommitDate)
fmt.Fprintf(w, "Go Version: %v\n", v.GoVersion)
} else {
fmt.Printf("%v\n", v.Version)
fmt.Fprintf(w, "%v\n", v.Version)
}
return nil
}
Expand Down
36 changes: 0 additions & 36 deletions internal/boxcli/writer/writer.go

This file was deleted.

Loading

0 comments on commit d3494d6

Please sign in to comment.