Skip to content

feat: Add recompute flag to shellenv command #1963

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

Closed
Closed
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
13 changes: 1 addition & 12 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,12 +28,6 @@ func globalCmd() *cobra.Command {
PersistentPostRunE: ensureGlobalEnvEnabled,
}

shellEnv := shellEnvCmd(&globalShellEnvCmdFlags.recompute)
shellEnv.Flags().BoolVarP(
&globalShellEnvCmdFlags.recompute, "recompute", "r", false,
"Recompute environment if needed",
)

addCommandAndHideConfigFlag(globalCmd, addCmd())
addCommandAndHideConfigFlag(globalCmd, installCmd())
addCommandAndHideConfigFlag(globalCmd, pathCmd())
Expand All @@ -47,7 +36,7 @@ func globalCmd() *cobra.Command {
addCommandAndHideConfigFlag(globalCmd, removeCmd())
addCommandAndHideConfigFlag(globalCmd, runCmd())
addCommandAndHideConfigFlag(globalCmd, servicesCmd(persistentPreRunE))
addCommandAndHideConfigFlag(globalCmd, shellEnv)
addCommandAndHideConfigFlag(globalCmd, shellEnvCmd())
addCommandAndHideConfigFlag(globalCmd, updateCmd())

// Create list for non-global? Mike: I want it :)
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 @@ -76,8 +75,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
13 changes: 9 additions & 4 deletions internal/boxcli/shellenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ type shellEnvCmdFlags struct {
preservePathStack bool
pure bool
runInitHook bool
recompute 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,11 @@ func shellEnvCmd(recomputeEnvIfNeeded *bool) *cobra.Command {
"Use this flag to disable this behavior.")
_ = command.Flags().MarkHidden("no-refresh-alias")

command.Flags().BoolVarP(
&flags.recompute, "recompute", "r", true,
"Recompute environment if needed",
)

flags.config.register(command)
flags.envFlag.register(command)

Expand All @@ -70,7 +76,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 +100,7 @@ func shellEnvFunc(
}

envStr, err := box.EnvExports(cmd.Context(), devopt.EnvExportsOpts{
DontRecomputeEnvironment: !recomputeEnvIfNeeded,
DontRecomputeEnvironment: !flags.recompute,
NoRefreshAlias: flags.noRefreshAlias,
RunHooks: flags.runInitHook,
})
Expand Down