/
config.go
91 lines (73 loc) · 1.65 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
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
"github.com/BurntSushi/toml"
)
var KlientToml string = ".klient.toml"
var KlientConfig Config
var KafkaDefaultGroup = "test-consumer-group"
type Config struct {
Mysql CommonConfigSection `toml:"mysql"`
Pgsql CommonConfigSection `toml:"pgsql"`
Redis CommonConfigSection `toml:"redis"`
Clickhouse CommonConfigSection `toml:"clickhouse"`
Kafka KafkaConfigSection `toml:"kafka"`
}
type CommonConfigSection struct {
Host string
Port string
User string
Pswd string
Db string
}
type KafkaConfigSection struct {
Brokers []string
Topics []string
Group string
User string
Pswd string
}
func LoadConfig() {
user, _ := user.Current()
dst := filepath.Join(user.HomeDir, KlientToml)
f, _ := os.OpenFile(dst, os.O_CREATE|os.O_RDONLY, os.ModePerm|os.ModeTemporary)
defer f.Close()
_, err := toml.DecodeReader(f, &KlientConfig)
if err != nil {
panic(k_red(err))
} else {
//DEBUG.Printf("%s: %+v \n", dst, KlientConfig)
}
return
}
func SaveConfig() error {
buf := new(bytes.Buffer)
err := toml.NewEncoder(buf).Encode(KlientConfig)
if err != nil {
log.Fatal(err)
}
//fmt.Println(buf.String())
user, _ := user.Current()
dst := filepath.Join(user.HomeDir, KlientToml)
err = ioutil.WriteFile(dst, buf.Bytes(), 0644)
if err != nil {
panic(k_red(err))
}
/*
f, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE, os.ModePerm|os.ModeTemporary)
if err != nil {
panic(k_red(err))
}
defer f.Close()
fmt.Println(KlientConfig)
if err = toml.NewEncoder(f).Encode(KlientConfig); err != nil {
panic(k_red(err))
}
*/
return err
}