forked from gopasspw/gopass
/
context.go
199 lines (171 loc) · 5.47 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package sub
import (
"context"
"github.com/justwatchcom/gopass/store"
)
type contextKey int
const (
ctxKeyFsckCheck contextKey = iota
ctxKeyFsckForce
ctxKeyAutoSync
ctxKeyReason
ctxKeyImportFunc
ctxKeyRecipientFunc
ctxKeyFsckFunc
ctxKeyCheckRecipients
)
// WithFsckCheck returns a context with the flag for fscks check set
func WithFsckCheck(ctx context.Context, check bool) context.Context {
return context.WithValue(ctx, ctxKeyFsckCheck, check)
}
// HasFsckCheck returns true if a value for fsck check has been set in this
// context
func HasFsckCheck(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyFsckCheck).(bool)
return ok
}
// IsFsckCheck returns the value of fsck check
func IsFsckCheck(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyFsckCheck).(bool)
if !ok {
return false
}
return bv
}
// WithFsckForce returns a context with the flag for fsck force set
func WithFsckForce(ctx context.Context, force bool) context.Context {
return context.WithValue(ctx, ctxKeyFsckForce, force)
}
// HasFsckForce returns true if a value for fsck force has been set in this
// context
func HasFsckForce(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyFsckForce).(bool)
return ok
}
// IsFsckForce returns the value of fsck force
func IsFsckForce(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyFsckForce).(bool)
if !ok {
return false
}
return bv
}
// WithAutoSync returns a context with the flag for autosync set
func WithAutoSync(ctx context.Context, sync bool) context.Context {
return context.WithValue(ctx, ctxKeyAutoSync, sync)
}
// HasAutoSync has been set if a value for auto sync has been set in this
// context
func HasAutoSync(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyAutoSync).(bool)
return ok
}
// IsAutoSync returns the value of autosync
func IsAutoSync(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyAutoSync).(bool)
if !ok {
return true
}
return bv
}
// WithReason returns a context with a commit/change reason set
func WithReason(ctx context.Context, msg string) context.Context {
return context.WithValue(ctx, ctxKeyReason, msg)
}
// HasReason returns true if a value for reason has been set in this context
func HasReason(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyReason).(string)
return ok
}
// GetReason returns the value of reason
func GetReason(ctx context.Context) string {
sv, ok := ctx.Value(ctxKeyReason).(string)
if !ok {
return ""
}
return sv
}
// WithImportFunc will return a context with the import callback set
func WithImportFunc(ctx context.Context, imf store.ImportCallback) context.Context {
return context.WithValue(ctx, ctxKeyImportFunc, imf)
}
// HasImportFunc returns true if a value for import func has been set in this
// context
func HasImportFunc(ctx context.Context) bool {
imf, ok := ctx.Value(ctxKeyImportFunc).(store.ImportCallback)
return ok && imf != nil
}
// GetImportFunc will return the import callback or a default one returning true
// Note: will never return nil
func GetImportFunc(ctx context.Context) store.ImportCallback {
imf, ok := ctx.Value(ctxKeyImportFunc).(store.ImportCallback)
if !ok || imf == nil {
return func(context.Context, string, []string) bool {
return true
}
}
return imf
}
// WithRecipientFunc will return a context with the recipient callback set
func WithRecipientFunc(ctx context.Context, imf store.RecipientCallback) context.Context {
return context.WithValue(ctx, ctxKeyRecipientFunc, imf)
}
// HasRecipientFunc returns true if a recipient func has been set in this
// context
func HasRecipientFunc(ctx context.Context) bool {
imf, ok := ctx.Value(ctxKeyRecipientFunc).(store.RecipientCallback)
return ok && imf != nil
}
// GetRecipientFunc will return the recipient callback or a default one returning
// the unaltered list of recipients.
// Note: will never return nil
func GetRecipientFunc(ctx context.Context) store.RecipientCallback {
imf, ok := ctx.Value(ctxKeyRecipientFunc).(store.RecipientCallback)
if !ok || imf == nil {
return func(ctx context.Context, msg string, rs []string) ([]string, error) {
return rs, nil
}
}
return imf
}
// WithFsckFunc will return a context with the fsck confirmation callback set
func WithFsckFunc(ctx context.Context, imf store.FsckCallback) context.Context {
return context.WithValue(ctx, ctxKeyFsckFunc, imf)
}
// HasFsckFunc returns true if a fsck func has been set in this context
func HasFsckFunc(ctx context.Context) bool {
imf, ok := ctx.Value(ctxKeyFsckFunc).(store.FsckCallback)
return ok && imf != nil
}
// GetFsckFunc will return the fsck confirmation callback or a default one
// returning true.
// Note: will never return nil
func GetFsckFunc(ctx context.Context) store.FsckCallback {
imf, ok := ctx.Value(ctxKeyFsckFunc).(store.FsckCallback)
if !ok || imf == nil {
return func(context.Context, string) bool {
return true
}
}
return imf
}
// WithCheckRecipients will return a context with the flag for check recipients
// set
func WithCheckRecipients(ctx context.Context, cr bool) context.Context {
return context.WithValue(ctx, ctxKeyCheckRecipients, cr)
}
// HasCheckRecipients returns true if check recipients has been set in this
// context
func HasCheckRecipients(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyCheckRecipients).(bool)
return ok
}
// IsCheckRecipients will return the value of the check recipients flag or the
// default value (false)
func IsCheckRecipients(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyCheckRecipients).(bool)
if !ok {
return false
}
return bv
}