-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
76 lines (71 loc) · 1.69 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"syscall"
)
type Command struct {
Command string
Options []string
commandString string
}
func (c *Command) initCommand() {
var com string
switch c.Command {
case "rsync":
com = strings.Join(append([]string{c.Command, "-rzilcpogn", "--delete"}, c.Options...), " ")
if rsyncExclude != "" {
com = fmt.Sprintf("%s --exclude %s", com, rsyncExclude)
}
if rsyncExcludeFrom != "" {
com = fmt.Sprintf("%s --exclude-from=%s", com, rsyncExcludeFrom)
}
case "vimdiff":
com = strings.Join(append([]string{c.Command, "-R"}, c.Options...), " ")
case "cat":
com = strings.Join(append([]string{c.Command}, c.Options...), " ")
if isColor {
com = fmt.Sprintf("%s %s", com, " | colordiff")
}
if isLess {
com = fmt.Sprintf("%s %s", com, " | less -Rr")
}
default:
panic(fmt.Sprintf("%s is not supported", c.Command))
}
c.commandString = com
}
func (c *Command) Run() (int, error) {
if c.commandString == "" {
c.initCommand()
}
cmd := exec.Command(os.Getenv("SHELL"), "-c", c.commandString)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
err := cmd.Start()
if err != nil {
return 1, err
}
err = cmd.Wait()
if err != nil {
if e2, ok := err.(*exec.ExitError); ok {
if s, ok := e2.Sys().(syscall.WaitStatus); ok {
return s.ExitStatus(), err
}
panic(errors.New("Unimplemented for system where exec.ExitError.Sys() is not syscall.WaitStatus."))
}
}
return 0, nil
}
func (c *Command) Output() ([]byte, error) {
if c.commandString == "" {
c.initCommand()
}
cmd := exec.Command(os.Getenv("SHELL"), "-c", c.commandString)
cmd.Stderr = os.Stderr
return cmd.Output()
}