-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
config.go
139 lines (121 loc) · 3.15 KB
/
config.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
package action
import (
"context"
"fmt"
"sort"
"github.com/gopasspw/gopass/internal/backend/storage/ondisk"
"github.com/gopasspw/gopass/internal/debug"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
// Config handles changes to the gopass configuration
func (s *Action) Config(c *cli.Context) error {
ctx := ctxutil.WithGlobalFlags(c)
if c.Args().Len() < 1 {
s.printConfigValues(ctx)
return nil
}
if c.Args().Len() == 1 {
s.printConfigValues(ctx, c.Args().Get(0))
return nil
}
if c.Args().Len() > 2 {
return ExitError(ExitUsage, nil, "Usage: %s config key value", s.Name)
}
if err := s.setConfigValue(ctx, c.String("store"), c.Args().Get(0), c.Args().Get(1)); err != nil {
return ExitError(ExitUnknown, err, "Error setting config value")
}
return nil
}
func (s *Action) printConfigValues(ctx context.Context, needles ...string) {
m := s.cfg.ConfigMap()
for _, k := range filterMap(m, needles) {
out.Print(ctx, "%s: %s", k, m[k])
}
for alias, path := range s.cfg.Mounts {
if len(needles) < 1 {
out.Print(ctx, "mount '%s' => '%s'", alias, path)
}
storage, ok := s.getOnDiskStorage(ctx, alias)
if !ok {
// not ondisk
continue
}
rcfg, err := storage.GetRemote(ctx)
if err != nil {
debug.Log("failed to read remote config: %s", err)
continue
}
out.Print(ctx, " remote: %s", rcfg)
}
}
func filterMap(haystack map[string]string, needles []string) []string {
out := make([]string, 0, len(haystack))
for k := range haystack {
if !contains(needles, k) {
continue
}
out = append(out, k)
}
sort.Strings(out)
return out
}
func contains(haystack []string, needle string) bool {
if len(haystack) < 1 {
return true
}
for _, blade := range haystack {
if blade == needle {
return true
}
}
return false
}
func (s *Action) setConfigValue(ctx context.Context, store, key, value string) error {
if key == "remote" && value != "" && store != "" {
return s.setRemoteConfig(ctx, store, value)
}
if err := s.cfg.SetConfigValue(key, value); err != nil {
return errors.Wrapf(err, "failed to set config value '%s'", key)
}
s.printConfigValues(ctx, key)
return nil
}
func (s *Action) getOnDiskStorage(ctx context.Context, store string) (*ondisk.OnDisk, bool) {
_, sub, err := s.Store.GetSubStore(ctx, store)
if err != nil {
debug.Log("failed to get sub store: %s", err)
return nil, false
}
storage, ok := sub.Storage().(*ondisk.OnDisk)
if !ok {
return nil, false
}
return storage, true
}
func (s *Action) setRemoteConfig(ctx context.Context, store, urlStr string) error {
storage, ok := s.getOnDiskStorage(ctx, store)
if !ok {
debug.Log("setting remote not supported")
return nil
}
return storage.SetRemote(ctx, urlStr)
}
func (s *Action) configKeys() []string {
cm := s.cfg.ConfigMap()
keys := make([]string, 0, len(cm)+1)
for k := range cm {
keys = append(keys, k)
}
keys = append(keys, "remote")
sort.Strings(keys)
return keys
}
// ConfigComplete will print the list of valid config keys
func (s *Action) ConfigComplete(c *cli.Context) {
for _, k := range s.configKeys() {
fmt.Fprintln(stdout, k)
}
}