-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
115 lines (97 loc) · 2.55 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"io"
"log"
"os"
"strings"
"time"
"github.com/hashicorp/go-hclog"
"github.com/rboyer/devconsul/app"
)
type command struct {
Name string
Func func(*app.App) error
Aliases []string
}
var allCommands = []command{
{"up", (*app.App).RunBringUp, nil}, // porcelain
{"down", (*app.App).RunBringDown, []string{"destroy", "rm"}}, // porcelain
{"restart", (*app.App).RunRestart, nil}, // porcelain
{"config", (*app.App).RunConfigDump, nil}, // porcelain
// ================ special scenarios
{"force-docker", (*app.App).RunForceDocker, []string{"docker"}},
{"primary", (*app.App).RunBringUpPrimary, []string{"up-primary", "up-pri"}},
{"stop-dc2", (*app.App).RunStopDC2, nil},
{"restart-dc2", (*app.App).RunRestartDC2, nil},
{"save-grafana", (*app.App).RunDebugSaveGrafana, nil},
{"config-entries", (*app.App).RunDebugListConfigs, nil},
{"grpc-check", (*app.App).RunGRPCCheck, nil},
{"check-mesh", (*app.App).RunCheckMesh, nil},
{"dump-logs", (*app.App).RunDumpLogs, nil},
}
func main() {
log.SetOutput(io.Discard)
// Create logger
logger := hclog.New(&hclog.LoggerOptions{
Name: app.ProgramName,
Level: hclog.Debug,
Output: os.Stderr,
JSONFormat: false,
})
if len(os.Args) == 1 {
logger.Error("Missing required subcommand")
os.Exit(1)
}
subcommand := os.Args[1]
os.Args = os.Args[1:]
os.Args[0] = app.ProgramName
var (
resetOnce bool
timeout time.Duration
)
flag.BoolVar(&resetOnce, "force", false, "force one time operations to run again")
flag.DurationVar(&timeout, "timeout", 1*time.Minute, "[check-mesh] total runtime")
flag.Parse()
if timeout < 0 {
timeout = 0
}
if resetOnce {
if err := app.ResetRunOnceMemory(); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}
if subcommand == "help" {
var keys []string
for _, cmd := range allCommands {
keys = append(keys, cmd.Name)
}
logger.Info("available commands: [" + strings.Join(keys, ", ") + "]")
os.Exit(0)
}
core, err := app.New(logger)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
core.SetTimeout(timeout)
commandMap := make(map[string]func(core *app.App) error)
for _, cmd := range allCommands {
commandMap[cmd.Name] = cmd.Func
for _, alt := range cmd.Aliases {
commandMap[alt] = cmd.Func
}
}
runFn, ok := commandMap[subcommand]
if !ok {
logger.Error("Unknown subcommand", "subcommand", subcommand)
os.Exit(1)
}
err = runFn(core)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
os.Exit(0)
}