forked from raystack/compass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (54 loc) · 1.19 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
61
62
63
64
65
66
67
68
69
package main
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/goto/compass/cli"
)
const exitError = 1
func main() {
if err := run(); err != nil {
os.Exit(exitError)
}
}
func run() error {
cliConfig, err := cli.LoadConfig()
if err != nil {
fmt.Println(err)
}
cliConfig.Telemetry.AppVersion = cli.Version
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
if cmd, err := cli.New(cliConfig).ExecuteContextC(ctx); err != nil {
printError(err)
switch errStr := err.Error(); {
case strings.HasPrefix(errStr, "unknown command"),
strings.HasPrefix(errStr, "unknown flag"),
strings.HasPrefix(errStr, "unknown shorthand flag"):
if !strings.HasSuffix(errStr, "\n") {
fmt.Println()
}
fmt.Println(cmd.UsageString())
return nil
}
return err
}
return nil
}
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)
}
}