From 5ac553bcc2697de037c08166c101137cc988b171 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Tue, 20 Feb 2018 21:33:16 +0100 Subject: [PATCH] Split execution functions to own file Signed-off-by: Knut Ahlers --- executor.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 66 ------------------------------------------------ 2 files changed, 73 insertions(+), 66 deletions(-) create mode 100644 executor.go diff --git a/executor.go b/executor.go new file mode 100644 index 0000000..ca2aeec --- /dev/null +++ b/executor.go @@ -0,0 +1,73 @@ +package main + +import ( + "io" + "os" + "os/exec" + "path" + "strings" + + "github.com/Luzifer/go_helpers/str" + log "github.com/sirupsen/logrus" +) + +func executeCommands(logEntry *log.Entry, commands [][]string) error { + for i, cmdCfg := range commands { + stderr := logEntry.WithFields(log.Fields{"cmd_id": i}).WriterLevel(log.ErrorLevel) + stdout := logEntry.WithFields(log.Fields{"cmd_id": i}).WriterLevel(log.InfoLevel) + defer stderr.Close() + defer stdout.Close() + + if err := executeCommand(cmdCfg, stdout, stderr, cfgFile.Cwd); err != nil { + return err + } + } + + return nil +} + +func executeCommand(command []string, stdout, stderr io.Writer, cwd string) error { + env := []string{ + strings.Join([]string{"GOPATH", cfgFile.GoPath}, "="), + strings.Join([]string{"PATH", os.Getenv("PATH")}, "="), + } + + cmd := exec.Command(command[0], command[1:]...) + cmd.Dir = cwd + cmd.Env = env + cmd.Stdout = stdout + cmd.Stderr = stderr + return cmd.Run() +} + +func executePackage(pkg pkgCfg, pkgLog *log.Entry) error { + pkgLog.Info("Started package installation") + + stderr := pkgLog.WriterLevel(log.ErrorLevel) + stdout := pkgLog.WriterLevel(log.InfoLevel) + defer stderr.Close() + defer stdout.Close() + + pkgLog.Debug("Fetching package using `go get`") + if err := executeCommand([]string{"go", "get", "-d", pkg.Name}, stdout, stderr, cfgFile.Cwd); err != nil { + return err + } + + if !str.StringInSlice(pkg.Version, []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 { + return err + } + + // Do the real reset + if err := executeCommand([]string{"git", "reset", "--hard", pkg.Version}, stdout, stderr, pkgPath); err != nil { + return err + } + } + + pkgLog.Debug("Install package using `go install`") + return executeCommand([]string{"go", "install", pkg.Name}, stdout, stderr, cfgFile.Cwd) +} diff --git a/main.go b/main.go index 9f86559..b4cab76 100644 --- a/main.go +++ b/main.go @@ -2,17 +2,12 @@ package main import ( "fmt" - "io" "io/ioutil" "os" - "os/exec" - "path" "runtime" - "strings" yaml "gopkg.in/yaml.v2" - "github.com/Luzifer/go_helpers/str" "github.com/Luzifer/rconfig" homedir "github.com/mitchellh/go-homedir" log "github.com/sirupsen/logrus" @@ -155,64 +150,3 @@ func defaultConfig() (*configFile, error) { PostCommands: [][]string{}, }, nil } - -func executeCommands(logEntry *log.Entry, commands [][]string) error { - for i, cmdCfg := range commands { - stderr := logEntry.WithFields(log.Fields{"cmd_id": i}).WriterLevel(log.ErrorLevel) - stdout := logEntry.WithFields(log.Fields{"cmd_id": i}).WriterLevel(log.InfoLevel) - defer stderr.Close() - defer stdout.Close() - - if err := executeCommand(cmdCfg, stdout, stderr, cfgFile.Cwd); err != nil { - return err - } - } - - return nil -} - -func executeCommand(command []string, stdout, stderr io.Writer, cwd string) error { - env := []string{ - strings.Join([]string{"GOPATH", cfgFile.GoPath}, "="), - strings.Join([]string{"PATH", os.Getenv("PATH")}, "="), - } - - cmd := exec.Command(command[0], command[1:]...) - cmd.Dir = cwd - cmd.Env = env - cmd.Stdout = stdout - cmd.Stderr = stderr - return cmd.Run() -} - -func executePackage(pkg pkgCfg, pkgLog *log.Entry) error { - pkgLog.Info("Started package installation") - - stderr := pkgLog.WriterLevel(log.ErrorLevel) - stdout := pkgLog.WriterLevel(log.InfoLevel) - defer stderr.Close() - defer stdout.Close() - - pkgLog.Debug("Fetching package using `go get`") - if err := executeCommand([]string{"go", "get", "-d", pkg.Name}, stdout, stderr, cfgFile.Cwd); err != nil { - return err - } - - if !str.StringInSlice(pkg.Version, []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 { - return err - } - - // Do the real reset - if err := executeCommand([]string{"git", "reset", "--hard", pkg.Version}, stdout, stderr, pkgPath); err != nil { - return err - } - } - - pkgLog.Debug("Install package using `go install`") - return executeCommand([]string{"go", "install", pkg.Name}, stdout, stderr, cfgFile.Cwd) -}