-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
context.go
90 lines (76 loc) · 2.23 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
package action
import "context"
type contextKey int
const (
ctxKeyClip contextKey = iota
ctxKeyForce
ctxKeyPasswordOnly
ctxKeyPrintQR
ctxKeyRevision
)
// WithClip returns a context with the value for clip (for copy to clipboard)
// set
func WithClip(ctx context.Context, clip bool) context.Context {
return context.WithValue(ctx, ctxKeyClip, clip)
}
// IsClip returns the value of clip or the default (false)
func IsClip(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyClip).(bool)
if !ok {
return false
}
return bv
}
// WithForce returns a context with the value for force set
func WithForce(ctx context.Context, force bool) context.Context {
return context.WithValue(ctx, ctxKeyForce, force)
}
// IsForce returns the value of force or the default (false)
func IsForce(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyForce).(bool)
if !ok {
return false
}
return bv
}
// WithPasswordOnly returns a context with the value of password only set
func WithPasswordOnly(ctx context.Context, pw bool) context.Context {
return context.WithValue(ctx, ctxKeyPasswordOnly, pw)
}
// IsPasswordOnly returns the value of password only or the default (false)
func IsPasswordOnly(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyPasswordOnly).(bool)
if !ok {
return false
}
return bv
}
// WithPrintQR returns a context with the value of print QR set
func WithPrintQR(ctx context.Context, qr bool) context.Context {
return context.WithValue(ctx, ctxKeyPrintQR, qr)
}
// IsPrintQR returns the value of print QR or the default (false)
func IsPrintQR(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyPrintQR).(bool)
if !ok {
return false
}
return bv
}
// WithRevision returns a context withe the value of revision set
func WithRevision(ctx context.Context, rev string) context.Context {
return context.WithValue(ctx, ctxKeyRevision, rev)
}
// HasRevision returns true if a value for revision was set in this context
func HasRevision(ctx context.Context) bool {
sv, ok := ctx.Value(ctxKeyRevision).(string)
return ok && sv != ""
}
// GetRevision returns the revison set in this context or an empty string
func GetRevision(ctx context.Context) string {
sv, ok := ctx.Value(ctxKeyRevision).(string)
if !ok {
return ""
}
return sv
}