forked from digitalocean/ceph_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
44 lines (36 loc) · 875 Bytes
/
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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
type ClusterConfig struct {
ClusterLabel string `yaml:"cluster_label"`
User string `yaml:"user"`
ConfigFile string `yaml:"config_file"`
}
// Config is the top-level configuration for Metastord.
type Config struct {
Cluster []*ClusterConfig
}
// fileExists returns true if the path exists and is a file.
func fileExists(path string) bool {
stat, err := os.Stat(path)
return !os.IsNotExist(err) && !stat.IsDir()
}
func ParseConfig(p string) (*Config, error) {
if !fileExists(p) {
return nil, fmt.Errorf("Config file does not exist.")
}
cfgData, err := ioutil.ReadFile(p)
if err != nil {
return nil, fmt.Errorf("ReadFile: %v", err)
}
var cfg Config
err = yaml.Unmarshal(cfgData, &cfg)
if err != nil {
return nil, fmt.Errorf("yaml parse: %v", err)
}
return &cfg, nil
}