Skip to content

[global] Fix global services #1484

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
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 10 additions & 3 deletions internal/boxcli/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ func globalCmd() *cobra.Command {
globalCmd := &cobra.Command{}

*globalCmd = cobra.Command{
Use: "global",
Short: "Manage global devbox packages",
Use: "global",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of copy-pasting, it'll be nice to use a common variable...

persistentPreRunE := setGlobalConfigForDelegatedCommands(globalCmd)

*globalCmd = cobra.Command{
 		Use:   "global",
 		PersistentPreRunE: persistentPreRunE,
 		 ...
 		 }
 		 
 		 ...
 addCommandAndHideConfigFlag(
 		 globalCmd,
 		servicesCmd(persistentPreRunE),
 ),

Short: "Manage global devbox packages",
// PersistentPreRunE is inherited only if children do not implement it
// (i.e. it's not chained). So this is fragile. Ideally we stop
// using PersistentPreRunE. For now a hack is to pass it down to commands
// that declare their own.
PersistentPreRunE: setGlobalConfigForDelegatedCommands(globalCmd),
PersistentPostRunE: ensureGlobalEnvEnabled,
}
Expand All @@ -32,7 +36,10 @@ func globalCmd() *cobra.Command {
addCommandAndHideConfigFlag(globalCmd, pushCmd())
addCommandAndHideConfigFlag(globalCmd, removeCmd())
addCommandAndHideConfigFlag(globalCmd, runCmd())
addCommandAndHideConfigFlag(globalCmd, servicesCmd())
addCommandAndHideConfigFlag(
globalCmd,
servicesCmd(setGlobalConfigForDelegatedCommands(globalCmd)),
)
addCommandAndHideConfigFlag(globalCmd, shellEnvCmd())
addCommandAndHideConfigFlag(globalCmd, updateCmd())

Expand Down
2 changes: 2 additions & 0 deletions internal/boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"go.jetpack.io/devbox/internal/vercheck"
)

type cobraFunc func(cmd *cobra.Command, args []string) error

var (
debugMiddleware = &midcobra.DebugMiddleware{}
traceMiddleware = &midcobra.TraceMiddleware{}
Expand Down
16 changes: 12 additions & 4 deletions internal/boxcli/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,22 @@ func (flags *serviceStopFlags) register(cmd *cobra.Command) {
&flags.allProjects, "all-projects", false, "Stop all running services across all your projects.\nThis flag cannot be used simultaneously with the [services] argument")
}

func servicesCmd() *cobra.Command {
func servicesCmd(persistentPreRunE ...cobraFunc) *cobra.Command {
flags := servicesCmdFlags{}
serviceUpFlags := serviceUpFlags{}
serviceStopFlags := serviceStopFlags{}
servicesCommand := &cobra.Command{
Use: "services",
Short: "Interact with devbox services",
PersistentPreRunE: ensureNixInstalled,
Use: "services",
Short: "Interact with devbox services",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
preruns := append([]cobraFunc{ensureNixInstalled}, persistentPreRunE...)
for _, fn := range preruns {
if err := fn(cmd, args); err != nil {
return err
}
}
return nil
},
}

lsCommand := &cobra.Command{
Expand Down