generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmdcontext.go
38 lines (32 loc) · 1.07 KB
/
cmdcontext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package cmdcontext
import (
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/spf13/cobra"
)
// InvokeParentPersistentPreRun executes the immediate parent command's
// PersistentPreRunE and PersistentPreRun functions, in that order. If
// an error is returned from PersistentPreRunE, it is immediately returned.
//
// TODO: reverse walk up command tree? would need to ensure no one parent is invoked multiple times.
func InvokeParentPersistentPreRun(cmd *cobra.Command, args []string) error {
parentCmd := cmd.Parent()
if parentCmd == nil {
return nil
}
var err error
// Invoke PersistentPreRunE, returning an error if one occurs
// If no error is returned, proceed with PersistentPreRun
parentPersistentPreRunE := parentCmd.PersistentPreRunE
if parentPersistentPreRunE != nil {
err = parentPersistentPreRunE(parentCmd, args)
}
if err != nil {
return breverrors.WrapAndTrace(err)
}
// Invoke PersistentPreRun
parentPersistentPreRun := parentCmd.PersistentPreRun
if parentPersistentPreRun != nil {
parentPersistentPreRun(parentCmd, args)
}
return nil
}