-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
191 lines (164 loc) · 4.38 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"bitbucket.org/kardianos/osext"
"encoding/json"
"fmt"
"github.com/docopt/docopt-go"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
)
const configName = "gondl.json"
// Type config holds a key=value configuration map
type Config struct {
Values map[string]interface{}
}
// NewConfig returns a ready to work with configuration object
func NewConfig(usage string, args []string, version string) Config {
config := Config{}
config.init(usage, args, version)
config.ammend(workingDirConfig())
config.ammend(homeDirConfig())
config.ammend(localDirConfig())
config.fill("--cachedir", "./cache")
config.fill("--cache", "240")
config.fill("--page", "1")
config.fill("--per_page", "300")
config.fill("--format", "csv")
return config
}
// init loads values from the docopt into the config object
func (c *Config) init(usage string, args []string, version string) {
c.Values, _ = docopt.Parse(usage, args, true, version, false)
}
// fill populates config fields with values, if they are nil
func (c *Config) fill(key, value string) {
if c.Values[key] == nil {
c.Values[key] = value
}
}
// ammend reads a config file and adds its vales to the config
// object. Formerly existing values will prevail.
func (c *Config) ammend(path string) {
loaded := loadConfig(path)
c.Values = merge(c.Values, loaded)
}
// loadConfig loads a JSON config file if available
func loadConfig(filename string) map[string]interface{} {
var result map[string]interface{}
jsonData, err := ioutil.ReadFile(filename)
if err != nil {
return result
}
err = json.Unmarshal(jsonData, &result)
if err != nil {
log.Fatalf("Error in %v:\n%v", filename, err.Error())
}
return result
}
// merge combines two Configs.
// truthiness takes priority over falsiness
// mapA takes priority over mapB
func merge(mapA, mapB map[string]interface{}) map[string]interface{} {
result := make(map[string]interface{})
for k, v := range mapA {
result[k] = v
}
for k, v := range mapB {
if _, ok := result[k]; !ok || result[k] == nil || result[k] == false {
result[k] = v
}
}
return result
}
// localDirConfig returns the path to the local config file
func localDirConfig() string {
// dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
dir, err := osext.ExecutableFolder()
panicon(err)
return dir + configName
}
// workingDirConfig returns the path to the working directory config
// file
func workingDirConfig() string {
dir, err := os.Getwd()
panicon(err)
return dir + string(filepath.Separator) + configName
}
// homeDirConfig returns the path to the user home directory config
// file
func homeDirConfig() string {
usr, err := user.Current()
panicon(err)
return usr.HomeDir + string(filepath.Separator) + configName
}
// makeConfig creates a sample gondl.json template file, if it
// doesn't already exist
func makeConfig() {
file := workingDirConfig()
msg := "Sample config file created here"
if exist(file) {
msg = "Config file already exists"
} else {
err := ioutil.WriteFile(file, []byte(configTemplate), 0644)
if err != nil {
msg = "Error - cannot create config file"
}
}
fmt.Printf(configHelp, msg, file)
}
// showConfig shows information about the config files.
func showConfig() {
f1, f2, f3 := "Not Found", "Not Found", "Not Found"
if exist(workingDirConfig()) {
f1 = "Found"
}
if exist(homeDirConfig()) {
f2 = "Found"
}
if exist(localDirConfig()) {
f3 = "Found"
}
fmt.Printf(configInfo,
f1, workingDirConfig(),
f2, homeDirConfig(),
f3, localDirConfig(),
)
}
func exist(file string) bool {
if _, err := os.Stat(file); err == nil {
return true
}
return false
}
const configTemplate = `{
"--apikey": "YOUR_KEY",
"--trim_start": "2014-01-01",
"--per_page": "10",
"--url": true
}
`
const configHelp = `%v:
%v
You may edit it and use any of the long-form options (--options) in
it. The arguments you provide in the command line override any argument
in any of the config files.
Run 'gondl --config' for more information about config files.
`
const configInfo = `
Gondl will look for config files in three folders. The working
directory, the user's home directory and the local (executable's)
directory.
Values in the working directory will have precedence over values in
the home directory, and values in the home directory
will have precedence over values in the local directory.
Working Directory: (%v)
%v
User Directory: (%v)
%v
Local Directory: (%v)
%v
`
//;D