-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
52 lines (43 loc) · 1.29 KB
/
Copy pathconfig.go
File metadata and controls
52 lines (43 loc) · 1.29 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
// Package config provides a type that represent configuration
package main
import (
"errors"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
"os"
)
// Config defines configuration
type Config struct {
Fields []string `yaml:"fields" mapstructure:"fields"` // Fields to print
Skips []string `yaml:"skips" mapstructure:"skips"` // Fields to skip
JsonFields []string `yaml:"jsonFields" mapstructure:"jsonFields"` // Fields to print in JSON format
Color bool `yaml:"color" mapstructure:"color"` // Enable color
}
// LoadConfig loads configuration from file, env and flags and return compiled and validated config
func LoadConfig() (*Config, error) {
v := viper.New()
dirname, err := os.UserHomeDir()
if err != nil {
log.Fatal(err)
}
f := flag.CommandLine
f.StringP("config", "c", dirname+"/.logparse/config.yaml", "The configuration file to use to configure this application")
flag.Parse()
configFile, err := f.GetString("config")
if err != nil {
exit(err, 1)
}
if configFile == "" {
return nil, errors.New("missing config")
}
v.SetConfigFile(configFile)
if err := v.ReadInConfig(); err != nil {
return nil, err
}
var config Config
if err := v.Unmarshal(&config); err != nil {
exit(err, 1)
}
return &config, nil
}