hub/cmd/cmd.go

118 строки
2.3 KiB
Go
Исходник Обычный вид История

2013-05-29 22:58:46 +04:00
package cmd
2013-04-29 00:41:45 +04:00
import (
2013-06-18 00:35:02 +04:00
"fmt"
2013-04-29 05:47:08 +04:00
"os"
2013-04-29 00:41:45 +04:00
"os/exec"
"runtime"
2013-07-02 22:28:50 +04:00
"strings"
"syscall"
"github.com/github/hub/ui"
"github.com/github/hub/utils"
"github.com/kballard/go-shellquote"
2013-04-29 00:41:45 +04:00
)
2013-05-29 22:58:46 +04:00
type Cmd struct {
Name string
Args []string
Stdin *os.File
Stdout *os.File
Stderr *os.File
2013-04-29 00:41:45 +04:00
}
2013-07-02 22:28:50 +04:00
func (cmd Cmd) String() string {
return fmt.Sprintf("%s %s", cmd.Name, strings.Join(cmd.Args, " "))
}
2013-05-29 22:58:46 +04:00
func (cmd *Cmd) WithArg(arg string) *Cmd {
cmd.Args = append(cmd.Args, arg)
2013-04-29 00:41:45 +04:00
return cmd
}
func (cmd *Cmd) WithArgs(args ...string) *Cmd {
2013-07-05 22:10:24 +04:00
for _, arg := range args {
cmd.WithArg(arg)
}
return cmd
}
func (cmd *Cmd) CombinedOutput() (string, error) {
verboseLog(cmd)
2013-06-22 05:02:29 +04:00
output, err := exec.Command(cmd.Name, cmd.Args...).CombinedOutput()
2013-04-29 00:41:45 +04:00
2013-06-22 05:02:29 +04:00
return string(output), err
2013-04-29 00:41:45 +04:00
}
func (cmd *Cmd) Success() bool {
verboseLog(cmd)
err := exec.Command(cmd.Name, cmd.Args...).Run()
return err == nil
}
// Run runs command with `Exec` on platforms except Windows
// which only supports `Spawn`
func (cmd *Cmd) Run() error {
if runtime.GOOS == "windows" {
return cmd.Spawn()
} else {
return cmd.Exec()
}
}
// Spawn runs command with spawn(3)
func (cmd *Cmd) Spawn() error {
verboseLog(cmd)
c := exec.Command(cmd.Name, cmd.Args...)
c.Stdin = cmd.Stdin
c.Stdout = cmd.Stdout
c.Stderr = cmd.Stderr
2013-04-29 05:47:08 +04:00
return c.Run()
}
// Exec runs command with exec(3)
// Note that Windows doesn't support exec(3): http://golang.org/src/pkg/syscall/exec_windows.go#L339
func (cmd *Cmd) Exec() error {
verboseLog(cmd)
binary, err := exec.LookPath(cmd.Name)
if err != nil {
return &exec.Error{
Name: cmd.Name,
Err: fmt.Errorf("command not found"),
}
}
args := []string{binary}
args = append(args, cmd.Args...)
return syscall.Exec(binary, args, os.Environ())
}
2013-11-26 19:14:47 +04:00
func New(cmd string) *Cmd {
cmds, err := shellquote.Split(cmd)
utils.Check(err)
name := cmds[0]
2018-05-29 13:31:14 +03:00
args := cmds[1:]
2018-04-24 20:18:24 +03:00
return &Cmd{Name: name, Args: args, Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
2013-04-29 00:41:45 +04:00
}
2013-06-01 03:32:30 +04:00
func NewWithArray(cmd []string) *Cmd {
return &Cmd{Name: cmd[0], Args: cmd[1:], Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr}
2013-06-01 03:32:30 +04:00
}
func verboseLog(cmd *Cmd) {
if os.Getenv("HUB_VERBOSE") != "" {
msg := fmt.Sprintf("$ %s %s", cmd.Name, strings.Join(cmd.Args, " "))
if ui.IsTerminal(os.Stderr) {
msg = fmt.Sprintf("\033[35m%s\033[0m", msg)
}
ui.Errorln(msg)
}
}