-
Notifications
You must be signed in to change notification settings - Fork 1
/
args.go
106 lines (85 loc) · 2.09 KB
/
args.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
package main
import (
"flag"
"os"
"path/filepath"
"runtime"
"text/template"
)
type UsageArgs struct {
Cmd string
Release string
Version string
BuildDate string
GoVer string
OS string
Arch string
DefaultDataDir string
}
var usageTmpl = `Usage: {{.Cmd}} [options]
{{.Release}} {{.Version}}
Built on {{.BuildDate}}
Target: {{.GoVer}} {{.OS}} {{.Arch}}
--help
Shows this help listing
--config <config-path> (default: config.json)
Configuration file
--datadir <data-dir> (default: ./data)
Directory to use for server storage
--log <log-path>
Log file path.
--pprof <address>
Launch runtime profiling on address
--regen-keys
Regenerate certificate and keys
`
type args struct {
ShowHelp bool
ConfigPath string
DataDir string
LogPath string
PProf string
RegenKeys bool
SQLiteDB string
CleanUp bool
}
func defaultDataDir() string {
cwd, _ := os.Getwd()
dirname := "data"
return filepath.Join(cwd, dirname)
}
func defaultConfigPath() string {
cwd, _ := os.Getwd()
return filepath.Join(cwd, "config.json")
}
func Usage() {
t, err := template.New("usage").Parse(usageTmpl)
if err != nil {
panic("unable to parse usage template")
}
err = t.Execute(os.Stdout, UsageArgs{
Cmd: os.Args[0],
Release: verRelease,
Version: VERSION,
BuildDate: BUILDDATE,
GoVer: runtime.Version(),
OS: runtime.GOOS,
Arch: runtime.GOARCH,
DefaultDataDir: defaultDataDir(),
})
if err != nil {
panic("unable to execute usage template")
}
}
var Args args
func init() {
flag.Usage = Usage
flag.BoolVar(&Args.ShowHelp, "help", false, "")
flag.StringVar(&Args.ConfigPath, "config", defaultConfigPath(), "")
flag.StringVar(&Args.DataDir, "datadir", defaultDataDir(), "")
flag.StringVar(&Args.LogPath, "log", "", "")
flag.StringVar(&Args.PProf, "pprof", "", "")
flag.BoolVar(&Args.RegenKeys, "regen-keys", false, "")
flag.StringVar(&Args.SQLiteDB, "import-murmurdb", "", "")
flag.BoolVar(&Args.CleanUp, "cleanup", false, "")
}