-
-
Notifications
You must be signed in to change notification settings - Fork 480
/
store_config.go
137 lines (128 loc) · 4.4 KB
/
store_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
package config
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/gopasspw/gopass/pkg/backend"
"github.com/pkg/errors"
)
// StoreConfig is a per-store (root or mount) config
type StoreConfig struct {
AskForMore bool `yaml:"askformore"` // ask for more data on generate
AutoClip bool `yaml:"autoclip"` // decide whether passwords are automatically copied or not
AutoPrint bool `yaml:"autoprint"` // decide whether passwords are automatically printed or not
AutoImport bool `yaml:"autoimport"` // import missing public keys w/o asking
AutoSync bool `yaml:"autosync"` // push to git remote after commit, pull before push if necessary
CheckRecpHash bool `yaml:"check_recipient_hash"`
ClipTimeout int `yaml:"cliptimeout"` // clear clipboard after seconds
Concurrency int `yaml:"concurrency"` // allow to run multiple thread when batch processing
EditRecipients bool `yaml:"editrecipients"` // edit recipients when confirming
NoColor bool `yaml:"nocolor"` // do not use color when outputing text
NoConfirm bool `yaml:"noconfirm"` // do not confirm recipients when encrypting
NoPager bool `yaml:"nopager"` // do not invoke a pager to display long lists
Notifications bool `yaml:"notifications"` // enable desktop notifications
Path *backend.URL `yaml:"path"` // path to the root store
RecipientHash map[string]string `yaml:"recipient_hash"`
SafeContent bool `yaml:"safecontent"` // avoid showing passwords in terminal
UseSymbols bool `yaml:"usesymbols"` // always use symbols when generating passwords
}
func (c *StoreConfig) checkDefaults() error {
if c == nil {
return nil
}
if c.Path == nil {
c.Path = backend.FromPath("")
}
if c.Concurrency == 0 {
c.Concurrency = 1
}
if c.RecipientHash == nil {
c.RecipientHash = make(map[string]string, 1)
}
return nil
}
// ConfigMap returns a map of stringified config values for easy printing
func (c *StoreConfig) ConfigMap() map[string]string {
m := make(map[string]string, 20)
o := reflect.ValueOf(c).Elem()
for i := 0; i < o.NumField(); i++ {
jsonArg := o.Type().Field(i).Tag.Get("yaml")
if jsonArg == "" || jsonArg == "-" {
continue
}
f := o.Field(i)
var strVal string
switch f.Kind() {
case reflect.String:
strVal = f.String()
case reflect.Bool:
strVal = fmt.Sprintf("%t", f.Bool())
case reflect.Int:
strVal = fmt.Sprintf("%d", f.Int())
case reflect.Ptr:
switch bup := f.Interface().(type) {
case *backend.URL:
if bup == nil {
continue
}
strVal = bup.String()
}
default:
continue
}
m[jsonArg] = strVal
}
return m
}
// SetConfigValue will try to set the given key to the value in the config struct
func (c *StoreConfig) SetConfigValue(key, value string) error {
if key != "path" {
value = strings.ToLower(value)
}
o := reflect.ValueOf(c).Elem()
for i := 0; i < o.NumField(); i++ {
jsonArg := o.Type().Field(i).Tag.Get("yaml")
if jsonArg == "" || jsonArg == "-" {
continue
}
if jsonArg != key {
continue
}
f := o.Field(i)
switch f.Kind() {
case reflect.String:
f.SetString(value)
return nil
case reflect.Bool:
if value == "true" {
f.SetBool(true)
return nil
} else if value == "false" {
f.SetBool(false)
return nil
} else {
return errors.Errorf("not a bool: %s", value)
}
case reflect.Int:
iv, err := strconv.Atoi(value)
if err != nil {
return errors.Wrapf(err, "failed to convert '%s' to int", value)
}
f.SetInt(int64(iv))
return nil
default:
continue
}
}
return errors.New("unknown config option")
}
func (c *StoreConfig) String() string {
return fmt.Sprintf("StoreConfig[AskForMore:%t,AutoClip:%t,AutoImport:%t,AutoSync:%t,ClipTimeout:%d,Concurrency:%d,EditRecipients:%t,NoColor:%t,NoConfirm:%t,NoPager:%t,Notifications:%t,Path:%s,SafeContent:%t,UseSymbols:%t]", c.AskForMore, c.AutoClip, c.AutoImport, c.AutoSync, c.ClipTimeout, c.Concurrency, c.EditRecipients, c.NoColor, c.NoConfirm, c.NoPager, c.Notifications, c.Path, c.SafeContent, c.UseSymbols)
}
func (c *StoreConfig) setRecipientHash(name, value string) {
if c.RecipientHash == nil {
c.RecipientHash = make(map[string]string, 1)
}
c.RecipientHash[name] = value
}