forked from seashell/drago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (121 loc) · 4.93 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//go:generate yarn --cwd ./ui
//go:generate yarn --cwd ./ui build
//go:generate touch ./ui/build/.keep
package main
import (
"context"
"embed"
"fmt"
"io/fs"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/debug"
command "github.com/seashell/drago/command"
cli "github.com/seashell/drago/pkg/cli"
version "github.com/seashell/drago/version"
)
//go:embed ui/build/*
var uiefs embed.FS
// Credits to https://github.com/pulumi/pulumi/blob/master/pkg/cmd/pulumi/main.go
func panicHandler() {
if panicPayload := recover(); panicPayload != nil {
stack := string(debug.Stack())
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "================================================================================")
fmt.Fprintln(os.Stderr, "Drago has encountered a fatal error. This is a bug!")
fmt.Fprintln(os.Stderr, "We would appreciate a report: https://github.com/seashell/drago/issues/")
fmt.Fprintln(os.Stderr, "Please provide all of the below text in your report.")
fmt.Fprintln(os.Stderr, "================================================================================")
fmt.Fprintf(os.Stderr, "Drago Version: %s\n", version.Version)
fmt.Fprintf(os.Stderr, "Go Version: %s\n", runtime.Version())
fmt.Fprintf(os.Stderr, "Go Compiler: %s\n", runtime.Compiler)
fmt.Fprintf(os.Stderr, "Architecture: %s\n", runtime.GOARCH)
fmt.Fprintf(os.Stderr, "Operating System: %s\n", runtime.GOOS)
fmt.Fprintf(os.Stderr, "Panic: %s\n\n", panicPayload)
fmt.Fprintln(os.Stderr, stack)
}
}
func init() {
os.Setenv("TZ", "UTC")
}
func main() {
os.Exit(run(os.Args[1:]))
}
func run(args []string) int {
cli := setupCLI()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
signalCh := make(chan os.Signal)
signal.Notify(signalCh, os.Interrupt)
go func() {
<-signalCh
fmt.Fprintf(os.Stderr, "Received signal. Interrupting...\n")
cancel()
}()
defer panicHandler()
code, err := cli.Run(ctx, args)
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
return 1
}
return code
}
func setupCLI() *cli.CLI {
ui := &cli.SimpleUI{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
subfs, err := fs.Sub(uiefs, "ui/build")
if err != nil {
panic(err)
}
uifs := http.FS(subfs)
cli := cli.New(&cli.Config{
Name: "drago",
Commands: map[string]cli.Command{
"agent": &command.AgentCommand{UI: ui, StaticFS: uifs},
"agent-info": &command.AgentInfoCommand{UI: ui},
"acl": &command.ACLCommand{UI: ui},
"acl bootstrap": &command.ACLBootstrapCommand{UI: ui},
"acl token": &command.ACLTokenCommand{UI: ui},
"acl token create": &command.ACLTokenCreateCommand{UI: ui},
"acl token delete": &command.ACLTokenDeleteCommand{UI: ui},
"acl token info": &command.ACLTokenInfoCommand{UI: ui},
"acl token list": &command.ACLTokenListCommand{UI: ui},
"acl token self": &command.ACLTokenSelfCommand{UI: ui},
"acl token update": &command.ACLTokenUpdateCommand{UI: ui},
"acl policy": &command.ACLPolicyCommand{UI: ui},
"acl policy apply": &command.ACLPolicyApplyCommand{UI: ui},
"acl policy delete": &command.ACLPolicyDeleteCommand{UI: ui},
"acl policy info": &command.ACLPolicyInfoCommand{UI: ui},
"acl policy list": &command.ACLPolicyListCommand{UI: ui},
"network": &command.NetworkCommand{UI: ui},
"network create": &command.NetworkCreateCommand{UI: ui},
"network delete": &command.NetworkDeleteCommand{UI: ui},
"network info": &command.NetworkInfoCommand{UI: ui},
"network list": &command.NetworkListCommand{UI: ui},
"node": &command.NodeCommand{UI: ui},
"node status": &command.NodeStatusCommand{UI: ui},
"node join": &command.NodeJoinCommand{UI: ui},
"node leave": &command.NodeLeaveCommand{UI: ui},
"interface": &command.InterfaceCommand{UI: ui},
"interface list": &command.InterfaceListCommand{UI: ui},
"interface update": &command.InterfaceUpdateCommand{UI: ui},
"connection": &command.ConnectionCommand{UI: ui},
"connection list": &command.ConnectionListCommand{UI: ui},
"connection create": &command.ConnectionCreateCommand{UI: ui},
"connection delete": &command.ConnectionDeleteCommand{UI: ui},
"connection update": &command.ConnectionUpdateCommand{UI: ui},
"connection update-rules": &command.ConnectionUpdateRulesCommand{UI: ui},
// "system": &command.SystemCommand{UI: ui},
// "system gc": &command.SystemGCCommand{UI: ui},
"ui": &command.UICommand{UI: ui},
"version": &command.VersionCommand{UI: ui},
},
Version: version.GetVersion().VersionNumber(),
})
return cli
}