From fec20c11632333ddf3074516ef2221df643eff7b Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 20 Feb 2018 22:45:45 +0100 Subject: [PATCH] Allow fetching version from a remote URL Signed-off-by: Knut Ahlers --- executor.go | 11 ++++++++--- main.go | 22 +++++++--------------- types.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 18 deletions(-) create mode 100644 types.go diff --git a/executor.go b/executor.go index ca2aeec..3915cc8 100644 --- a/executor.go +++ b/executor.go @@ -53,17 +53,22 @@ func executePackage(pkg pkgCfg, pkgLog *log.Entry) error { return err } - if !str.StringInSlice(pkg.Version, []string{"", "master"}) { + ver, err := pkg.Version() + if err != nil { + return err + } + + if !str.StringInSlice(ver, []string{"", "master"}) { pkgLog.Debug("Resetting to specified version") pkgPath := path.Join(os.Getenv("GOPATH"), "src", pkg.Name) // Fetch required references - if err := executeCommand([]string{"git", "fetch", "-q", "--tags", "origin", pkg.Version}, stdout, stderr, pkgPath); err != nil { + if err := executeCommand([]string{"git", "fetch", "-q", "--tags", "origin", ver}, stdout, stderr, pkgPath); err != nil { return err } // Do the real reset - if err := executeCommand([]string{"git", "reset", "--hard", pkg.Version}, stdout, stderr, pkgPath); err != nil { + if err := executeCommand([]string{"git", "reset", "--hard", ver}, stdout, stderr, pkgPath); err != nil { return err } } diff --git a/main.go b/main.go index 9c380ce..d5ad535 100644 --- a/main.go +++ b/main.go @@ -13,20 +13,6 @@ import ( log "github.com/sirupsen/logrus" ) -type pkgCfg struct { - Name string `yaml:"name"` - Single bool `yaml:"single"` - Version string `yaml:"version"` -} - -type configFile struct { - Cwd string `yaml:"cwd"` - GoPath string `yaml:"gopath"` - Packages []pkgCfg `yaml:"packages"` - PreCommands [][]string `yaml:"pre_commands"` - PostCommands [][]string `yaml:"post_commands"` -} - var ( cfg = struct { Config string `flag:"config,c" default:"~/.config/gotools.yml" description:"Configuration for update-gotools utility"` @@ -109,7 +95,13 @@ func runPackageBuilds(n int, filter func(pkgCfg) bool) { limit.Add() go func(pkg pkgCfg) { - logVer := pkg.Version + logVer, err := pkg.Version() + if err != nil { + log.WithFields(log.Fields{ + "pkg": pkg.Name, + }).WithError(err).Fatal("Unable to fetch version information") + } + if logVer == "" { logVer = "HEAD" } diff --git a/types.go b/types.go new file mode 100644 index 0000000..492d236 --- /dev/null +++ b/types.go @@ -0,0 +1,45 @@ +package main + +import ( + "io/ioutil" + "net/http" +) + +type pkgCfg struct { + Name string `yaml:"name"` + Single bool `yaml:"single"` + Ver string `yaml:"version"` + VersionURL string `yaml:"version_url"` +} + +type configFile struct { + Cwd string `yaml:"cwd"` + GoPath string `yaml:"gopath"` + Packages []pkgCfg `yaml:"packages"` + PreCommands [][]string `yaml:"pre_commands"` + PostCommands [][]string `yaml:"post_commands"` +} + +func (p *pkgCfg) Version() (string, error) { + if p.Ver != "" { + return p.Ver, nil + } + + if p.VersionURL != "" { + resp, err := http.Get(p.VersionURL) + if err != nil { + return "", err + } + defer resp.Body.Close() + + v, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + p.Ver = string(v) + return p.Ver, nil + } + + return "", nil +}