Skip to content

Commit 7034939

Browse files
authored
[shellenv] add --recompute flag with default=true, while keep global shellenv's recompute flag with default=false (#2013)
## Summary Credit to @dax and #1963 for pointing out the need for this, and pioneering this solution. This PR merely nudges the code to be a bit neater. ## How was it tested? ``` % git diff diff --git a/internal/boxcli/shellenv.go b/internal/boxcli/shellenv.go index 991c2be..8076e125 100644 --- a/internal/boxcli/shellenv.go +++ b/internal/boxcli/shellenv.go @@ -78,6 +78,9 @@ func shellEnvFunc( cmd *cobra.Command, flags shellEnvCmdFlags, ) (string, error) { + fmt.Printf("shellenv --recompute flag is %v\n", flags.recompute) + return "", nil + env, err := flags.Env(flags.config.path) if err != nil { return "", err ``` compiled via `devbox run build`, and then: ``` % devbox shellenv shellenv --recompute flag is true hash -r % devbox global shellenv shellenv --recompute flag is false hash -r ```
1 parent 309abe9 commit 7034939

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

internal/boxcli/global.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ import (
1414
"go.jetpack.io/devbox/internal/ux"
1515
)
1616

17-
type globalShellEnvCmdFlags struct {
18-
recompute bool
19-
}
20-
2117
func globalCmd() *cobra.Command {
22-
globalShellEnvCmdFlags := globalShellEnvCmdFlags{}
2318
globalCmd := &cobra.Command{}
2419
persistentPreRunE := setGlobalConfigForDelegatedCommands(globalCmd)
2520
*globalCmd = cobra.Command{
@@ -33,11 +28,17 @@ func globalCmd() *cobra.Command {
3328
PersistentPostRunE: ensureGlobalEnvEnabled,
3429
}
3530

36-
shellEnv := shellEnvCmd(&globalShellEnvCmdFlags.recompute)
37-
shellEnv.Flags().BoolVarP(
38-
&globalShellEnvCmdFlags.recompute, "recompute", "r", false,
39-
"Recompute environment if needed",
40-
)
31+
shellEnv := shellEnvCmd()
32+
// For `devbox shellenv` the default value of recompute is true.
33+
// Change the default value to false for `devbox global shellenv` only.
34+
shellEnv.Flag("recompute").DefValue = "false" // Needed for help text
35+
if err := shellEnv.Flag("recompute").Value.Set("false"); err != nil {
36+
// This will never panic because internally it just does
37+
// `strconv.ParseBool("false")` which is always valid.
38+
// If this were to change, we'll immediately detect this during development
39+
// since this code always runs on any devbox command (and will fix it).
40+
panic(errors.WithStack(err))
41+
}
4142

4243
addCommandAndHideConfigFlag(globalCmd, addCmd())
4344
addCommandAndHideConfigFlag(globalCmd, installCmd())

internal/boxcli/root.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212
"time"
1313

14-
"github.com/samber/lo"
1514
"github.com/spf13/cobra"
1615

1716
"go.jetpack.io/devbox/internal/boxcli/featureflag"
@@ -77,8 +76,7 @@ func RootCmd() *cobra.Command {
7776
command.AddCommand(servicesCmd())
7877
command.AddCommand(setupCmd())
7978
command.AddCommand(shellCmd())
80-
// True to always recompute environment if needed.
81-
command.AddCommand(shellEnvCmd(lo.ToPtr(true)))
79+
command.AddCommand(shellEnvCmd())
8280
command.AddCommand(updateCmd())
8381
command.AddCommand(versionCmd())
8482
// Preview commands

internal/boxcli/shellenv.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ type shellEnvCmdFlags struct {
2020
noRefreshAlias bool
2121
preservePathStack bool
2222
pure bool
23+
recomputeEnv bool
2324
runInitHook bool
2425
}
2526

26-
func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command {
27+
func shellEnvCmd() *cobra.Command {
2728
flags := shellEnvCmdFlags{}
2829
command := &cobra.Command{
2930
Use: "shellenv",
3031
Short: "Print shell commands that add Devbox packages to your PATH",
3132
Args: cobra.ExactArgs(0),
3233
PreRunE: ensureNixInstalled,
3334
RunE: func(cmd *cobra.Command, args []string) error {
34-
s, err := shellEnvFunc(cmd, flags, *recomputeEnvIfNeeded)
35+
s, err := shellEnvFunc(cmd, flags)
3536
if err != nil {
3637
return err
3738
}
@@ -61,6 +62,12 @@ func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command {
6162
"Use this flag to disable this behavior.")
6263
_ = command.Flags().MarkHidden("no-refresh-alias")
6364

65+
// Note, `devbox global shellenv` will override the default value to be false
66+
command.Flags().BoolVarP(
67+
&flags.recomputeEnv, "recompute", "r", true,
68+
"Recompute environment if needed",
69+
)
70+
6471
flags.config.register(command)
6572
flags.envFlag.register(command)
6673

@@ -70,7 +77,6 @@ func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command {
7077
func shellEnvFunc(
7178
cmd *cobra.Command,
7279
flags shellEnvCmdFlags,
73-
recomputeEnvIfNeeded bool,
7480
) (string, error) {
7581
env, err := flags.Env(flags.config.path)
7682
if err != nil {
@@ -95,7 +101,7 @@ func shellEnvFunc(
95101
}
96102

97103
envStr, err := box.EnvExports(cmd.Context(), devopt.EnvExportsOpts{
98-
DontRecomputeEnvironment: !recomputeEnvIfNeeded,
104+
DontRecomputeEnvironment: !flags.recomputeEnv,
99105
NoRefreshAlias: flags.noRefreshAlias,
100106
RunHooks: flags.runInitHook,
101107
})

0 commit comments

Comments
 (0)