-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
context.go
44 lines (33 loc) · 982 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
42
43
44
package config
import (
"context"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/gitconfig"
)
type contextKey int
const (
ctxKeyConfig contextKey = iota
ctxKeyMountPoint
)
func (c *Config) WithConfig(ctx context.Context) context.Context {
return context.WithValue(ctx, ctxKeyConfig, c)
}
func WithMount(ctx context.Context, mp string) context.Context {
return context.WithValue(ctx, ctxKeyMountPoint, mp)
}
// FromContext returns a config from a context, as well as the current mount point (store name) if found.
func FromContext(ctx context.Context) (*Config, string) {
mount := ""
if m, found := ctx.Value(ctxKeyMountPoint).(string); found && m != "" {
mount = m
}
if c, found := ctx.Value(ctxKeyConfig).(*Config); found && c != nil {
return c, mount
}
debug.Log("no config in context, loading anew")
cfg := &Config{
root: newGitconfig().LoadAll(""),
}
cfg.root.Preset = gitconfig.NewFromMap(defaults)
return cfg, mount
}