From 5745ca3437a587ca5f78ab6198f3dee4678a7a1f Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Fri, 19 Jul 2019 20:23:05 +0200 Subject: [PATCH] updated default values for configuration --- functions.go | 31 +++++++++++++++++++------------ types.go | 1 - 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/functions.go b/functions.go index bbed429..608373e 100644 --- a/functions.go +++ b/functions.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "flag" "fmt" "io/ioutil" @@ -25,27 +26,33 @@ func GetConfig(configfile string, weatherconfig *WeatherConfig) error { var wc WeatherConfig weatherSection := config.Section("weather") - wc.WeatherVersion = weatherSection.Key("version").String() - wc.WeatherAppID = weatherSection.Key("appid").String() - wc.WeatherCities = weatherSection.Key("cities").Strings(",") - wc.WeatherMeasurements = weatherSection.Key("measurements").Strings(",") - wc.WeatherTable = weatherSection.Key("table").String() - influxdbSection := config.Section("influxdb") - wc.InfluxHost = influxdbSection.Key("hostname").String() - wc.InfluxPort, err = influxdbSection.Key("port").Int() - if err != nil { + wc.WeatherVersion = weatherSection.Key("version").MustString("2.5") + wc.WeatherAppID = weatherSection.Key("appid").MustString("appid") + wc.WeatherCities = weatherSection.Key("cities").Strings(",") + if len(wc.WeatherCities) < 1 { + err := errors.New("No cities provided in config") return err } - wc.InfluxUser = influxdbSection.Key("username").String() - wc.InfluxPass = influxdbSection.Key("password").String() - wc.InfluxDB = influxdbSection.Key("database").String() + wc.WeatherMeasurements = weatherSection.Key("measurements").Strings(",") + if len(wc.WeatherMeasurements) < 1 { + err := errors.New("No measurements provided in config") + return err + } + + influxdbSection := config.Section("influxdb") + wc.InfluxHost = influxdbSection.Key("hostname").MustString("localhost") + wc.InfluxPort = influxdbSection.Key("port").MustInt(8086) + wc.InfluxUser = influxdbSection.Key("username").MustString("username") + wc.InfluxPass = influxdbSection.Key("password").MustString("password") + wc.InfluxDB = influxdbSection.Key("database").MustString("me") *weatherconfig = wc return err } +// FetchData fetch data from api func FetchData(city string) (Data, error) { var d Data diff --git a/types.go b/types.go index b0afbf8..928e63a 100644 --- a/types.go +++ b/types.go @@ -6,7 +6,6 @@ type WeatherConfig struct { WeatherAppID string WeatherCities []string WeatherMeasurements []string - WeatherTable string InfluxHost string InfluxPort int InfluxUser string