From 4bf2f1e02f5b52ea057c654588329b7de443decb Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Thu, 22 Aug 2019 19:47:25 +0200 Subject: [PATCH] fix: golint check passed --- CHANGELOG.md | 1 + config.go | 8 ++++++-- main.go | 1 + weather.go | 6 +++++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 392f1f7..19cd537 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) - too many open files close with influxdb connection - add license in code file +- golint check passed ## v1.0.0 - 2019-08-14 diff --git a/config.go b/config.go index e0144ec..154e34c 100644 --- a/config.go +++ b/config.go @@ -23,6 +23,7 @@ import ( yaml "gopkg.in/yaml.v2" ) +// Config struct contains all options type Config struct { InfluxDB struct { URL string `yaml:"url"` @@ -32,13 +33,14 @@ type Config struct { Password string `yaml:"password"` } `yaml:"influxdb"` OpenWeatherMap struct { - ApiKey string `yaml:"api_key"` + APIKey string `yaml:"api_key"` Units string `yaml:"units"` } `yaml:"openweathermap"` Cities []string `yaml:"cities"` Interval int64 `yaml:"interval"` } +// Load the config from a file func (c *Config) Load(path string) error { data, err := ioutil.ReadFile(path) if err != nil { @@ -59,6 +61,7 @@ func (c *Config) Load(path string) error { return nil } +// Defaults set options with default value func (c *Config) Defaults() { if c.OpenWeatherMap.Units == "" { c.OpenWeatherMap.Units = "metric" @@ -77,12 +80,13 @@ func (c *Config) Defaults() { } } +// Check if the options are good func (c *Config) Check() error { if c.InfluxDB.Database == "" { return fmt.Errorf("you must specify influxdb.database in config file") } - if c.OpenWeatherMap.ApiKey == "" { + if c.OpenWeatherMap.APIKey == "" { return fmt.Errorf("you must specify api_key in config file") } diff --git a/main.go b/main.go index 859f441..bded921 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "time" ) +// Options and OpenWeatherMap URI var ( URI = "https://api.openweathermap.org/data/2.5" CONFIG = flag.String("config", "", "config file path") diff --git a/weather.go b/weather.go index 9dde2e0..dfa8fa7 100644 --- a/weather.go +++ b/weather.go @@ -26,11 +26,13 @@ import ( influx "github.com/influxdata/influxdb1-client/v2" ) +// Weather is the main struct type Weather struct { Config Config WeatherDatas []WeatherData } +// WeatherData contains weather data from openweathermap type WeatherData struct { City string `json:"name"` Main struct { @@ -58,6 +60,7 @@ type WeatherData struct { } `json:"sys"` } +// FetchData from OpenWeatherMap API func (w *Weather) FetchData() { for _, city := range w.Config.Cities { resp, err := http.Get( @@ -65,7 +68,7 @@ func (w *Weather) FetchData() { "%s/weather?q=%s&appid=%s&units=%s", URI, city, - w.Config.OpenWeatherMap.ApiKey, + w.Config.OpenWeatherMap.APIKey, w.Config.OpenWeatherMap.Units, ), ) @@ -87,6 +90,7 @@ func (w *Weather) FetchData() { } } +// SendToInfluxDB send data in influxdb func (w *Weather) SendToInfluxDB() error { conn, err := influx.NewHTTPClient(influx.HTTPConfig{ Addr: w.Config.InfluxDB.URL,