/
context.go
177 lines (150 loc) · 4.93 KB
/
context.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package flag
import (
"context"
"slices"
"time"
"github.com/spf13/pflag"
"github.com/superfly/flyctl/internal/env"
"github.com/superfly/flyctl/internal/flag/flagctx"
"github.com/superfly/flyctl/internal/flag/flagnames"
)
// NewContext derives a context that carries fs from ctx.
func NewContext(ctx context.Context, fs *pflag.FlagSet) context.Context {
return flagctx.NewContext(ctx, fs)
}
// FromContext returns the FlagSet ctx carries. It panics in case ctx carries
// no FlagSet.
func FromContext(ctx context.Context) *pflag.FlagSet {
return flagctx.FromContext(ctx)
}
// Args is shorthand for FromContext(ctx).Args().
func Args(ctx context.Context) []string {
return FromContext(ctx).Args()
}
// FirstArg returns the first arg ctx carries or an empty string in case ctx
// carries an empty argument set. It panics in case ctx carries no FlagSet.
func FirstArg(ctx context.Context) string {
if args := Args(ctx); len(args) > 0 {
return args[0]
}
return ""
}
// GetString returns the value of the named string flag ctx carries.
func GetString(ctx context.Context, name string) string {
if v, err := FromContext(ctx).GetString(name); err != nil {
return ""
} else {
return v
}
}
// SetString sets the value of the named string flag ctx carries.
func SetString(ctx context.Context, name, value string) error {
return FromContext(ctx).Set(name, value)
}
// GetInt returns the value of the named int flag ctx carries. It panics
// in case ctx carries no flags or in case the named flag isn't an int one.
func GetInt(ctx context.Context, name string) int {
if v, err := FromContext(ctx).GetInt(name); err != nil {
panic(err)
} else {
return v
}
}
// GetFloat64 returns the value of the named int flag ctx carries. It panics
// in case ctx carries no flags or in case the named flag isn't a float64 one.
func GetFloat64(ctx context.Context, name string) float64 {
if v, err := FromContext(ctx).GetFloat64(name); err != nil {
panic(err)
} else {
return v
}
}
// GetStringArray returns the values of the named string flag ctx carries.
// Preserves commas (unlike the following `GetStringSlice`): in `--flag x,y` the value is string[]{`x,y`}.
// This is useful to pass key-value pairs like environment variables or build arguments.
func GetStringArray(ctx context.Context, name string) []string {
if v, err := FromContext(ctx).GetStringArray(name); err != nil {
return []string{}
} else {
return v
}
}
// GetStringSlice returns the values of the named string flag ctx carries.
// Can be comma separated or passed "by repeated flags": `--flag x,y` is equivalent to `--flag x --flag y`.
func GetStringSlice(ctx context.Context, name string) []string {
if v, err := FromContext(ctx).GetStringSlice(name); err != nil {
return []string{}
} else {
return v
}
}
// GetDuration returns the value of the named duration flag ctx carries.
func GetDuration(ctx context.Context, name string) time.Duration {
if v, err := FromContext(ctx).GetDuration(name); err != nil {
return 0
} else {
return v
}
}
// GetBool returns the value of the named boolean flag ctx carries.
func GetBool(ctx context.Context, name string) bool {
if v, err := FromContext(ctx).GetBool(name); err != nil {
return false
} else {
return v
}
}
// IsSpecified returns whether a flag has been specified at all or not.
// This is useful, for example, when differentiating between 0/"" and unspecified.
func IsSpecified(ctx context.Context, name string) bool {
flag := FromContext(ctx).Lookup(name)
return flag != nil && flag.Changed
}
// GetOrg is shorthand for GetString(ctx, Org).
func GetOrg(ctx context.Context) string {
org := GetString(ctx, flagnames.Org)
if org == "" {
org = env.First("FLY_ORG")
}
return org
}
// GetRegion is shorthand for GetString(ctx, Region).
func GetRegion(ctx context.Context) string {
return GetString(ctx, flagnames.Region)
}
// GetYes is shorthand for GetBool(ctx, Yes).
func GetYes(ctx context.Context) bool {
return GetBool(ctx, flagnames.Yes)
}
// GetApp is shorthand for GetString(ctx, App).
func GetApp(ctx context.Context) string {
return GetString(ctx, flagnames.App)
}
// GetAppConfigFilePath is shorthand for GetString(ctx, AppConfigFilePath).
func GetAppConfigFilePath(ctx context.Context) string {
if path, err := FromContext(ctx).GetString(flagnames.AppConfigFilePath); err != nil {
return ""
} else {
return path
}
}
// GetBindAddr is shorthand for GetString(ctx, BindAddr).
func GetBindAddr(ctx context.Context) string {
return GetString(ctx, flagnames.BindAddr)
}
// GetFlagsName returns the name of flags that have been set except unwanted flags.
func GetFlagsName(ctx context.Context, ignoreFlags []string) []string {
flagsName := []string{}
FromContext(ctx).Visit(func(f *pflag.Flag) {
if f.Hidden {
return
}
if !slices.Contains(ignoreFlags, f.Name) {
flagsName = append(flagsName, f.Name)
}
})
return flagsName
}
func GetProcessGroup(ctx context.Context) string {
return GetString(ctx, flagnames.ProcessGroup)
}