forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
159 lines (140 loc) · 3.63 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package config
import (
"fmt"
"os"
"path/filepath"
"sort"
"github.com/pkg/errors"
)
var (
// ErrConfigNotFound is returned on load if the config was not found
ErrConfigNotFound = errors.Errorf("config not found")
// ErrConfigNotParsed is returned on load if the config could not be decoded
ErrConfigNotParsed = errors.Errorf("config not parseable")
debug = false
)
func init() {
if gdb := os.Getenv("GOPASS_DEBUG"); gdb != "" {
debug = true
}
}
// Config is the current config struct
type Config struct {
Path string `yaml:"-"`
Root *StoreConfig `yaml:"root"`
Mounts map[string]*StoreConfig `yaml:"mounts"`
// Catches all undefined files and must be empty after parsing
XXX map[string]interface{} `yaml:",inline"`
}
// New creates a new config with sane default values
func New() *Config {
return &Config{
Path: configLocation(),
Root: &StoreConfig{
AskForMore: false,
AutoClip: true,
AutoImport: true,
AutoSync: true,
ClipTimeout: 45,
Concurrency: 1,
NoColor: false,
NoConfirm: false,
NoPager: false,
SafeContent: false,
UseSymbols: false,
Notifications: true,
},
Mounts: make(map[string]*StoreConfig),
}
}
// CheckOverflow implements configer. It will check for any extra config values not
// handled by the current struct
func (c *Config) CheckOverflow() error {
return checkOverflow(c.XXX, "config")
}
// Config will return a current config
func (c *Config) Config() *Config {
return c
}
// SetConfigValue will try to set the given key to the value in the config struct
func (c *Config) SetConfigValue(mount, key, value string) error {
if mount == "" {
if err := c.Root.SetConfigValue(key, value); err != nil {
return err
}
return c.Save()
}
if sc, found := c.Mounts[mount]; found {
if err := sc.SetConfigValue(key, value); err != nil {
return err
}
}
return c.Save()
}
func (c *Config) checkDefaults() error {
if c == nil {
return fmt.Errorf("config is nil")
}
if c.Root == nil {
c.Root = &StoreConfig{}
}
if err := c.Root.checkDefaults(); err != nil {
return err
}
for _, sc := range c.Mounts {
if err := sc.checkDefaults(); err != nil {
return err
}
}
return nil
}
func (c *Config) String() string {
mounts := ""
keys := make([]string, 0, len(c.Mounts))
for alias := range c.Mounts {
keys = append(keys, alias)
}
sort.Strings(keys)
for _, alias := range keys {
sc := c.Mounts[alias]
mounts += alias + "=>" + sc.String()
}
return fmt.Sprintf("Config[Root:%s,Mounts(%s)]", c.Root.String(), mounts)
}
// Directory returns the directory this config is using
func (c *Config) Directory() string {
return filepath.Dir(c.Path)
}
// GetRecipientHash returns the recipients hash for the given store and file
func (c *Config) GetRecipientHash(alias, name string) string {
if alias == "" {
return c.Root.RecipientHash[name]
}
if sc, found := c.Mounts[alias]; found && sc != nil {
return sc.RecipientHash[name]
}
return ""
}
// SetRecipientHash will set and save the recipient hash for the given store
// and file
func (c *Config) SetRecipientHash(alias, name, value string) error {
if alias == "" {
c.Root.setRecipientHash(name, value)
} else {
if sc, found := c.Mounts[alias]; found && sc != nil {
sc.setRecipientHash(name, value)
}
}
return c.Save()
}
// CheckRecipientHash returns true if we should report/fail on any
// recipient hash errors for this store
func (c *Config) CheckRecipientHash(alias string) bool {
if alias == "" {
return c.Root.CheckRecpHash
}
if sc, found := c.Mounts[alias]; found && sc != nil {
return sc.CheckRecpHash
}
return false
}