-
-
Notifications
You must be signed in to change notification settings - Fork 480
/
context.go
41 lines (32 loc) · 926 Bytes
/
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
package gpg
import "context"
type contextKey int
const (
ctxKeyAlwaysTrust contextKey = iota
ctxKeyUseCache
)
// WithAlwaysTrust will return a context with the flag for always trust set.
func WithAlwaysTrust(ctx context.Context, at bool) context.Context {
return context.WithValue(ctx, ctxKeyAlwaysTrust, at)
}
// IsAlwaysTrust will return the value of the always trust flag or the default
// (false).
func IsAlwaysTrust(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyAlwaysTrust).(bool)
if !ok {
return false
}
return bv
}
// WithUseCache returns a context with the value of NoCache set.
func WithUseCache(ctx context.Context, nc bool) context.Context {
return context.WithValue(ctx, ctxKeyUseCache, nc)
}
// UseCache returns true if this request should ignore the cache.
func UseCache(ctx context.Context) bool {
nc, ok := ctx.Value(ctxKeyUseCache).(bool)
if !ok {
return false
}
return nc
}