Skip to content

Commit

Permalink
use lifecycle hook
Browse files Browse the repository at this point in the history
  • Loading branch information
savil committed Nov 1, 2024
1 parent 3b0a9c9 commit cd09583
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
20 changes: 15 additions & 5 deletions internal/boxcli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"go.jetpack.io/devbox/internal/devbox"
"go.jetpack.io/devbox/internal/devbox/devopt"
"go.jetpack.io/devbox/internal/redact"
"go.jetpack.io/devbox/internal/ux"
)

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

onStaleState := func() {
ux.FHidableWarning(
ctx,
cmd.ErrOrStderr(),
devbox.StateOutOfDateMessage,
"with --recompute=true",
)
}

envOpts := devopt.EnvOptions{
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
RecomputeEnv: &devopt.RecomputeEnvOpts{
Disabled: !flags.recomputeEnv,
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
Hooks: devopt.EnvLifecycleHooks{
OnStaleStateWithSkipRecompute: onStaleState,
},
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
SkipRecompute: !flags.recomputeEnv,
}
if err := box.RunScript(ctx, envOpts, script, scriptArgs); err != nil {
return redact.Errorf("error running script %q in Devbox: %w", script, err)
Expand Down
20 changes: 15 additions & 5 deletions internal/boxcli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"go.jetpack.io/devbox/internal/devbox"
"go.jetpack.io/devbox/internal/devbox/devopt"
"go.jetpack.io/devbox/internal/envir"
"go.jetpack.io/devbox/internal/ux"
)

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

onStaleState := func() {
ux.FHidableWarning(
ctx,
cmd.ErrOrStderr(),
devbox.StateOutOfDateMessage,
"with --recompute=true",
)
}

return box.Shell(ctx, devopt.EnvOptions{
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
RecomputeEnv: &devopt.RecomputeEnvOpts{
Disabled: !flags.recomputeEnv,
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, "with --recompute=true"),
Hooks: devopt.EnvLifecycleHooks{
OnStaleStateWithSkipRecompute: onStaleState,
},
OmitNixEnv: flags.omitNixEnv,
Pure: flags.pure,
SkipRecompute: !flags.recomputeEnv,
})
}

Expand Down
17 changes: 13 additions & 4 deletions internal/boxcli/shellenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,24 @@ func shellEnvFunc(
}
}

onStaleState := func() {
ux.FHidableWarning(
ctx,
cmd.ErrOrStderr(),
devbox.StateOutOfDateMessage,
box.RefreshAliasOrCommand(),
)
}

envStr, err := box.EnvExports(ctx, devopt.EnvExportsOpts{
EnvOptions: devopt.EnvOptions{
Hooks: devopt.EnvLifecycleHooks{
OnStaleStateWithSkipRecompute: onStaleState,
},
OmitNixEnv: flags.omitNixEnv,
PreservePathStack: flags.preservePathStack,
Pure: flags.pure,
RecomputeEnv: &devopt.RecomputeEnvOpts{
Disabled: !flags.recomputeEnv,
StateOutOfDateMessage: fmt.Sprintf(devbox.StateOutOfDateMessage, box.RefreshAliasOrCommand()),
},
SkipRecompute: !flags.recomputeEnv,
},
NoRefreshAlias: flags.noRefreshAlias,
RunHooks: flags.runInitHook,
Expand Down
10 changes: 4 additions & 6 deletions internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,12 @@ func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
) (map[string]string, error) {
defer debug.FunctionTimer().End()

if envOpts.RecomputeEnv.Disabled {
if envOpts.SkipRecompute {
upToDate, _ := d.lockfile.IsUpToDateAndInstalled(isFishShell())
if !upToDate {
ux.FHidableWarning(
ctx,
d.stderr,
envOpts.RecomputeEnv.StateOutOfDateMessage, //nolint:govet
)
if envOpts.Hooks.OnStaleStateWithSkipRecompute != nil {
envOpts.Hooks.OnStaleStateWithSkipRecompute()
}
}
} else {
// When ensureStateIsUpToDate is called with ensure=true, it always
Expand Down
9 changes: 5 additions & 4 deletions internal/devbox/devopt/devboxopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ type EnvExportsOpts struct {
// like `shellenv`, `shell` and `run`.
// - The struct is designed for the "common case" to be zero-initialized as `EnvOptions{}`.
type EnvOptions struct {
Hooks EnvLifecycleHooks
OmitNixEnv bool
PreservePathStack bool
Pure bool
RecomputeEnv *RecomputeEnvOpts
SkipRecompute bool
}

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

0 comments on commit cd09583

Please sign in to comment.