-
Notifications
You must be signed in to change notification settings - Fork 32
/
flags.go
150 lines (127 loc) · 4.09 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
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
package cmd
// Standard flag definitions shared across commands
import (
"fmt"
"strings"
"github.com/urfave/cli"
)
// Standard flags for path expression parts.
var (
stdOrgFlag = orgFlag("Use this organization.", true)
stdProjectFlag = projectFlag("Use this project.", true)
stdEnvFlag = envFlag("Use this environment.", true)
stdServiceFlag = serviceFlag("Use this service.", "", true)
stdUserFlag = userFlag("Use this user.", true)
stdInstanceFlag = instanceFlag("Use this instance.", true)
)
// orgFlag creates a new --org cli.Flag with custom usage string.
func orgFlag(usage string, required bool) cli.Flag {
return newPlaceholder("org, o", "ORG", usage, "", "TORUS_ORG", required)
}
// projectFlag creates a new --project cli.Flag with custom usage string.
func projectFlag(usage string, required bool) cli.Flag {
return newPlaceholder("project, p", "PROJECT", usage, "", "TORUS_PROJECT", required)
}
// envFlag creates a new --environment cli.Flag with custom usage string.
func envFlag(usage string, required bool) cli.Flag {
return newPlaceholder("environment, e", "ENV", usage, "", "TORUS_ENVIRONMENT", required)
}
// serviceFlag creates a new --service cli.Flag with custom usage string.
func serviceFlag(usage, value string, required bool) cli.Flag {
return newPlaceholder("service, s", "SERVICE", usage, value, "TORUS_SERVICE", required)
}
// userFlag creates a new --user cli.Flag with custom usage string.
func userFlag(usage string, required bool) cli.Flag {
return newPlaceholder("user, u", "USER", usage, "", "TORUS_USER", required)
}
// instanceFlag creates a new --instance cli.Flag with custom usage string.
func instanceFlag(usage string, required bool) cli.Flag {
return newPlaceholder("instance, i", "INSTANCE", usage, "1", "TORUS_INSTANCE", required)
}
// placeHolderStringSliceFlag is a StringSliceFlag that has been extended to use a
// specific placedholder value in the usage, without parsing it out of the
// usage string.
type placeHolderStringSliceFlag struct {
cli.StringSliceFlag
Placeholder string
Default string
Required bool
}
func (psf placeHolderStringSliceFlag) String() string {
flags := prefixedNames(psf.Name, psf.Placeholder)
def := ""
if len(psf.Default) > 0 {
def = fmt.Sprintf(" (default: %s)", psf.Default)
}
multi := " Can be specified multiple times."
if psf.Usage[len(psf.Usage)-1] != '.' {
multi = "." + multi
}
return fmt.Sprintf("%s\t%s%s%s", flags, psf.Usage, multi, def)
}
func newSlicePlaceholder(name, placeholder, usage string, value string,
envvar string, required bool) placeHolderStringSliceFlag {
return placeHolderStringSliceFlag{
StringSliceFlag: cli.StringSliceFlag{
Name: name,
Usage: usage,
EnvVar: envvar,
},
Placeholder: placeholder,
Default: value,
Required: required,
}
}
// placeHolderStringFlag is a StringFlag that has been extended to use a
// specific placedholder value in the usage, without parsing it out of the
// usage string.
type placeHolderStringFlag struct {
cli.StringFlag
Placeholder string
Required bool
}
func (psf placeHolderStringFlag) String() string {
flags := prefixedNames(psf.Name, psf.Placeholder)
def := ""
if psf.Value != "" {
def = fmt.Sprintf(" (default: %s)", psf.Value)
}
return fmt.Sprintf("%s\t%s%s", flags, psf.Usage, def)
}
func newPlaceholder(name, placeholder, usage, value, envvar string,
required bool) placeHolderStringFlag {
return placeHolderStringFlag{
StringFlag: cli.StringFlag{
Name: name,
Usage: usage,
Value: value,
EnvVar: envvar,
},
Placeholder: placeholder,
Required: required,
}
}
// prefixedNames and prefixFor are taken from urfave/cli
func prefixedNames(fullName, placeholder string) string {
var prefixed string
parts := strings.Split(fullName, ",")
for i, name := range parts {
name = strings.Trim(name, " ")
prefixed += prefixFor(name) + name
if placeholder != "" {
prefixed += " " + placeholder
}
if i < len(parts)-1 {
prefixed += ", "
}
}
return prefixed
}
func prefixFor(name string) (prefix string) {
if len(name) == 1 {
prefix = "-"
} else {
prefix = "--"
}
return
}