From be176fe419545b5ae9e8e39afafbee3461530202 Mon Sep 17 00:00:00 2001 From: Patrick Georgi Date: Fri, 29 Oct 2021 22:13:39 +0200 Subject: [PATCH] Move config file handling to the rest of the config stuff --- config.go | 26 +++++++++++++++++++++++++- gerritcp.go | 18 +----------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/config.go b/config.go index 5d9fcf4..f8d7f5c 100644 --- a/config.go +++ b/config.go @@ -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 +} diff --git a/gerritcp.go b/gerritcp.go index dcc878c..126a20c 100644 --- a/gerritcp.go +++ b/gerritcp.go @@ -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)