forked from gopasspw/gopass
/
context.go
107 lines (89 loc) · 3.16 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
package backend
import "context"
type contextKey int
const (
ctxKeyCryptoBackend contextKey = iota
ctxKeyRCSBackend
ctxKeyStorageBackend
)
// CryptoBackendName returns the name of the given backend
func CryptoBackendName(cb CryptoBackend) string {
return cryptoNameFromBackend(cb)
}
// WithCryptoBackendString returns a context with the given crypto backend set
func WithCryptoBackendString(ctx context.Context, be string) context.Context {
if cb := cryptoBackendFromName(be); cb >= 0 {
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
}
// RCSBackendName returns the name of the given backend
func RCSBackendName(sb RCSBackend) string {
return rcsNameFromBackend(sb)
}
// WithRCSBackendString returns a context with the given sync backend set
func WithRCSBackendString(ctx context.Context, sb string) context.Context {
if be := rcsBackendFromName(sb); be >= 0 {
return WithRCSBackend(ctx, be)
}
return WithRCSBackend(ctx, Noop)
}
// WithRCSBackend returns a context with the given sync backend set
func WithRCSBackend(ctx context.Context, sb RCSBackend) context.Context {
return context.WithValue(ctx, ctxKeyRCSBackend, sb)
}
// HasRCSBackend returns true if a value for sync backend has been set in the context
func HasRCSBackend(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyRCSBackend).(RCSBackend)
return ok
}
// GetRCSBackend returns the sync backend or the default (Git Mock)
func GetRCSBackend(ctx context.Context) RCSBackend {
be, ok := ctx.Value(ctxKeyRCSBackend).(RCSBackend)
if !ok {
return Noop
}
return be
}
// WithStorageBackendString returns a context with the given store backend set
func WithStorageBackendString(ctx context.Context, sb string) context.Context {
return WithStorageBackend(ctx, storageBackendFromName(sb))
}
// 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 {
return storageNameFromBackend(sb)
}