/
config.go
68 lines (60 loc) · 1.5 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
package config
import (
"flag"
"os"
"gopkg.in/yaml.v2"
)
// Flags are the command line Flags
type Flags struct {
Config string
Debug bool
}
// Config contains the njmon_exporter configuration data
type Config struct {
OutFile string `yaml:"outfile"`
API struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
CertFile string `yaml:"certfile"`
URL string `yaml:"url"`
} `yaml:"api"`
Logging struct {
Journal bool `yaml:"journal"`
LevelStr string `yaml:"level"`
} `yaml:"logging"`
}
// ParseConfig imports a yaml formatted config file into a Config struct
func ParseConfig(filename string) (*Config, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
config := &Config{}
d := yaml.NewDecoder(file)
if err := d.Decode(&config); err != nil {
return nil, err
}
return config, nil
}
// WriteConfig will create a YAML formatted config file from a Config struct
func (c *Config) WriteConfig(filename string) error {
data, err := yaml.Marshal(c)
if err != nil {
return err
}
err = os.WriteFile(filename, data, 0644)
if err != nil {
return err
}
return nil
}
// parseFlags processes arguments passed on the command line in the format
// standard format: --foo=bar
func ParseFlags() *Flags {
f := new(Flags)
flag.StringVar(&f.Config, "config", "pgome.yml", "Path to pgome configuration file")
flag.BoolVar(&f.Debug, "debug", false, "Expand logging with Debug level messaging and format")
flag.Parse()
return f
}