1
0
Fork 0
mirror of https://github.com/Luzifer/update-gotools.git synced 2024-12-22 12:51:20 +00:00

Allow fetching version from a remote URL

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-02-20 22:45:45 +01:00
parent 3eb44daa51
commit fec20c1163
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 60 additions and 18 deletions

View file

@ -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
}
}

22
main.go
View file

@ -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"
}

45
types.go Normal file
View file

@ -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
}