From 9da67961582ddb962445af928357199486b40250 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 6 Apr 2018 11:39:03 +0200 Subject: [PATCH] Add homdir expansion for paths Signed-off-by: Knut Ahlers --- main.go | 10 +--------- types.go | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 16f5555..3eb523e 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,9 @@ package main import ( "fmt" - "io/ioutil" "os" "runtime" - yaml "gopkg.in/yaml.v2" - "github.com/Luzifer/rconfig" homedir "github.com/mitchellh/go-homedir" log "github.com/sirupsen/logrus" @@ -55,12 +52,7 @@ func main() { log.WithError(err).Fatal("Unable to create default config") } - cfgFileRaw, err := ioutil.ReadFile(cfg.Config) - if err != nil { - log.WithError(err).Fatal("Unable to read config") - } - - if err := yaml.Unmarshal(cfgFileRaw, cfgFile); err != nil { + if err := cfgFile.LoadFromPath(cfg.Config); err != nil { log.WithError(err).Fatal("Unable to parse config file") } diff --git a/types.go b/types.go index 492d236..6cc7687 100644 --- a/types.go +++ b/types.go @@ -3,6 +3,11 @@ package main import ( "io/ioutil" "net/http" + "os" + + homedir "github.com/mitchellh/go-homedir" + + yaml "gopkg.in/yaml.v2" ) type pkgCfg struct { @@ -20,6 +25,28 @@ type configFile struct { PostCommands [][]string `yaml:"post_commands"` } +func (c *configFile) LoadFromPath(filepath string) error { + r, err := os.Open(filepath) + if err != nil { + return err + } + defer r.Close() + + if err := yaml.NewDecoder(r).Decode(cfgFile); err != nil { + return err + } + + if c.Cwd, err = homedir.Expand(c.Cwd); err != nil { + return err + } + + if c.GoPath, err = homedir.Expand(c.GoPath); err != nil { + return err + } + + return nil +} + func (p *pkgCfg) Version() (string, error) { if p.Ver != "" { return p.Ver, nil