-
Notifications
You must be signed in to change notification settings - Fork 0
/
kksecret.go
80 lines (66 loc) · 1.47 KB
/
kksecret.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
package kksecret
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
var PATH = "/the-secret/"
type Database struct {
Name string
Path string
Writer DatabaseMeta `json:"writer"`
Reader DatabaseMeta `json:"reader"`
}
type Redis struct {
Name string
Path string
Master RedisMeta `json:"master"`
Slave RedisMeta `json:"slave"`
}
type DatabaseMeta struct {
Adapter string
Params struct {
Charset string `json:"charset"`
Host string `json:"host"`
DBName string `json:"dbname"`
Username string `json:"username"`
Password string `json:"password"`
} `json:"params"`
}
type RedisMeta struct {
Host string `json:"host"`
Port uint `json:"port"`
}
func DatabaseProfile(dbname string) *Database {
path := fmt.Sprintf("%sdb-%s/db.json", PATH, dbname)
if _, e := os.Stat(path); os.IsNotExist(e) {
return nil
}
if bytes, err := ioutil.ReadFile(path); err == nil {
var database []Database
if err := json.Unmarshal(bytes, &database); err != nil {
return nil
}
database[0].Name = dbname
database[0].Path = path
return &database[0]
}
return nil
}
func RedisProfile(redisName string) *Redis {
path := fmt.Sprintf("%sredis-%s/redis.json", PATH, redisName)
if _, e := os.Stat(path); os.IsNotExist(e) {
return nil
}
if bytes, err := ioutil.ReadFile(path); err == nil {
var redis = &Redis{}
if err := json.Unmarshal(bytes, redis); err != nil {
return nil
}
redis.Name = redisName
redis.Path = path
return redis
}
return nil
}