forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
175 lines (162 loc) · 3.79 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package action
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"github.com/ghodss/yaml"
"github.com/justwatchcom/gopass/fsutil"
"github.com/justwatchcom/gopass/password"
"github.com/urfave/cli"
)
// Config handles changes to the gopass configuration
func (s *Action) Config(c *cli.Context) error {
if len(c.Args()) < 1 {
return s.printConfigValues()
}
if len(c.Args()) == 1 {
return s.printConfigValues(c.Args()[0])
}
if len(c.Args()) > 2 {
return fmt.Errorf("Usage: gopass config key value")
}
return s.setConfigValue(c.Args()[0], c.Args()[1])
}
func (s *Action) printConfigValues(filter ...string) error {
out := make([]string, 0, 10)
o := reflect.ValueOf(s.Store).Elem()
for i := 0; i < o.NumField(); i++ {
jsonArg := o.Type().Field(i).Tag.Get("json")
if jsonArg == "" || jsonArg == "-" {
continue
}
if !contains(filter, jsonArg) {
continue
}
f := o.Field(i)
strVal := ""
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())
default:
continue
}
out = append(out, fmt.Sprintf("%s: %s", jsonArg, strVal))
}
sort.Strings(out)
for _, line := range out {
fmt.Println(line)
}
return nil
}
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(key, value string) error {
if key == "version" {
return fmt.Errorf("Can not change version")
}
if key != "path" {
value = strings.ToLower(value)
}
o := reflect.ValueOf(s.Store).Elem()
for i := 0; i < o.NumField(); i++ {
jsonArg := o.Type().Field(i).Tag.Get("json")
if jsonArg == "" || jsonArg == "-" {
continue
}
if jsonArg != key {
continue
}
f := o.Field(i)
switch f.Kind() {
case reflect.String:
f.SetString(value)
case reflect.Bool:
if value == "true" {
f.SetBool(true)
} else if value == "false" {
f.SetBool(false)
} else {
return fmt.Errorf("No a bool: %s", value)
}
case reflect.Int:
iv, err := strconv.Atoi(value)
if err != nil {
return err
}
f.SetInt(int64(iv))
default:
continue
}
}
return writeConfig(s.Store)
}
// hasConfig is a short hand for checking if the config file exists
func hasConfig() bool {
for _, l := range configLocations() {
if fsutil.IsFile(l) {
return true
}
}
return false
}
// writeConfig saves the config
func writeConfig(s *password.RootStore) error {
buf, err := yaml.Marshal(s)
if err != nil {
return err
}
cfgLoc := configLocation()
cfgDir := filepath.Dir(cfgLoc)
if !fsutil.IsDir(cfgDir) {
if err := os.MkdirAll(cfgDir, 0700); err != nil {
return err
}
}
if err := ioutil.WriteFile(cfgLoc, buf, 0600); err != nil {
return err
}
return nil
}
// configLocation returns the location of the config file. Either reading from
// GOPASS_CONFIG or using the default location (~/.gopass.yml)
func configLocation() string {
if cf := os.Getenv("GOPASS_CONFIG"); cf != "" {
return cf
}
if xch := os.Getenv("XDG_CONFIG_HOME"); xch != "" {
return filepath.Join(xch, "gopass", "config.yml")
}
return filepath.Join(os.Getenv("HOME"), ".config", "gopass", "config.yml")
}
// configLocations returns the possible locations of gopass config files,
// in decreasing priority
func configLocations() []string {
l := []string{}
if cf := os.Getenv("GOPASS_CONFIG"); cf != "" {
l = append(l, cf)
}
if xch := os.Getenv("XDG_CONFIG_HOME"); xch != "" {
l = append(l, filepath.Join(xch, "gopass", "config.yml"))
}
l = append(l, filepath.Join(os.Getenv("HOME"), ".config", "gopass", "config.yml"))
l = append(l, filepath.Join(os.Getenv("HOME"), ".gopass.yml"))
return l
}