Skip to content

Commit 70aed6b

Browse files
committed
[shellenv] add --recompute flag with default=true, while keep global shellenv's recompute flag with default=false
1 parent 935f8ee commit 70aed6b

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

internal/boxcli/global.go

Lines changed: 7 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,13 @@ 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"
35+
if err := shellEnv.Flag("recompute").Value.Set("false"); err != nil {
36+
panic(errors.WithStack(err))
37+
}
4138

4239
addCommandAndHideConfigFlag(globalCmd, addCmd())
4340
addCommandAndHideConfigFlag(globalCmd, installCmd())

internal/boxcli/root.go

Lines changed: 1 addition & 2 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"
@@ -78,7 +77,7 @@ func RootCmd() *cobra.Command {
7877
command.AddCommand(setupCmd())
7978
command.AddCommand(shellCmd())
8079
// True to always recompute environment if needed.
81-
command.AddCommand(shellEnvCmd(lo.ToPtr(true)))
80+
command.AddCommand(shellEnvCmd())
8281
command.AddCommand(updateCmd())
8382
command.AddCommand(versionCmd())
8483
// 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+
recompute 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.recompute, "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.recompute,
99105
NoRefreshAlias: flags.noRefreshAlias,
100106
RunHooks: flags.runInitHook,
101107
})

0 commit comments

Comments
 (0)