diff --git a/main.go b/main.go index 29925bd..edb1787 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,13 @@ package main import ( - "fmt" - "github.com/BurntSushi/toml" + "./src/config" tm "github.com/buger/goterm" - "io/ioutil" - "os" "time" ) func main() { - config := getConfig("status.toml") + config := config.New("status.toml") sl := SL{Config: &config} clock := Clock{Config: &config} @@ -52,22 +49,3 @@ func main() { time.Sleep(time.Second / 10) } } - -func getConfig(configFile string) Config { - var config Config - // Read the configfile - - file, err := ioutil.ReadFile(configFile) - if err != nil { - fmt.Printf("File error: %v\n", err) - os.Exit(1) - } - - // Parse config - if _, err := toml.Decode(string(file), &config); err != nil { - fmt.Printf("Config error: %v\n", err) - os.Exit(1) - } - - return config -} diff --git a/src/config/config.go b/src/config/config.go new file mode 100644 index 0000000..dca51f2 --- /dev/null +++ b/src/config/config.go @@ -0,0 +1,49 @@ +package config + +import ( + "fmt" + "github.com/BurntSushi/toml" + "io/ioutil" + "os" +) + +// Config is a struct with my config +type Config struct { + SL struct { + APIKey string + RefreshDelay int64 + SiteID int + APIURL string + } + Clock struct { + TimeFormat string + } + Weather struct { + URL string + RefreshDelay int64 + } + Pollen struct { + URL string + RefreshDelay int64 + CityName string + } +} + +func New(configFile string) Config { + var config Config + + // Read the configfile + file, err := ioutil.ReadFile(configFile) + if err != nil { + fmt.Printf("File error: %v\n", err) + os.Exit(1) + } + + // Parse config + if _, err := toml.Decode(string(file), &config); err != nil { + fmt.Printf("Config error: %v\n", err) + os.Exit(1) + } + + return config +} diff --git a/statusscreen-clock.go b/statusscreen-clock.go index 64429b4..16292a4 100644 --- a/statusscreen-clock.go +++ b/statusscreen-clock.go @@ -1,13 +1,14 @@ package main import ( + "./src/config" "github.com/common-nighthawk/go-figure" "time" ) // Clock is my Clock struct type Clock struct { - Config *Config + Config *config.Config } // GetOutput returns a rendered result of this module diff --git a/statusscreen-config.go b/statusscreen-config.go deleted file mode 100644 index 108f4e0..0000000 --- a/statusscreen-config.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -// Config is a struct with my config -type Config struct { - SL struct { - APIKey string - RefreshDelay int64 - SiteID int - APIURL string - } - Clock struct { - TimeFormat string - } - Weather struct { - URL string - RefreshDelay int64 - } - Pollen struct { - URL string - RefreshDelay int64 - CityName string - } -} diff --git a/statusscreen-pollen.go b/statusscreen-pollen.go index 0039493..860cf0d 100644 --- a/statusscreen-pollen.go +++ b/statusscreen-pollen.go @@ -1,6 +1,7 @@ package main import ( + "./src/config" "bytes" "encoding/json" "github.com/olekukonko/tablewriter" @@ -11,7 +12,7 @@ import ( ) type Pollen struct { - Config *Config + Config *config.Config lastResponse PollenResponse nextRefresh int64 } @@ -77,7 +78,7 @@ func (pollen *Pollen) GetOutput() []string { for _, v2 := range v.Pollen { // Drop rows that have the status: "inga halter" or "ingen rapport" - if v2.Day0Description[:3] == "ing" && + if v2.Day0Description[:3] == "ing" && v2.Day1Description[:3] == "ing" && v2.Day2Description[:3] == "ing" && v2.Day3Description[:3] == "ing" { diff --git a/statusscreen-sl.go b/statusscreen-sl.go index 3fe77b0..8f1d6df 100644 --- a/statusscreen-sl.go +++ b/statusscreen-sl.go @@ -1,6 +1,7 @@ package main import ( + "./src/config" "bytes" "encoding/json" "errors" @@ -15,7 +16,7 @@ import ( // SL is my SL struct type SL struct { - Config *Config + Config *config.Config lastResponse SLResponse nextRefresh int64 } diff --git a/statusscreen-weather.go b/statusscreen-weather.go index 7d7ab1e..0b7dbdc 100644 --- a/statusscreen-weather.go +++ b/statusscreen-weather.go @@ -1,6 +1,7 @@ package main import ( + "./src/config" "io/ioutil" "net/http" "strings" @@ -9,7 +10,7 @@ import ( // Weather is my Weather struct type Weather struct { - Config *Config + Config *config.Config lastResponse string nextRefresh int64 }