-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag.go
103 lines (81 loc) · 1.71 KB
/
flag.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
package cmd
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/xerrors"
)
var (
FlagLogLevel LogLevel = LogLevel{Lvl: zerolog.ErrorLevel}
FlagLogFormat LogFormat = LogFormat{f: "terminal"}
FlagLogOut string
FlagCPUProfile string
FlagMemProfile string
FlagTrace string
FlagExitAfter time.Duration
FlagNumberOfNodes uint = 3
FlagQuiet bool
FlagQueries []string
FlagJSONPretty bool
)
type LogLevel struct {
Lvl zerolog.Level
}
func (f LogLevel) String() string {
return f.Lvl.String()
}
func (f *LogLevel) Set(v string) error {
lvl, err := zerolog.ParseLevel(v)
if err != nil {
return err
}
f.Lvl = lvl
return nil
}
func (f LogLevel) Type() string {
return "log-level"
}
type LogFormat struct {
f string
}
func (f LogFormat) String() string {
return f.f
}
func (f *LogFormat) Set(v string) error {
s := strings.ToLower(v)
switch s {
case "json":
case "terminal":
default:
return xerrors.Errorf("invalid log format: %q", v)
}
f.f = s
return nil
}
func (f LogFormat) Type() string {
return "log-format"
}
func escapeFlagValue(v interface{}, q string) string {
if len(q) < 1 {
return fmt.Sprintf("%v", v)
}
return q + strings.Replace(fmt.Sprintf("%v", v), "'", "\\"+q, -1) + q
}
func PrintFlagsJSON(cmd *cobra.Command) json.RawMessage {
out := map[string]interface{}{}
cmd.Flags().VisitAll(func(pf *pflag.Flag) {
if pf.Name == "help" {
return
}
out[fmt.Sprintf("--%s", pf.Name)] = map[string]interface{}{
"default": escapeFlagValue(pf.DefValue, ""),
"value": escapeFlagValue(pf.Value, ""),
}
})
b, _ := json.Marshal(out)
return b
}