66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"flag"
|
||
|
"log"
|
||
|
"os"
|
||
|
|
||
|
"gopkg.in/ini.v1"
|
||
|
)
|
||
|
|
||
|
// ParseArgs is not yet implemented
|
||
|
func ParseArgs() error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// GetConfig fetch config from ini file
|
||
|
func GetConfig(configfile string, weatherconfig *WeatherConfig) error {
|
||
|
flag.Usage = Usage
|
||
|
|
||
|
config, err := ini.Load(configfile)
|
||
|
HandleFatalError(err)
|
||
|
|
||
|
var wc WeatherConfig
|
||
|
|
||
|
owmSection := config.Section("owm")
|
||
|
wc.OwmURL = owmSection.Key("url").String()
|
||
|
wc.OwmAppID = owmSection.Key("appid").String()
|
||
|
wc.OwmCities = owmSection.Key("cities").Strings(",")
|
||
|
wc.OwmMeasurements = owmSection.Key("measurements").Strings(",")
|
||
|
wc.OwmTable = owmSection.Key("table").String()
|
||
|
|
||
|
influxdbSection := config.Section("influxdb")
|
||
|
wc.InfluxHost = influxdbSection.Key("hostname").String()
|
||
|
wc.InfluxPort, err = influxdbSection.Key("port").Int()
|
||
|
HandleError(err)
|
||
|
wc.InfluxUser = influxdbSection.Key("username").String()
|
||
|
wc.InfluxPass = influxdbSection.Key("password").String()
|
||
|
wc.InfluxDB = influxdbSection.Key("database").String()
|
||
|
|
||
|
*weatherconfig = wc
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// HandleError handles errors to return err
|
||
|
func HandleError(err error) error {
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// HandleFatalError fatal errors
|
||
|
func HandleFatalError(err error) {
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
os.Exit(2)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Usage displays possible arguments
|
||
|
func Usage() {
|
||
|
flag.PrintDefaults()
|
||
|
os.Exit(1)
|
||
|
}
|