-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
122 lines (111 loc) · 2.67 KB
/
config.go
File metadata and controls
122 lines (111 loc) · 2.67 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/BurntSushi/toml"
"github.com/olivere/elastic/v7"
)
type profiles struct {
Profile []EsConfig
}
type EsConfig struct {
Name string
Address []string
Sniff bool
Username string
Password string
}
func setConfigFilePath(runtimeOS string) string {
// set config file by OS.
// windows: $APPDATA\cles\
// unix-like: $HOME/.config/cles/
var dir string
if runtimeOS == "windows" {
dir = os.Getenv("APPDATA")
if dir == "" {
dir = filepath.Join(os.Getenv("USERPROFILE"), "Application Data")
}
} else {
dir = filepath.Join(os.Getenv("HOME"), ".config")
}
return filepath.Join(dir, "cles", "config.toml")
}
func loadConfigFromFile(path string, profile string) (*EsConfig, error) {
f, err := ioutil.ReadFile(path)
if err == nil {
profiles := &profiles{}
err := toml.Unmarshal(f, profiles)
if err != nil {
return nil, err
}
for _, prof := range profiles.Profile {
if prof.Name == profile {
cfg := EsConfig{
Name: prof.Name,
Address: prof.Address,
Username: prof.Username,
Password: prof.Password,
Sniff: prof.Sniff,
}
return &cfg, nil
}
}
}
return nil, nil
}
func loadConfig(cfgPath string, profileName string) (*EsConfig, error) {
// default value
cfg := EsConfig{
Name: profileName,
Address: []string{"http://127.0.0.1:9200"},
Username: "",
Password: "",
Sniff: false,
}
fcfg, err := loadConfigFromFile(cfgPath, profileName)
if err != nil {
return nil, err
}
if fcfg != nil {
cfg = *fcfg
}
// overwrite with environment variables
if len(os.Getenv("ES_ADDRESS")) > 0 {
cfg.Address = strings.Split(os.Getenv("ES_ADDRESS"), ",")
}
if len(os.Getenv("ES_USERNAME")) > 0 {
cfg.Username = os.Getenv("ES_USERNAME")
}
if len(os.Getenv("ES_PASSWORD")) > 0 {
cfg.Password = os.Getenv("ES_PASSWORD")
}
if len(os.Getenv("ES_SNIFF")) > 0 {
cfg.Sniff = strings.ToLower(os.Getenv("ES_SNIFF")) == "true"
}
return &cfg, nil
}
func initClient(profile string, debugFn func(message string)) (*elastic.Client, error) {
cfgPath := setConfigFilePath(runtime.GOOS)
debugFn(fmt.Sprintf("config file path: %s", cfgPath))
cfg, err := loadConfig(cfgPath, profile)
if err != nil {
return nil, err
}
debugFn(fmt.Sprintf("URL : %v", cfg.Address))
debugFn(fmt.Sprintf("USER : %s", cfg.Username))
debugFn(fmt.Sprintf("PASS : %s", cfg.Password))
debugFn(fmt.Sprintf("SNIFF: %v", cfg.Sniff))
client, err := elastic.NewClient(
elastic.SetURL(cfg.Address...),
elastic.SetBasicAuth(cfg.Username, cfg.Password),
elastic.SetSniff(cfg.Sniff),
)
if err != nil {
return nil, err
}
return client, nil
}