Skip to content

Commit cd09583

Browse files
committed
use lifecycle hook
1 parent 3b0a9c9 commit cd09583

File tree

5 files changed

+52
-24
lines changed

5 files changed

+52
-24
lines changed

internal/boxcli/run.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"go.jetpack.io/devbox/internal/devbox"
1818
"go.jetpack.io/devbox/internal/devbox/devopt"
1919
"go.jetpack.io/devbox/internal/redact"
20+
"go.jetpack.io/devbox/internal/ux"
2021
)
2122

2223
type runCmdFlags struct {
@@ -122,13 +123,22 @@ func runScriptCmd(cmd *cobra.Command, args []string, flags runCmdFlags) error {
122123
return redact.Errorf("error reading devbox.json: %w", err)
123124
}
124125

126+
onStaleState := func() {
127+
ux.FHidableWarning(
128+
ctx,
129+
cmd.ErrOrStderr(),
130+
devbox.StateOutOfDateMessage,
131+
"with --recompute=true",
132+
)
133+
}
134+
125135
envOpts := devopt.EnvOptions{
126-
OmitNixEnv: flags.omitNixEnv,
127-
Pure: flags.pure,
128-
RecomputeEnv: &devopt.RecomputeEnvOpts{
129-
Disabled: !flags.recomputeEnv,
130-
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
136+
Hooks: devopt.EnvLifecycleHooks{
137+
OnStaleStateWithSkipRecompute: onStaleState,
131138
},
139+
OmitNixEnv: flags.omitNixEnv,
140+
Pure: flags.pure,
141+
SkipRecompute: !flags.recomputeEnv,
132142
}
133143
if err := box.RunScript(ctx, envOpts, script, scriptArgs); err != nil {
134144
return redact.Errorf("error running script %q in Devbox: %w", script, err)

internal/boxcli/shell.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"go.jetpack.io/devbox/internal/devbox"
1414
"go.jetpack.io/devbox/internal/devbox/devopt"
1515
"go.jetpack.io/devbox/internal/envir"
16+
"go.jetpack.io/devbox/internal/ux"
1617
)
1718

1819
type shellCmdFlags struct {
@@ -95,13 +96,22 @@ func runShellCmd(cmd *cobra.Command, flags shellCmdFlags) error {
9596
return shellInceptionErrorMsg("devbox shell")
9697
}
9798

99+
onStaleState := func() {
100+
ux.FHidableWarning(
101+
ctx,
102+
cmd.ErrOrStderr(),
103+
devbox.StateOutOfDateMessage,
104+
"with --recompute=true",
105+
)
106+
}
107+
98108
return box.Shell(ctx, devopt.EnvOptions{
99-
OmitNixEnv: flags.omitNixEnv,
100-
Pure: flags.pure,
101-
RecomputeEnv: &devopt.RecomputeEnvOpts{
102-
Disabled: !flags.recomputeEnv,
103-
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
109+
Hooks: devopt.EnvLifecycleHooks{
110+
OnStaleStateWithSkipRecompute: onStaleState,
104111
},
112+
OmitNixEnv: flags.omitNixEnv,
113+
Pure: flags.pure,
114+
SkipRecompute: !flags.recomputeEnv,
105115
})
106116
}
107117

internal/boxcli/shellenv.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,24 @@ func shellEnvFunc(
115115
}
116116
}
117117

118+
onStaleState := func() {
119+
ux.FHidableWarning(
120+
ctx,
121+
cmd.ErrOrStderr(),
122+
devbox.StateOutOfDateMessage,
123+
box.RefreshAliasOrCommand(),
124+
)
125+
}
126+
118127
envStr, err := box.EnvExports(ctx, devopt.EnvExportsOpts{
119128
EnvOptions: devopt.EnvOptions{
129+
Hooks: devopt.EnvLifecycleHooks{
130+
OnStaleStateWithSkipRecompute: onStaleState,
131+
},
120132
OmitNixEnv: flags.omitNixEnv,
121133
PreservePathStack: flags.preservePathStack,
122134
Pure: flags.pure,
123-
RecomputeEnv: &devopt.RecomputeEnvOpts{
124-
Disabled: !flags.recomputeEnv,
125-
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, box.RefreshAliasOrCommand()),
126-
},
135+
SkipRecompute: !flags.recomputeEnv,
127136
},
128137
NoRefreshAlias: flags.noRefreshAlias,
129138
RunHooks: flags.runInitHook,

internal/devbox/devbox.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,12 @@ func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
804804
) (map[string]string, error) {
805805
defer debug.FunctionTimer().End()
806806

807-
if envOpts.RecomputeEnv.Disabled {
807+
if envOpts.SkipRecompute {
808808
upToDate, _ := d.lockfile.IsUpToDateAndInstalled(isFishShell())
809809
if !upToDate {
810-
ux.FHidableWarning(
811-
ctx,
812-
d.stderr,
813-
envOpts.RecomputeEnv.StateOutOfDateMessage, //nolint:govet
814-
)
810+
if envOpts.Hooks.OnStaleStateWithSkipRecompute != nil {
811+
envOpts.Hooks.OnStaleStateWithSkipRecompute()
812+
}
815813
}
816814
} else {
817815
// When ensureStateIsUpToDate is called with ensure=true, it always

internal/devbox/devopt/devboxopts.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@ type EnvExportsOpts struct {
7272
// like `shellenv`, `shell` and `run`.
7373
// - The struct is designed for the "common case" to be zero-initialized as `EnvOptions{}`.
7474
type EnvOptions struct {
75+
Hooks EnvLifecycleHooks
7576
OmitNixEnv bool
7677
PreservePathStack bool
7778
Pure bool
78-
RecomputeEnv *RecomputeEnvOpts
79+
SkipRecompute bool
7980
}
8081

81-
type RecomputeEnvOpts struct {
82-
Disabled bool // Disabled instead of Enabled, because zero-value is false
83-
StateOutOfDateMessage string
82+
type EnvLifecycleHooks struct {
83+
// OnStaleStateWithSkipRecompute is called when the Devbox state is out of date, AND it is not being recomputed.
84+
OnStaleStateWithSkipRecompute func()
8485
}

0 commit comments

Comments
 (0)