-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
context.go
90 lines (69 loc) · 2.36 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 backend
import "context"
type contextKey int
const (
ctxKeyCryptoBackend contextKey = iota
ctxKeyStorageBackend
)
// CryptoBackendName returns the name of the given backend.
func CryptoBackendName(cb CryptoBackend) string {
if name, err := CryptoRegistry.BackendName(cb); err == nil {
return name
}
return ""
}
// WithCryptoBackendString returns a context with the given crypto backend set.
func WithCryptoBackendString(ctx context.Context, be string) context.Context {
if cb, err := CryptoRegistry.Backend(be); err == nil {
ctx = WithCryptoBackend(ctx, cb)
}
return ctx
}
// WithCryptoBackend returns a context with the given crypto backend set.
func WithCryptoBackend(ctx context.Context, be CryptoBackend) context.Context {
return context.WithValue(ctx, ctxKeyCryptoBackend, be)
}
// HasCryptoBackend returns true if a value for crypto backend has been set in the context.
func HasCryptoBackend(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyCryptoBackend).(CryptoBackend)
return ok
}
// GetCryptoBackend returns the selected crypto backend or the default (GPGCLI).
func GetCryptoBackend(ctx context.Context) CryptoBackend {
be, ok := ctx.Value(ctxKeyCryptoBackend).(CryptoBackend)
if !ok {
return GPGCLI
}
return be
}
// WithStorageBackendString returns a context with the given store backend set.
func WithStorageBackendString(ctx context.Context, sb string) context.Context {
if be, err := StorageRegistry.Backend(sb); err == nil {
return WithStorageBackend(ctx, be)
}
return WithStorageBackend(ctx, FS)
}
// WithStorageBackend returns a context with the given store backend set.
func WithStorageBackend(ctx context.Context, sb StorageBackend) context.Context {
return context.WithValue(ctx, ctxKeyStorageBackend, sb)
}
// GetStorageBackend returns the store backend or the default (FS).
func GetStorageBackend(ctx context.Context) StorageBackend {
be, ok := ctx.Value(ctxKeyStorageBackend).(StorageBackend)
if !ok {
return FS
}
return be
}
// HasStorageBackend returns true if a value for store backend was set.
func HasStorageBackend(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyStorageBackend).(StorageBackend)
return ok
}
// StorageBackendName returns the name of the given backend.
func StorageBackendName(sb StorageBackend) string {
if name, err := StorageRegistry.BackendName(sb); err == nil {
return name
}
return ""
}