You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.1 KiB
65 lines
1.1 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
toml "github.com/pelletier/go-toml"
|
|
"io/ioutil"
|
|
"os"
|
|
)
|
|
|
|
type ESConfiguration struct {
|
|
Elastic ElasticConfiguration
|
|
}
|
|
|
|
type ElasticConfiguration struct {
|
|
URL string
|
|
User string
|
|
Pass string
|
|
}
|
|
|
|
func LoadConfig(cfgpath string) *ESConfiguration {
|
|
cfg := ESConfiguration{}
|
|
if cfgpath != "" {
|
|
cfgfile, err := ioutil.ReadFile(cfgpath)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "read error: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
err = toml.Unmarshal(cfgfile, &cfg)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "parse error: %s\n", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
return &cfg
|
|
}
|
|
|
|
paths := []string{}
|
|
homedir, _ := os.UserHomeDir()
|
|
if homedir != "" {
|
|
paths = append(paths, homedir+"/.es.toml")
|
|
}
|
|
paths = append(paths, "/etc/es.toml")
|
|
|
|
for _, path := range paths {
|
|
cfgfile, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
err = toml.Unmarshal(cfgfile, &cfg)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
return &cfg
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "no valid configuration found in:\n")
|
|
for _, path := range paths {
|
|
fmt.Fprintf(os.Stderr, " %s\n", path)
|
|
}
|
|
os.Exit(1)
|
|
return nil
|
|
}
|