forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctxutil.go
319 lines (272 loc) · 8.83 KB
/
ctxutil.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package ctxutil
import "context"
type contextKey int
const (
ctxKeyDebug contextKey = iota
ctxKeyColor
ctxKeyTerminal
ctxKeyInteractive
ctxKeyStdin
ctxKeyAskForMore
ctxKeyClipTimeout
ctxKeyNoConfirm
ctxKeyNoPager
ctxKeyShowSafeContent
ctxKeyGitCommit
ctxKeyAlwaysYes
ctxKeyUseSymbols
ctxKeyNoColor
ctxKeyFuzzySearch
)
// WithDebug returns a context with an explizit value for debug
func WithDebug(ctx context.Context, dbg bool) context.Context {
return context.WithValue(ctx, ctxKeyDebug, dbg)
}
// HasDebug returns true if a value for debug has been set in this context
func HasDebug(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyDebug).(bool)
return ok
}
// IsDebug returns the value of debug or the default (false)
func IsDebug(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyDebug).(bool)
if !ok {
return false
}
return bv
}
// WithColor returns a context with an explizit value for color
func WithColor(ctx context.Context, color bool) context.Context {
return context.WithValue(ctx, ctxKeyColor, color)
}
// HasColor returns true if a value for Color has been set in this context
func HasColor(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyColor).(bool)
return ok
}
// IsColor returns the value of color or the default (true)
func IsColor(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyColor).(bool)
if !ok {
return true
}
return bv
}
// WithTerminal returns a context with an explizit value for terminal
func WithTerminal(ctx context.Context, isTerm bool) context.Context {
return context.WithValue(ctx, ctxKeyTerminal, isTerm)
}
// HasTerminal returns true if a value for Terminal has been set in this context
func HasTerminal(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyTerminal).(bool)
return ok
}
// IsTerminal returns the value of terminal or the default (true)
func IsTerminal(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyTerminal).(bool)
if !ok {
return true
}
return bv
}
// WithInteractive returns a context with an explizit value for interactive
func WithInteractive(ctx context.Context, isInteractive bool) context.Context {
return context.WithValue(ctx, ctxKeyInteractive, isInteractive)
}
// HasInteractive returns true if a value for Interactive has been set in this context
func HasInteractive(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyInteractive).(bool)
return ok
}
// IsInteractive returns the value of interactive or the default (true)
func IsInteractive(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyInteractive).(bool)
if !ok {
return true
}
return bv
}
// WithStdin returns a context with the value for Stdin set. If true some input
// is available on Stdin (e.g. something is being piped into it)
func WithStdin(ctx context.Context, isStdin bool) context.Context {
return context.WithValue(ctx, ctxKeyStdin, isStdin)
}
// HasStdin returns true if a value for Stdin has been set in this context
func HasStdin(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyStdin).(bool)
return ok
}
// IsStdin returns the value of stdin, i.e. if it's true some data is being
// piped to stdin. If not set it returns the default value (false)
func IsStdin(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyStdin).(bool)
if !ok {
return false
}
return bv
}
// WithAskForMore returns a context with the value for ask for more set
func WithAskForMore(ctx context.Context, afm bool) context.Context {
return context.WithValue(ctx, ctxKeyAskForMore, afm)
}
// HasAskForMore returns true if a value for AskForMore has been set in this context
func HasAskForMore(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyAskForMore).(bool)
return ok
}
// IsAskForMore returns the value of ask for more or the default (false)
func IsAskForMore(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyAskForMore).(bool)
if !ok {
return false
}
return bv
}
// WithClipTimeout returns a context with the value for clip timeout set
func WithClipTimeout(ctx context.Context, to int) context.Context {
return context.WithValue(ctx, ctxKeyClipTimeout, to)
}
// HasClipTimeout returns true if a value for ClipTimeout has been set in this context
func HasClipTimeout(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyClipTimeout).(int)
return ok
}
// GetClipTimeout returns the value of clip timeout or the default (45)
func GetClipTimeout(ctx context.Context) int {
iv, ok := ctx.Value(ctxKeyClipTimeout).(int)
if !ok || iv < 1 {
return 45
}
return iv
}
// WithNoConfirm returns a context with the value for ask for more set
func WithNoConfirm(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyNoConfirm, bv)
}
// HasNoConfirm returns true if a value for NoConfirm has been set in this context
func HasNoConfirm(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyNoConfirm).(bool)
return ok
}
// IsNoConfirm returns the value of ask for more or the default (false)
func IsNoConfirm(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyNoConfirm).(bool)
if !ok {
return false
}
return bv
}
// WithNoPager returns a context with the value for ask for more set
func WithNoPager(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyNoPager, bv)
}
// HasNoPager returns true if a value for NoPager has been set in this context
func HasNoPager(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyNoPager).(bool)
return ok
}
// IsNoPager returns the value of ask for more or the default (false)
func IsNoPager(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyNoPager).(bool)
if !ok {
return false
}
return bv
}
// WithShowSafeContent returns a context with the value for ask for more set
func WithShowSafeContent(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyShowSafeContent, bv)
}
// HasShowSafeContent returns true if a value for ShowSafeContent has been set in this context
func HasShowSafeContent(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyShowSafeContent).(bool)
return ok
}
// IsShowSafeContent returns the value of ask for more or the default (false)
func IsShowSafeContent(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyShowSafeContent).(bool)
if !ok {
return false
}
return bv
}
// WithGitCommit returns a context with the value of git commit set
func WithGitCommit(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyGitCommit, bv)
}
// HasGitCommit returns true if a value for GitCommit has been set in this context
func HasGitCommit(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyShowSafeContent).(bool)
return ok
}
// IsGitCommit returns the value of git commit or the default (true)
func IsGitCommit(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyGitCommit).(bool)
if !ok {
return true
}
return bv
}
// WithUseSymbols returns a context with the value for ask for more set
func WithUseSymbols(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyUseSymbols, bv)
}
// HasUseSymbols returns true if a value for UseSymbols has been set in this context
func HasUseSymbols(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyUseSymbols).(bool)
return ok
}
// IsUseSymbols returns the value of ask for more or the default (false)
func IsUseSymbols(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyUseSymbols).(bool)
if !ok {
return false
}
return bv
}
// WithNoColor returns a context with the value for ask for more set
func WithNoColor(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyNoColor, bv)
}
// HasNoColor returns true if a value for NoColor has been set in this context
func HasNoColor(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyNoColor).(bool)
return ok
}
// IsNoColor returns the value of ask for more or the default (false)
func IsNoColor(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyNoColor).(bool)
if !ok {
return false
}
return bv
}
// WithAlwaysYes returns a context with the value of always yes set
func WithAlwaysYes(ctx context.Context, bv bool) context.Context {
return context.WithValue(ctx, ctxKeyAlwaysYes, bv)
}
// HasAlwaysYes returns true if a value for AlwaysYes has been set in this context
func HasAlwaysYes(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyAlwaysYes).(bool)
return ok
}
// IsAlwaysYes returns the value of always yes or the default (false)
func IsAlwaysYes(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyAlwaysYes).(bool)
if !ok {
return false
}
return bv
}
// WithFuzzySearch returns a context with the value for fuzzy search set
func WithFuzzySearch(ctx context.Context, fuzzy bool) context.Context {
return context.WithValue(ctx, ctxKeyFuzzySearch, fuzzy)
}
// IsFuzzySearch return the value of fuzzy search or the default (true)
func IsFuzzySearch(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyFuzzySearch).(bool)
if !ok {
return true
}
return bv
}