-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
218 lines (179 loc) · 4.78 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"github.com/joho/godotenv"
"github.com/yuuki/shawk/command"
"github.com/yuuki/shawk/config"
"github.com/yuuki/shawk/logging"
"github.com/yuuki/shawk/statik"
"github.com/yuuki/shawk/version"
)
const (
exitCodeOK = 0
exitCodeErr = 10 + iota
)
var (
logger = logging.New("main")
)
// CLI is the command line object.
type CLI struct {
// outStream and errStream are the stdout and stderr
// to write message from the CLI.
outStream, errStream io.Writer
}
func main() {
cli := &CLI{outStream: os.Stdout, errStream: os.Stderr}
os.Exit(cli.Run(os.Args))
}
// Run invokes the CLI with the given arguments.
func (c *CLI) Run(args []string) int {
logging.SetOutput(c.errStream)
if len(args) <= 1 {
printHelp(c.errStream)
return exitCodeErr
}
if env := os.Getenv("SHAWK_ENV_FILE"); env != "" {
if err := godotenv.Load(env); err != nil {
log.Fatalf("Cannot start load config from env: %v\n", err)
}
}
if err := config.Load(); err != nil {
log.Fatalf("Cannot load config from env: %v\n", err)
}
var (
debug bool
help bool
)
flags := flag.NewFlagSet("shawk", flag.ContinueOnError)
flags.SetOutput(c.errStream)
flags.Usage = func() {
printHelp(c.errStream)
}
flags.BoolVar(&help, "help", false, "")
flags.BoolVar(&debug, "debug", false, "")
if err := flags.Parse(args[1:]); err != nil {
return exitCodeErr
}
if help {
printHelp(c.outStream)
return exitCodeOK
}
if debug {
logging.SetLogLevel(logging.DEBUG)
}
var err error
switch args[1] {
case "look":
err = c.doLook(args[2:])
case "probe":
err = c.doProbe(args[2:])
case "create-scheme":
err = c.doCreateScheme(args[2:])
case "version":
version.PrintVersion(c.errStream)
return exitCodeOK
case "credits":
text, err := statik.FindString("/CREDITS")
if err != nil {
logger.Fatalf("%v", err)
}
fmt.Fprintln(c.outStream, text)
return exitCodeOK
case "help":
printHelp(c.outStream)
return exitCodeOK
default:
fmt.Fprintf(c.errStream, "No such sub command: %s\n", args[1])
printHelp(c.errStream)
return exitCodeErr
}
if err != nil {
fmt.Fprintf(c.errStream, "%+v\n", err)
return exitCodeErr
}
return 0
}
var helpText = `Usage: shawk [options]
A socket-based tracing system for discovering network dependencies in distributed applications.
Commands:
look show dependencies starting from a specified node.
probe start agent for collecting flows and processes.
create-scheme create CMDB scheme.
version print version
credits print credits
help print help
Options:
--help print help
--debug enable debug logging
Environs:
SHAWK_ENV_FILE=/path/to/envfile
`
func printHelp(w io.Writer) {
fmt.Fprint(w, helpText)
}
func (c *CLI) prepareFlags(name, help string) *flag.FlagSet {
flags := flag.NewFlagSet(name, flag.ContinueOnError)
flags.SetOutput(c.errStream)
flags.Usage = func() {
fmt.Fprint(c.errStream, help)
}
return flags
}
var lookHelpText = `
Usage: shawk look [options]
print dependencies starting from a specified node.
Options:
--ipv4 ADDR filter flows regarding a specific ipv4 address as a root node
--since filter flows since a specific date (relative duration such as '5m', '2h45m')
--until filter flows until a specific date (relative duration such as '5m', '2h45m')
--depth depth of dependency graph
`
const defaultDepth = 1
func (c *CLI) doLook(args []string) error {
var param command.LookParam
flags := c.prepareFlags("look", lookHelpText)
flags.StringVar(¶m.IPv4, "ipv4", "", "")
flags.StringVar(¶m.Since, "since", "", "")
flags.StringVar(¶m.Until, "until", "", "")
flags.IntVar(¶m.Depth, "depth", defaultDepth, "")
if err := flags.Parse(args); err != nil {
return err
}
if param.Depth <= 0 || param.Depth > command.MaxGraphDepth {
return fmt.Errorf("depth must be 0 < depth < %d, but specified %d",
command.MaxGraphDepth, param.Depth)
}
return command.Look(¶m)
}
var probeHelpText = `
Usage: shawk probe [options]
start agent for collecting flows and processes.
Options:
--env
--once run once only if --mode='polling'
`
func (c *CLI) doProbe(args []string) error {
var param command.ProbeParam
flags := c.prepareFlags("probe", probeHelpText)
flags.BoolVar(¶m.Once, "once", false, "")
if err := flags.Parse(args); err != nil {
return err
}
return command.Probe(¶m)
}
var createSchemeHelpText = `
Usage: shawk create-scheme [options]
create CMDB scheme.
`
func (c *CLI) doCreateScheme(args []string) error {
var param command.CreateSchemeParam
flags := c.prepareFlags("create-scheme", createSchemeHelpText)
if err := flags.Parse(args); err != nil {
return err
}
return command.CreateScheme(¶m)
}