Skip to content
Merged
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
15 changes: 9 additions & 6 deletions internal/boxcli/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ import (
)

func globalCmd() *cobra.Command {

globalCmd := &cobra.Command{}

persistentPreRunE := setGlobalConfigForDelegatedCommands(globalCmd)
*globalCmd = cobra.Command{
Use: "global",
Short: "Manage global devbox packages",
PersistentPreRunE: setGlobalConfigForDelegatedCommands(globalCmd),
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: persistentPreRunE,
PersistentPostRunE: ensureGlobalEnvEnabled,
}

Expand All @@ -32,7 +35,7 @@ func globalCmd() *cobra.Command {
addCommandAndHideConfigFlag(globalCmd, pushCmd())
addCommandAndHideConfigFlag(globalCmd, removeCmd())
addCommandAndHideConfigFlag(globalCmd, runCmd())
addCommandAndHideConfigFlag(globalCmd, servicesCmd())
addCommandAndHideConfigFlag(globalCmd, servicesCmd(persistentPreRunE))
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