Skip to content

Commit

Permalink
Add seperate file for calling git
Browse files Browse the repository at this point in the history
  • Loading branch information
mkchoi212 committed Dec 31, 2017
1 parent 2e3a0b7 commit 7068c59
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
39 changes: 39 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"bytes"
"os/exec"
"syscall"
)

func RunCommand(name string, dir string, args ...string) (stderr string, exitCode int) {
var errbuf bytes.Buffer
cmd := exec.Command(name, args...)
cmd.Dir = dir
cmd.Stderr = &errbuf

err := cmd.Run()
stderr = errbuf.String()

if err != nil {
// try to get the exit code
if exitError, ok := err.(*exec.ExitError); ok {
ws := exitError.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
} else {
// This will happen (in OSX) if `name` is not available in $PATH,
// in this situation, exit code could not be get, and stderr will be
// empty string very likely, so we use the default fail code, and format err
// to string and set to stderr
exitCode = 1
if stderr == "" {
stderr = err.Error()
}
}
} else {
// success, exitCode should be 0 if go is ok
ws := cmd.ProcessState.Sys().(syscall.WaitStatus)
exitCode = ws.ExitStatus()
}
return
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ func main() {
switch err.(type) {
case *ErrNoConflict:
fmt.Println(Green(Regular, err.Error()))
return
default:
log.Panicln(err)
fmt.Print(Red(Regular, err.Error()))
}
return
}
conflictCount = len(conflicts)

Expand Down
1 change: 0 additions & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"log"
"os"
"os/exec"
"path"
"strconv"
"strings"
Expand Down

0 comments on commit 7068c59

Please sign in to comment.