-
Notifications
You must be signed in to change notification settings - Fork 90
/
cli.go
40 lines (38 loc) · 943 Bytes
/
cli.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
package middleware
import "github.com/urfave/cli/v2"
func ShouldShowHelp(c *cli.Context) bool {
args := c.Args().Slice()
// if help argument is provided then skip this check.
for _, arg := range args {
if arg == "-h" || arg == "--help" || arg == "help" {
return true
}
}
return false
}
func WithBeforeFuncs(cmd *cli.Command, funcs ...cli.BeforeFunc) *cli.Command {
// run the commands own before function last if it exists
// this will help to ensure we have meaningful levels of error precedence
// e.g check if deployment config exists before checking for aws credentials
b := cmd.Before
cmd.Before = func(c *cli.Context) error {
// skip before funcs and allows the help to be displayed
if ShouldShowHelp(c) {
return nil
}
for _, f := range funcs {
err := f(c)
if err != nil {
return err
}
}
if b != nil {
err := b(c)
if err != nil {
return err
}
}
return nil
}
return cmd
}