fix: golint check passed
This commit is contained in:
parent
9139556fbe
commit
4bf2f1e02f
4 changed files with 13 additions and 3 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
|
1
main.go
1
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")
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue