Read config file path from commandline

This commit is contained in:
Thomas Roehl 2021-05-11 12:41:29 +02:00
parent 40265726ed
commit 4ee4e617b2

View File

@ -11,6 +11,7 @@ import (
"strings"
"sync"
"time"
"flag"
)
// List of provided collectors. Which collector should be run can be
@ -51,12 +52,19 @@ func LoadConfiguration(file string, config *GlobalConfig) error {
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
return err
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(config)
return err
}
func ReadCli() string {
cfg := flag.String("config", "./config.json", "Path to configuration file")
flag.Parse()
return *cfg
}
// Register an interrupt handler for Ctrl+C and similar. At signal,
// all collectors are closed
func shutdown(wg *sync.WaitGroup, config *GlobalConfig, sink sinks.SinkFuncs) {
@ -86,9 +94,14 @@ func main() {
log.Print(err)
return
}
configfile := ReadCli()
// Load and check configuration
LoadConfiguration("config.json", &config)
err = LoadConfiguration(configfile, &config)
if (err != nil) {
log.Print("Error reading configuration file ", configfile)
return
}
if config.Interval <= 0 || time.Duration(config.Interval)*time.Second <= 0 {
log.Print("Configuration value 'interval' must be greater than zero")
return