-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (61 loc) · 1.52 KB
/
main.go
File metadata and controls
70 lines (61 loc) · 1.52 KB
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
70
package main
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"connectrpc.com/connect"
"github.com/raystack/compass/cli"
"github.com/raystack/compass/internal/config"
saltconfig "github.com/raystack/salt/config"
)
const (
exitOK = 0
exitError = 1
)
func main() {
cfg, err := loadConfig()
if err != nil {
fmt.Println(err)
}
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
if cmd, err := cli.New(cfg).ExecuteContextC(ctx); err != nil {
printError(err)
msg := err.Error()
if strings.HasPrefix(msg, "unknown command") ||
strings.HasPrefix(msg, "unknown flag") ||
strings.HasPrefix(msg, "unknown shorthand flag") {
if !strings.HasSuffix(msg, "\n") {
fmt.Println()
}
fmt.Println(cmd.UsageString())
os.Exit(exitOK)
}
os.Exit(exitError)
}
}
func loadConfig() (*config.Config, error) {
var cfg config.Config
err := saltconfig.NewLoader(
saltconfig.WithFile("./config.yaml"),
saltconfig.WithEnvPrefix("COMPASS"),
).Load(&cfg)
if err != nil {
loader := saltconfig.NewLoader(saltconfig.WithAppConfig("compass"))
if loadErr := loader.Load(&cfg); loadErr != nil {
return &cfg, fmt.Errorf("config not found: run \"compass config init\" to create one")
}
}
return &cfg, nil
}
func printError(err error) {
if connectErr := new(connect.Error); errors.As(err, &connectErr) {
fmt.Fprintf(os.Stderr, "Code: %s Error: %s\n", connectErr.Code(), connectErr.Message())
return
}
fmt.Fprintln(os.Stderr, err)
}