rebuilt error handling

This commit is contained in:
Paul 2019-07-09 16:41:35 +02:00
parent c3163c83d0
commit 237f7a2907
2 changed files with 56 additions and 36 deletions

View File

@ -2,28 +2,42 @@ package main
import (
"flag"
"log"
)
func main() {
var configpath string
var fpc FuelPricesConfig
var err error
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse()
GetConfig(configpath, &fpc)
var szip Srcfile
var output []byte
flag.StringVar(&configpath, "configfile", "common.ini", "config file to use with fuelprices section")
flag.Parse()
err = GetConfig(configpath, &fpc)
if err != nil {
log.Fatal(err)
}
err = DownloadFile(&fpc, &szip)
HandleFatalError(err)
if err != nil {
log.Fatal(err)
}
err = ExtractZip(&fpc, &szip, &output)
HandleFatalError(err)
if err != nil {
log.Fatal(err)
}
var prices *[]Price
GetPrices(&prices, &fpc, &output)
err = GetPrices(&prices, &fpc, &output)
if err != nil {
log.Fatal(err)
}
SendToInflux(&fpc, prices)
err = SendToInflux(&fpc, prices)
if err != nil {
log.Fatal(err)
}
}

View File

@ -28,7 +28,9 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
flag.Usage = Usage
config, err := ini.Load(configfile)
HandleFatalError(err)
if err != nil {
return err
}
var fpc FuelPricesConfig
@ -41,10 +43,8 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
influxdbSection := config.Section("influxdb")
fpc.InfluxHost = influxdbSection.Key("hostname").String()
fpc.InfluxPort, err = influxdbSection.Key("port").Int()
HandleError(err)
fpc.InfluxUser = influxdbSection.Key("username").String()
fpc.InfluxPass = influxdbSection.Key("password").String()
HandleError(err)
fpc.InfluxDB = influxdbSection.Key("database").String()
@ -53,7 +53,11 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
*fuelpricesconfig = fpc
return err
if err != nil {
return err
}
return nil
}
// DownloadFile fetch file from webserver
@ -66,23 +70,29 @@ func DownloadFile(fpc *FuelPricesConfig, szip *Srcfile) error {
}}
//resp, err := http.Get(url)
resp, err := c.Get(fpc.RemoteURL)
HandleError(err)
if err != nil {
return err
}
defer resp.Body.Close()
szip.Content, err = ioutil.ReadAll(resp.Body)
HandleError(err)
if err != nil {
return err
}
time.Sleep(pollTo)
return err
return nil
}
// ExtractZip get the XML file to be processed
func ExtractZip(fpc *FuelPricesConfig, szip *Srcfile, output *[]byte) error {
zipfile, err := zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content)))
HandleFatalError(err)
if err != nil {
return err
}
for _, f := range zipfile.File {
if f.Name == fpc.RemoteFilename {
@ -104,9 +114,12 @@ func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error {
var pr []Price
var xml *xmlquery.Node
f := bytes.NewReader(*output)
xml, err := xmlquery.Parse(f)
HandleError(err)
if err != nil {
return err
}
for _, station := range fpc.Pos {
for _, fuel := range fpc.Types {
@ -134,18 +147,22 @@ func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error {
}
// SendToInflux sends time series data to influxdb
func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) {
func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) error {
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
Addr: fmt.Sprintf("http://%s:%d", fpc.InfluxHost, fpc.InfluxPort),
Username: fpc.InfluxUser,
Password: fpc.InfluxPass,
})
HandleFatalError(err)
if err != nil {
return err
}
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: fpc.InfluxDB,
})
HandleFatalError(err)
if err != nil {
return err
}
for _, p := range *prices {
@ -163,23 +180,12 @@ func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) {
bp.AddPoint(point)
err = httpClient.Write(bp)
HandleError(err)
if err != nil {
return err
}
}
}
// HandleError handles errors
func HandleError(err error) {
if err != nil {
log.Println(err)
}
}
// HandleFatalError handles fatal errors
func HandleFatalError(err error) {
if err != nil {
log.Fatal(err)
os.Exit(2)
}
return nil
}
// Usage displays possible arguments