diff --git a/internal/boxcli/add.go b/internal/boxcli/add.go index e04d403808a..9e6c0cea5d9 100644 --- a/internal/boxcli/add.go +++ b/internal/boxcli/add.go @@ -5,7 +5,6 @@ package boxcli import ( "fmt" - "os" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -24,9 +23,9 @@ func AddCmd() *cobra.Command { flags := addCmdFlags{} command := &cobra.Command{ - Use: "add ...", - Short: "Add a new package to your devbox", - PersistentPreRunE: nix.EnsureInstalled, + Use: "add ...", + 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( @@ -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) } diff --git a/internal/boxcli/cloud.go b/internal/boxcli/cloud.go index ce5152760d9..e4729d63b34 100644 --- a/internal/boxcli/cloud.go +++ b/internal/boxcli/cloud.go @@ -4,8 +4,6 @@ package boxcli import ( - "os" - "github.com/pkg/errors" "github.com/spf13/cobra" "go.jetpack.io/devbox" @@ -36,7 +34,7 @@ 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) }, } @@ -44,10 +42,10 @@ func cloudShellCmd() *cobra.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()) } diff --git a/internal/boxcli/info.go b/internal/boxcli/info.go index c812f1d0002..a389e728260 100644 --- a/internal/boxcli/info.go +++ b/internal/boxcli/info.go @@ -4,8 +4,6 @@ package boxcli import ( - "os" - "github.com/pkg/errors" "github.com/spf13/cobra" "go.jetpack.io/devbox" @@ -21,11 +19,11 @@ type infoCmdFlags struct { func InfoCmd() *cobra.Command { flags := infoCmdFlags{} command := &cobra.Command{ - Use: "info ", - Hidden: !featureflag.PKGConfig.Enabled(), - Short: "Display package info", - Args: cobra.ExactArgs(1), - PersistentPreRunE: nix.EnsureInstalled, + Use: "info ", + 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) }, @@ -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) } diff --git a/internal/boxcli/init.go b/internal/boxcli/init.go index 4b4445c177c..c69b2c9433f 100644 --- a/internal/boxcli/init.go +++ b/internal/boxcli/init.go @@ -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 { @@ -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) } diff --git a/internal/boxcli/plan.go b/internal/boxcli/plan.go index 88bf94698c0..56f321a925f 100644 --- a/internal/boxcli/plan.go +++ b/internal/boxcli/plan.go @@ -5,7 +5,6 @@ package boxcli import ( "encoding/json" - "os" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -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) diff --git a/internal/boxcli/rm.go b/internal/boxcli/rm.go index a668b1b603d..67d26b6f361 100644 --- a/internal/boxcli/rm.go +++ b/internal/boxcli/rm.go @@ -4,8 +4,6 @@ package boxcli import ( - "os" - "github.com/pkg/errors" "github.com/spf13/cobra" "go.jetpack.io/devbox" @@ -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) } diff --git a/internal/boxcli/root.go b/internal/boxcli/root.go index b3659314620..3781b7c368c 100644 --- a/internal/boxcli/root.go +++ b/internal/boxcli/root.go @@ -5,6 +5,7 @@ package boxcli import ( "context" + "io" "os" "strings" @@ -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() }, diff --git a/internal/boxcli/run.go b/internal/boxcli/run.go index 87cdf54f2de..51b0342ac5a 100644 --- a/internal/boxcli/run.go +++ b/internal/boxcli/run.go @@ -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" ) @@ -22,11 +21,11 @@ type runCmdFlags struct { func RunCmd() *cobra.Command { flags := runCmdFlags{} command := &cobra.Command{ - Use: "run