fix: golint check passed

This commit is contained in:
Adrien Waksberg 2019-08-22 19:47:25 +02:00
parent 9139556fbe
commit 4bf2f1e02f
4 changed files with 13 additions and 3 deletions

View file

@ -11,6 +11,7 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
- too many open files close with influxdb connection - too many open files close with influxdb connection
- add license in code file - add license in code file
- golint check passed
## v1.0.0 - 2019-08-14 ## v1.0.0 - 2019-08-14

View file

@ -23,6 +23,7 @@ import (
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
) )
// Config struct contains all options
type Config struct { type Config struct {
InfluxDB struct { InfluxDB struct {
URL string `yaml:"url"` URL string `yaml:"url"`
@ -32,13 +33,14 @@ type Config struct {
Password string `yaml:"password"` Password string `yaml:"password"`
} `yaml:"influxdb"` } `yaml:"influxdb"`
OpenWeatherMap struct { OpenWeatherMap struct {
ApiKey string `yaml:"api_key"` APIKey string `yaml:"api_key"`
Units string `yaml:"units"` Units string `yaml:"units"`
} `yaml:"openweathermap"` } `yaml:"openweathermap"`
Cities []string `yaml:"cities"` Cities []string `yaml:"cities"`
Interval int64 `yaml:"interval"` Interval int64 `yaml:"interval"`
} }
// Load the config from a file
func (c *Config) Load(path string) error { func (c *Config) Load(path string) error {
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
@ -59,6 +61,7 @@ func (c *Config) Load(path string) error {
return nil return nil
} }
// Defaults set options with default value
func (c *Config) Defaults() { func (c *Config) Defaults() {
if c.OpenWeatherMap.Units == "" { if c.OpenWeatherMap.Units == "" {
c.OpenWeatherMap.Units = "metric" c.OpenWeatherMap.Units = "metric"
@ -77,12 +80,13 @@ func (c *Config) Defaults() {
} }
} }
// Check if the options are good
func (c *Config) Check() error { func (c *Config) Check() error {
if c.InfluxDB.Database == "" { if c.InfluxDB.Database == "" {
return fmt.Errorf("you must specify influxdb.database in config file") 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") return fmt.Errorf("you must specify api_key in config file")
} }

View file

@ -23,6 +23,7 @@ import (
"time" "time"
) )
// Options and OpenWeatherMap URI
var ( var (
URI = "https://api.openweathermap.org/data/2.5" URI = "https://api.openweathermap.org/data/2.5"
CONFIG = flag.String("config", "", "config file path") CONFIG = flag.String("config", "", "config file path")

View file

@ -26,11 +26,13 @@ import (
influx "github.com/influxdata/influxdb1-client/v2" influx "github.com/influxdata/influxdb1-client/v2"
) )
// Weather is the main struct
type Weather struct { type Weather struct {
Config Config Config Config
WeatherDatas []WeatherData WeatherDatas []WeatherData
} }
// WeatherData contains weather data from openweathermap
type WeatherData struct { type WeatherData struct {
City string `json:"name"` City string `json:"name"`
Main struct { Main struct {
@ -58,6 +60,7 @@ type WeatherData struct {
} `json:"sys"` } `json:"sys"`
} }
// FetchData from OpenWeatherMap API
func (w *Weather) FetchData() { func (w *Weather) FetchData() {
for _, city := range w.Config.Cities { for _, city := range w.Config.Cities {
resp, err := http.Get( resp, err := http.Get(
@ -65,7 +68,7 @@ func (w *Weather) FetchData() {
"%s/weather?q=%s&appid=%s&units=%s", "%s/weather?q=%s&appid=%s&units=%s",
URI, URI,
city, city,
w.Config.OpenWeatherMap.ApiKey, w.Config.OpenWeatherMap.APIKey,
w.Config.OpenWeatherMap.Units, w.Config.OpenWeatherMap.Units,
), ),
) )
@ -87,6 +90,7 @@ func (w *Weather) FetchData() {
} }
} }
// SendToInfluxDB send data in influxdb
func (w *Weather) SendToInfluxDB() error { func (w *Weather) SendToInfluxDB() error {
conn, err := influx.NewHTTPClient(influx.HTTPConfig{ conn, err := influx.NewHTTPClient(influx.HTTPConfig{
Addr: w.Config.InfluxDB.URL, Addr: w.Config.InfluxDB.URL,