diff options
author | Alejandro Rodríguez <alejorro70@gmail.com> | 2017-02-28 17:51:48 +0300 |
---|---|---|
committer | Alejandro Rodríguez <alejorro70@gmail.com> | 2017-02-28 18:06:34 +0300 |
commit | 262f60a19ed2f477be91a700a34f85d2ff0fd474 (patch) | |
tree | 321b590f3ca81cdb48d746f887ae077c2473b6cf | |
parent | 6381c464b606c28df11712d94ad49299855fdd3a (diff) |
Add git commmand helper
-rw-r--r-- | internal/helper/command.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/helper/command.go b/internal/helper/command.go new file mode 100644 index 000000000..c9c0cdf94 --- /dev/null +++ b/internal/helper/command.go @@ -0,0 +1,53 @@ +package helper + +import ( + "fmt" + "io" + "os" + "os/exec" + "syscall" +) + +// Command encapsulates operations with commands creates with NewCommand +type Command struct { + io.Reader + *exec.Cmd +} + +// Kill cleans the subprocess group of the command. Callers should defer a call +// to kill after they get the command from NewCommand +func (c *Command) Kill() { + CleanUpProcessGroup(c.Cmd) +} + +// GitCommand creates a git Command with the given args +func GitCommand(args ...string) (*Command, error) { + return NewCommand(exec.Command("git", args...)) +} + +// NewCommand creates a Command from an exec.Cmd +func NewCommand(cmd *exec.Cmd) (*Command, error) { + // Explicitly set the environment for the command + cmd.Env = []string{ + fmt.Sprintf("HOME=%s", os.Getenv("HOME")), + fmt.Sprintf("PATH=%s", os.Getenv("PATH")), + fmt.Sprintf("LD_LIBRARY_PATH=%s", os.Getenv("LD_LIBRARY_PATH")), + } + + // Start the command in its own process group (nice for signalling) + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + + // If we don't do something with cmd.Stderr, Git errors will be lost + cmd.Stderr = os.Stderr + + stdout, err := cmd.StdoutPipe() + if err != nil { + return nil, fmt.Errorf("GitCommand: stdout: %v", err) + } + + if err := cmd.Start(); err != nil { + return nil, fmt.Errorf("GitCommand: start %v: %v", cmd.Args, err) + } + + return &Command{Reader: stdout, Cmd: cmd}, nil +} |