rebuilt error handling
This commit is contained in:
parent
c3163c83d0
commit
237f7a2907
@ -2,28 +2,42 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var configpath string
|
var configpath string
|
||||||
var fpc FuelPricesConfig
|
var fpc FuelPricesConfig
|
||||||
var err error
|
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 szip Srcfile
|
||||||
var output []byte
|
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)
|
err = DownloadFile(&fpc, &szip)
|
||||||
HandleFatalError(err)
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
err = ExtractZip(&fpc, &szip, &output)
|
err = ExtractZip(&fpc, &szip, &output)
|
||||||
HandleFatalError(err)
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
var prices *[]Price
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
60
functions.go
60
functions.go
@ -28,7 +28,9 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
|
|||||||
flag.Usage = Usage
|
flag.Usage = Usage
|
||||||
|
|
||||||
config, err := ini.Load(configfile)
|
config, err := ini.Load(configfile)
|
||||||
HandleFatalError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var fpc FuelPricesConfig
|
var fpc FuelPricesConfig
|
||||||
|
|
||||||
@ -41,10 +43,8 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
|
|||||||
influxdbSection := config.Section("influxdb")
|
influxdbSection := config.Section("influxdb")
|
||||||
fpc.InfluxHost = influxdbSection.Key("hostname").String()
|
fpc.InfluxHost = influxdbSection.Key("hostname").String()
|
||||||
fpc.InfluxPort, err = influxdbSection.Key("port").Int()
|
fpc.InfluxPort, err = influxdbSection.Key("port").Int()
|
||||||
HandleError(err)
|
|
||||||
fpc.InfluxUser = influxdbSection.Key("username").String()
|
fpc.InfluxUser = influxdbSection.Key("username").String()
|
||||||
fpc.InfluxPass = influxdbSection.Key("password").String()
|
fpc.InfluxPass = influxdbSection.Key("password").String()
|
||||||
HandleError(err)
|
|
||||||
|
|
||||||
fpc.InfluxDB = influxdbSection.Key("database").String()
|
fpc.InfluxDB = influxdbSection.Key("database").String()
|
||||||
|
|
||||||
@ -53,7 +53,11 @@ func GetConfig(configfile string, fuelpricesconfig *FuelPricesConfig) error {
|
|||||||
|
|
||||||
*fuelpricesconfig = fpc
|
*fuelpricesconfig = fpc
|
||||||
|
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownloadFile fetch file from webserver
|
// DownloadFile fetch file from webserver
|
||||||
@ -66,23 +70,29 @@ func DownloadFile(fpc *FuelPricesConfig, szip *Srcfile) error {
|
|||||||
}}
|
}}
|
||||||
//resp, err := http.Get(url)
|
//resp, err := http.Get(url)
|
||||||
resp, err := c.Get(fpc.RemoteURL)
|
resp, err := c.Get(fpc.RemoteURL)
|
||||||
HandleError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
szip.Content, err = ioutil.ReadAll(resp.Body)
|
szip.Content, err = ioutil.ReadAll(resp.Body)
|
||||||
HandleError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(pollTo)
|
time.Sleep(pollTo)
|
||||||
|
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtractZip get the XML file to be processed
|
// ExtractZip get the XML file to be processed
|
||||||
func ExtractZip(fpc *FuelPricesConfig, szip *Srcfile, output *[]byte) error {
|
func ExtractZip(fpc *FuelPricesConfig, szip *Srcfile, output *[]byte) error {
|
||||||
|
|
||||||
zipfile, err := zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content)))
|
zipfile, err := zip.NewReader(bytes.NewReader(szip.Content), int64(len(szip.Content)))
|
||||||
HandleFatalError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for _, f := range zipfile.File {
|
for _, f := range zipfile.File {
|
||||||
if f.Name == fpc.RemoteFilename {
|
if f.Name == fpc.RemoteFilename {
|
||||||
@ -104,9 +114,12 @@ func GetPrices(prices **[]Price, fpc *FuelPricesConfig, output *[]byte) error {
|
|||||||
|
|
||||||
var pr []Price
|
var pr []Price
|
||||||
var xml *xmlquery.Node
|
var xml *xmlquery.Node
|
||||||
|
|
||||||
f := bytes.NewReader(*output)
|
f := bytes.NewReader(*output)
|
||||||
xml, err := xmlquery.Parse(f)
|
xml, err := xmlquery.Parse(f)
|
||||||
HandleError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for _, station := range fpc.Pos {
|
for _, station := range fpc.Pos {
|
||||||
for _, fuel := range fpc.Types {
|
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
|
// 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{
|
httpClient, err := client.NewHTTPClient(client.HTTPConfig{
|
||||||
Addr: fmt.Sprintf("http://%s:%d", fpc.InfluxHost, fpc.InfluxPort),
|
Addr: fmt.Sprintf("http://%s:%d", fpc.InfluxHost, fpc.InfluxPort),
|
||||||
Username: fpc.InfluxUser,
|
Username: fpc.InfluxUser,
|
||||||
Password: fpc.InfluxPass,
|
Password: fpc.InfluxPass,
|
||||||
})
|
})
|
||||||
HandleFatalError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
|
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
|
||||||
Database: fpc.InfluxDB,
|
Database: fpc.InfluxDB,
|
||||||
})
|
})
|
||||||
HandleFatalError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for _, p := range *prices {
|
for _, p := range *prices {
|
||||||
|
|
||||||
@ -163,23 +180,12 @@ func SendToInflux(fpc *FuelPricesConfig, prices *[]Price) {
|
|||||||
|
|
||||||
bp.AddPoint(point)
|
bp.AddPoint(point)
|
||||||
err = httpClient.Write(bp)
|
err = httpClient.Write(bp)
|
||||||
HandleError(err)
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// HandleError handles errors
|
return nil
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Usage displays possible arguments
|
// Usage displays possible arguments
|
||||||
|
Loading…
Reference in New Issue
Block a user