Move config file handling to the rest of the config stuff

This commit is contained in:
Patrick Georgi 2021-10-29 22:13:39 +02:00
parent 2ac05f29d1
commit be176fe419
2 changed files with 26 additions and 18 deletions

View file

@ -1,6 +1,12 @@
package main
import "strings"
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v2"
)
type Repo struct {
URL string `yaml:"url"`
@ -29,3 +35,21 @@ type configFormat struct {
Sites sites `yaml:"sites"`
Workstreams map[string]Workstreams `yaml:"workstreams"`
}
func readConfig(configFile string) *configFormat {
file, err := os.Open(configFile)
if err != nil {
panic(fmt.Sprintf("Could not open '%s': %s", configFile, err))
}
defer file.Close()
cfg := new(configFormat)
configDecoder := yaml.NewDecoder(file)
err = configDecoder.Decode(&cfg)
if err != nil {
panic(fmt.Sprintf("Failed parsing '%s': %s", configFile, err))
}
return cfg
}

View file

@ -3,9 +3,6 @@ package main
import (
"flag"
"fmt"
"os"
"gopkg.in/yaml.v2"
)
func main() {
@ -13,20 +10,7 @@ func main() {
configFile := flag.String("config", "config.yaml", "path to configuration")
flag.Parse()
file, err := os.Open(*configFile)
if err != nil {
panic(fmt.Sprintf("Could not open '%s': %s", *configFile, err))
}
defer file.Close()
cfg := new(configFormat)
configDecoder := yaml.NewDecoder(file)
err = configDecoder.Decode(&cfg)
if err != nil {
panic(fmt.Sprintf("Failed parsing '%s': %s", *configFile, err))
}
cfg := readConfig(*configFile)
repo := openRepo(dir, cfg)
fmt.Println(repo)