-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
60 lines (48 loc) · 1.07 KB
/
main.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
54
55
56
57
58
59
60
package main
import (
"fmt"
"os"
"strings"
"github.com/raystack/guardian/cli"
)
const (
exitOK = 0
exitError = 1
)
func main() {
cliConfig, err := cli.LoadConfig()
if err != nil {
cliConfig = &cli.Config{}
}
root := cli.New(cliConfig)
if cmd, err := root.ExecuteC(); err != nil {
printError(err)
cmdErr := strings.HasPrefix(err.Error(), "unknown command")
flagErr := strings.HasPrefix(err.Error(), "unknown flag")
sflagErr := strings.HasPrefix(err.Error(), "unknown shorthand flag")
if cmdErr || flagErr || sflagErr {
if !strings.HasSuffix(err.Error(), "\n") {
fmt.Println()
}
fmt.Println(cmd.UsageString())
os.Exit(exitOK)
} else {
os.Exit(exitError)
}
}
// TODO: Notify if updated version is available
}
func printError(err error) {
e := err.Error()
if strings.Split(e, ":")[0] == "rpc error" {
es := strings.Split(e, "= ")
em := es[2]
errMsg := "Error: " + em
ec := es[1]
errCode := ec[0 : len(ec)-5]
errCode = "Code: " + errCode
fmt.Fprintln(os.Stderr, errCode, errMsg)
} else {
fmt.Fprintln(os.Stderr, err)
}
}