-
Notifications
You must be signed in to change notification settings - Fork 2
/
flags.go
94 lines (79 loc) · 2.47 KB
/
flags.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
package main
import (
"flag"
"fmt"
"os"
)
// UIMode describes how the app should run
type UIMode string
const (
// NcursesMode is used when we're using ncurses
NcursesMode UIMode = "ui"
// JobMode is used for one off commands
JobMode UIMode = "job"
// HelpMode is used when we show the help
HelpMode UIMode = "help"
// ListJobsMode is used to list jobs
ListJobsMode UIMode = "list-jobs"
)
type trekOptions struct {
nomadAddress string
trekMode UIMode
jobID string
taskGroup string
allocationIndex int
taskName string
displayFormat string
}
type cliOptions struct {
nomadAddress string
help bool
ncurses bool
listJobs bool
job string
taskGroup string
allocationIndex int
taskName string
displayFormat string
}
func (options *cliOptions) DetermineMode() UIMode {
actualMode := HelpMode
if options.help {
actualMode = HelpMode
} else {
if options.ncurses {
actualMode = NcursesMode
} else if options.listJobs {
actualMode = ListJobsMode
} else if options.job != "" {
actualMode = JobMode
}
}
return actualMode
}
func parseFlags() trekOptions {
options := new(cliOptions)
flag.BoolVar(&(*options).help, "help", false, "show usage prompt")
flag.StringVar(&(*options).nomadAddress, "nomad-address", "http://localhost:4646", "nomad cluster address")
flag.BoolVar(&(*options).ncurses, "ui", false, "use UI mode")
flag.BoolVar(&(*options).listJobs, "list-jobs", false, "list jobs")
flag.StringVar(&(*options).job, "job", "", "job name to get (only used when running in non-ui mode)")
flag.StringVar(&(*options).taskGroup, "task-group", "", "task group to get (only used when running in non-ui mode)")
flag.IntVar(&(*options).allocationIndex, "allocation", -1, "allocation index to get (starts at 0, only used when running in non-ui mode)")
flag.StringVar(&(*options).taskName, "task", "", "task name to get (only used when running in non-ui mode)")
flag.StringVar(&(*options).displayFormat, "display-format", "", "task display format")
flag.Parse()
return trekOptions{
nomadAddress: (*options).nomadAddress,
trekMode: (*options).DetermineMode(),
jobID: (*options).job,
taskGroup: (*options).taskGroup,
allocationIndex: (*options).allocationIndex,
taskName: (*options).taskName,
displayFormat: (*options).displayFormat,
}
}
func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [options]\n", os.Args[0])
flag.PrintDefaults()
}