Skip to content

[shellenv] add --recompute flag with default=true, while keep global shellenv's recompute flag with default=false #2013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions internal/boxcli/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -33,11 +28,17 @@ 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" // 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))
}

addCommandAndHideConfigFlag(globalCmd, addCmd())
addCommandAndHideConfigFlag(globalCmd, installCmd())
Expand Down
4 changes: 1 addition & 3 deletions internal/boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

"github.com/samber/lo"
"github.com/spf13/cobra"

"go.jetpack.io/devbox/internal/boxcli/featureflag"
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions internal/boxcli/shellenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ 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",
Short: "Print shell commands that add Devbox packages to your PATH",
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
}
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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,
})
Expand Down