Skip to content

Commit 39b7f0e

Browse files
authored
[global] Fix global services (#1484)
## Summary cobra doesn't chain `PersistentPreRunE`. So if a parent declares one and a child declares it as well, the parent function is ignored. `global` uses `PersistentPreRunE` to set the global project directory. The `services` command was running it to ensure nix is installed. The child replaced the parent, so we were never setting th The fix here is a bit ugly but it solves them problem. There's a UX issue I rediscovered while testing this. When adding global packages the environment is not automatically refreshed. This means services wont work until a new shell is opened or if the user does `eval "$(devbox global shellenv)"` manually. ## How was it tested? ``` devbox global add nginx eval "$(devbox global shellenv)" devbox global services up ```
1 parent 067eb92 commit 39b7f0e

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

internal/boxcli/global.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ import (
1515
)
1616

1717
func globalCmd() *cobra.Command {
18-
1918
globalCmd := &cobra.Command{}
20-
19+
persistentPreRunE := setGlobalConfigForDelegatedCommands(globalCmd)
2120
*globalCmd = cobra.Command{
22-
Use: "global",
23-
Short: "Manage global devbox packages",
24-
PersistentPreRunE: setGlobalConfigForDelegatedCommands(globalCmd),
21+
Use: "global",
22+
Short: "Manage global devbox packages",
23+
// PersistentPreRunE is inherited only if children do not implement it
24+
// (i.e. it's not chained). So this is fragile. Ideally we stop
25+
// using PersistentPreRunE. For now a hack is to pass it down to commands
26+
// that declare their own.
27+
PersistentPreRunE: persistentPreRunE,
2528
PersistentPostRunE: ensureGlobalEnvEnabled,
2629
}
2730

@@ -32,7 +35,7 @@ func globalCmd() *cobra.Command {
3235
addCommandAndHideConfigFlag(globalCmd, pushCmd())
3336
addCommandAndHideConfigFlag(globalCmd, removeCmd())
3437
addCommandAndHideConfigFlag(globalCmd, runCmd())
35-
addCommandAndHideConfigFlag(globalCmd, servicesCmd())
38+
addCommandAndHideConfigFlag(globalCmd, servicesCmd(persistentPreRunE))
3639
addCommandAndHideConfigFlag(globalCmd, shellEnvCmd())
3740
addCommandAndHideConfigFlag(globalCmd, updateCmd())
3841

internal/boxcli/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"go.jetpack.io/devbox/internal/vercheck"
2222
)
2323

24+
type cobraFunc func(cmd *cobra.Command, args []string) error
25+
2426
var (
2527
debugMiddleware = &midcobra.DebugMiddleware{}
2628
traceMiddleware = &midcobra.TraceMiddleware{}

internal/boxcli/services.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ func (flags *serviceStopFlags) register(cmd *cobra.Command) {
4141
&flags.allProjects, "all-projects", false, "Stop all running services across all your projects.\nThis flag cannot be used simultaneously with the [services] argument")
4242
}
4343

44-
func servicesCmd() *cobra.Command {
44+
func servicesCmd(persistentPreRunE ...cobraFunc) *cobra.Command {
4545
flags := servicesCmdFlags{}
4646
serviceUpFlags := serviceUpFlags{}
4747
serviceStopFlags := serviceStopFlags{}
4848
servicesCommand := &cobra.Command{
49-
Use: "services",
50-
Short: "Interact with devbox services",
51-
PersistentPreRunE: ensureNixInstalled,
49+
Use: "services",
50+
Short: "Interact with devbox services",
51+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
52+
preruns := append([]cobraFunc{ensureNixInstalled}, persistentPreRunE...)
53+
for _, fn := range preruns {
54+
if err := fn(cmd, args); err != nil {
55+
return err
56+
}
57+
}
58+
return nil
59+
},
5260
}
5361

5462
lsCommand := &cobra.Command{

0 commit comments

Comments
 (0)