-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
53 lines (47 loc) · 1.3 KB
/
run.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package verdeter
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
// buildPreRunCheckE create a cobra compatible PreRunE function.
func (verdeterCmd *VerdeterCommand) buildPreRunCheckE(preRunE func(cobraCmd *cobra.Command, args []string) error) func(*cobra.Command, []string) error {
if preRunE == nil {
return nil
}
return func(cobraCmd *cobra.Command, args []string) error {
err := verdeterCmd.initAndValidate()
if err != nil {
return err
}
if preRunE == nil {
return nil
}
return preRunE(cobraCmd, args)
}
}
// initAndValidate initialize the command
func (verdeterCmd *VerdeterCommand) initAndValidate() error {
err := initConfig(verdeterCmd)
if err != nil {
return err
}
if err := verdeterCmd.Validate(true); err != nil {
return fmt.Errorf("prerun check for %s failed. (Error=%q))", verdeterCmd.cmd.Name(), err.Error())
}
return nil
}
// buildPreRunCheck create a cobra compatible PreRun function.
func (verdeterCmd *VerdeterCommand) buildPreRunCheck(preRun func(cobraCmd *cobra.Command, args []string)) func(*cobra.Command, []string) {
return func(cobraCmd *cobra.Command, args []string) {
err := verdeterCmd.initAndValidate()
if err != nil {
fmt.Printf("An error was returned: %s\n", err.Error())
os.Exit(1)
}
if preRun == nil {
return
}
preRun(cobraCmd, args)
}
}