[general] check return codes and seperate method to get the git
working copy root
This commit is contained in:
parent
9b3ae629f8
commit
e30a499eec
|
@ -2,29 +2,45 @@ package general
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os/exec"
|
"log"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunCommand is a simple wrapper to run commands
|
// RunCommand is a simple wrapper to run commands
|
||||||
func RunCommand(command string, values ...string) string {
|
// result is the output of stdout and errCode is the exit code
|
||||||
|
func RunCommand(command string, values ...string) (result string, errCode int) {
|
||||||
cmd := exec.Command(command, values...)
|
cmd := exec.Command(command, values...)
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
cmd.Stdout = &out
|
cmd.Stdout = &out
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
panic(err)
|
log.Fatalf("cmd.Start: %v", err)
|
||||||
}
|
}
|
||||||
return out.String()
|
|
||||||
|
if err := cmd.Wait(); err != nil {
|
||||||
|
if exiterr, ok := err.(*exec.ExitError); ok {
|
||||||
|
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
|
||||||
|
errCode = status.ExitStatus()
|
||||||
|
return result, errCode
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Fatalf("cmd.Wait: %v", err)
|
||||||
|
return result, errCode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = out.String()
|
||||||
|
return result, errCode
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChangedGoFiles returns a list of changed .go files
|
// GetChangedGoFiles returns a list of changed .go files
|
||||||
// anything else is filtered out
|
// anything else is filtered out
|
||||||
func GetChangedGoFiles() (result []string) {
|
func GetChangedGoFiles() (result []string) {
|
||||||
gitDir := RunCommand("git", "rev-parse", "--git-dir")
|
gitDiff, _ := RunCommand("git", "diff", "--name-only", "--cached", "--diff-filter=ACM")
|
||||||
gitDiff := RunCommand("git", "diff", "--name-only", "--cached", "--diff-filter=ACM")
|
absolutePath := GetGitRoot()
|
||||||
absolutePath := filepath.Dir(gitDir)
|
|
||||||
|
|
||||||
resultLines := strings.Split(gitDiff, "\n")
|
resultLines := strings.Split(gitDiff, "\n")
|
||||||
if resultLines == nil {
|
if resultLines == nil {
|
||||||
|
@ -37,3 +53,9 @@ func GetChangedGoFiles() (result []string) {
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetGitRoot returns the path with the .git directory
|
||||||
|
func GetGitRoot() string {
|
||||||
|
gitDir, _ := RunCommand("git", "rev-parse", "--git-dir")
|
||||||
|
return filepath.Dir(gitDir)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user