-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.go
71 lines (58 loc) · 1.49 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"strings"
)
type database struct {
AppServer string
DbServer string
DbName string
User string
Pass string
SQLType string
}
func mustReadDatabasesConfigFile() map[string]database {
var paths []string
databases := map[string]database{}
usr, err := user.Current()
if err != nil {
usage("Couldn't obtain the current user err=%v", err)
}
home := usr.HomeDir
xdgHome := os.Getenv("XDG_CONFIG_HOME")
if xdgHome == "" {
xdgHome = fmt.Sprintf("%v/.config/", home)
}
xdgHome += "sql/.databases.json"
paths = append(paths, xdgHome)
xdgConfigDirs := strings.Split(os.Getenv("XDG_CONFIG_DIRS"), ":")
xdgConfigDirs = append(xdgConfigDirs, "/etc/xdg")
for _, d := range xdgConfigDirs {
if d != "" {
paths = append(paths, fmt.Sprintf("%v/sql/.databases.json", d))
}
}
paths = append(paths, fmt.Sprintf("%v/.databases.json", home))
var byts []byte
for _, p := range paths {
if byts, err = ioutil.ReadFile(p); err != nil {
continue
}
break
}
if err != nil {
usage("Couldn't find .databases.json in the following paths [%v]. err=%v", paths, err)
}
err = json.Unmarshal(byts, &databases)
if err != nil {
usage("Found but couldn't JSON unmarshal .databases.json. Looked like this:\n\n%v\n\nerr=%v", string(byts), err)
}
if len(databases) == 0 {
usage("Couldn't find any database configurations on .databases.json. Looked like this:\n\n%v\n", string(byts))
}
return databases
}