From 82e32591d3931d61e41c0dd006e6829eae63270d Mon Sep 17 00:00:00 2001 From: Savil Srivastava <676452+savil@users.noreply.github.com> Date: Fri, 19 Apr 2024 17:15:27 -0700 Subject: [PATCH 1/2] [shellenv] add --recompute flag with default=true, while keep global shellenv's recompute flag with default=false --- internal/boxcli/global.go | 17 +++++++---------- internal/boxcli/root.go | 4 +--- internal/boxcli/shellenv.go | 14 ++++++++++---- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/internal/boxcli/global.go b/internal/boxcli/global.go index c2c9476d081..efc993c2f97 100644 --- a/internal/boxcli/global.go +++ b/internal/boxcli/global.go @@ -14,12 +14,7 @@ import ( "go.jetpack.io/devbox/internal/ux" ) -type globalShellEnvCmdFlags struct { - recompute bool -} - func globalCmd() *cobra.Command { - globalShellEnvCmdFlags := globalShellEnvCmdFlags{} globalCmd := &cobra.Command{} persistentPreRunE := setGlobalConfigForDelegatedCommands(globalCmd) *globalCmd = cobra.Command{ @@ -33,11 +28,13 @@ func globalCmd() *cobra.Command { PersistentPostRunE: ensureGlobalEnvEnabled, } - shellEnv := shellEnvCmd(&globalShellEnvCmdFlags.recompute) - shellEnv.Flags().BoolVarP( - &globalShellEnvCmdFlags.recompute, "recompute", "r", false, - "Recompute environment if needed", - ) + shellEnv := shellEnvCmd() + // For `devbox shellenv` the default value of recompute is true. + // Change the default value to false for `devbox global shellenv` only. + shellEnv.Flag("recompute").DefValue = "false" + if err := shellEnv.Flag("recompute").Value.Set("false"); err != nil { + panic(errors.WithStack(err)) + } addCommandAndHideConfigFlag(globalCmd, addCmd()) addCommandAndHideConfigFlag(globalCmd, installCmd()) diff --git a/internal/boxcli/root.go b/internal/boxcli/root.go index 077ce97acb9..8a40226cc45 100644 --- a/internal/boxcli/root.go +++ b/internal/boxcli/root.go @@ -11,7 +11,6 @@ import ( "strings" "time" - "github.com/samber/lo" "github.com/spf13/cobra" "go.jetpack.io/devbox/internal/boxcli/featureflag" @@ -77,8 +76,7 @@ func RootCmd() *cobra.Command { command.AddCommand(servicesCmd()) command.AddCommand(setupCmd()) command.AddCommand(shellCmd()) - // True to always recompute environment if needed. - command.AddCommand(shellEnvCmd(lo.ToPtr(true))) + command.AddCommand(shellEnvCmd()) command.AddCommand(updateCmd()) command.AddCommand(versionCmd()) // Preview commands diff --git a/internal/boxcli/shellenv.go b/internal/boxcli/shellenv.go index e59eb43ee48..5a09f43d5c9 100644 --- a/internal/boxcli/shellenv.go +++ b/internal/boxcli/shellenv.go @@ -20,10 +20,11 @@ type shellEnvCmdFlags struct { noRefreshAlias bool preservePathStack bool pure bool + recomputeEnv bool runInitHook bool } -func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command { +func shellEnvCmd() *cobra.Command { flags := shellEnvCmdFlags{} command := &cobra.Command{ Use: "shellenv", @@ -31,7 +32,7 @@ func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command { Args: cobra.ExactArgs(0), PreRunE: ensureNixInstalled, RunE: func(cmd *cobra.Command, args []string) error { - s, err := shellEnvFunc(cmd, flags, *recomputeEnvIfNeeded) + s, err := shellEnvFunc(cmd, flags) if err != nil { return err } @@ -61,6 +62,12 @@ func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command { "Use this flag to disable this behavior.") _ = command.Flags().MarkHidden("no-refresh-alias") + // Note, `devbox global shellenv` will override the default value to be false + command.Flags().BoolVarP( + &flags.recomputeEnv, "recompute", "r", true, + "Recompute environment if needed", + ) + flags.config.register(command) flags.envFlag.register(command) @@ -70,7 +77,6 @@ func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command { func shellEnvFunc( cmd *cobra.Command, flags shellEnvCmdFlags, - recomputeEnvIfNeeded bool, ) (string, error) { env, err := flags.Env(flags.config.path) if err != nil { @@ -95,7 +101,7 @@ func shellEnvFunc( } envStr, err := box.EnvExports(cmd.Context(), devopt.EnvExportsOpts{ - DontRecomputeEnvironment: !recomputeEnvIfNeeded, + DontRecomputeEnvironment: !flags.recomputeEnv, NoRefreshAlias: flags.noRefreshAlias, RunHooks: flags.runInitHook, }) From 91722d8dc0227e89b7bcc4d865777c02feaaae62 Mon Sep 17 00:00:00 2001 From: Savil Srivastava <676452+savil@users.noreply.github.com> Date: Thu, 25 Apr 2024 07:37:09 -0700 Subject: [PATCH 2/2] fix comment --- internal/boxcli/global.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/boxcli/global.go b/internal/boxcli/global.go index efc993c2f97..f0c0998ffc8 100644 --- a/internal/boxcli/global.go +++ b/internal/boxcli/global.go @@ -31,8 +31,12 @@ func globalCmd() *cobra.Command { shellEnv := shellEnvCmd() // For `devbox shellenv` the default value of recompute is true. // Change the default value to false for `devbox global shellenv` only. - shellEnv.Flag("recompute").DefValue = "false" + shellEnv.Flag("recompute").DefValue = "false" // Needed for help text if err := shellEnv.Flag("recompute").Value.Set("false"); err != nil { + // This will never panic because internally it just does + // `strconv.ParseBool("false")` which is always valid. + // If this were to change, we'll immediately detect this during development + // since this code always runs on any devbox command (and will fix it). panic(errors.WithStack(err)) }