generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
commandLine.go
55 lines (46 loc) · 1.48 KB
/
commandLine.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
package main
import (
"os"
"time"
"github.com/alecthomas/kong"
"go.uber.org/fx"
)
type CommandLine struct {
Exec []string `name:"exec" short:"e" required:"" help:"one or more commands to execute when the switch triggers"`
Dir string `name:"dir" short:"d" optional:"" help:"the working directory for all commands"`
HTTP string `name:"http" short:"h" default:":8080" help:"the HTTP listen address or port"`
TTL time.Duration `name:"ttl" short:"t" default:"1m" help:"the maximum interval for TTL updates to keep the switch open"`
Misses int `name:"misses" short:"m" default:"1" help:"the maximum number of missed updates allowed before the switch closes"`
Debug bool `name:"debug" default:"false" help:"produce debug logging"`
}
func parseCommandLine(args []string) fx.Option {
var (
options []fx.Option
cl CommandLine
k, err = kong.New(
&cl,
kong.Description(
"A dead man's switch which invokes one or more actions unless postponed on regular intervals. To postpone the action(s), issue an HTTP PUT to /postpone, with no body, to the configured HTTP address.",
),
)
)
if err == nil {
_, err = k.Parse(args)
}
if err == nil {
var debug Logger
if cl.Debug {
debug = WriterLogger{Writer: os.Stdout}
} else {
debug = DiscardLogger{}
}
options = append(options,
fx.Logger(debug),
fx.Supply(cl),
)
}
if err != nil {
options = append(options, fx.Error(err))
}
return fx.Options(options...)
}