-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
114 lines (92 loc) · 3.16 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package dev
import (
"os"
"os/exec"
"strings"
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus"
"github.com/wish/dev/docker"
)
// Command is a wrapper around exec.Command so we can substitute a test version
// in code without changing its usage.
type Command interface {
Run() error
}
// Commander wraps the exec.Command constructor for use in testing.
type Commander func(name string, args ...string) Command
var cmdExecutor Commander
func setExecutor(executor Commander) {
cmdExecutor = executor
}
func newExecutor(name string, args ...string) Command {
if cmdExecutor == nil {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd
}
return cmdExecutor(name, args...)
}
func runCommand(name string, args []string) error {
log.Debugf("Running: %s %s", name, strings.Join(args, " "))
command := newExecutor(name, args...)
return command.Run()
}
// RunDockerCompose runs docker-compose with the specified subcommand and
// arguments.
func runDockerCompose(cmd, project string, composePaths []string, args ...string) {
cmdLine := []string{"-p", project}
for _, path := range composePaths {
cmdLine = append(cmdLine, "-f", path)
}
// one of build, exec, etc.
cmdLine = append(cmdLine, cmd)
// append any additional arguments or flags, i.e., -d
cmdLine = append(cmdLine, args...)
runCommand("docker-compose", cmdLine)
}
// RunComposeBuild runs docker-compose build with the specified docker compose
// files and args.
func RunComposeBuild(project string, composePaths []string, args ...string) {
runDockerCompose("build", project, composePaths, args...)
}
// RunComposeUp runs docker-compose up with the specified docker compose
// files and args.
func RunComposeUp(project string, composePaths []string, args ...string) {
runDockerCompose("up", project, composePaths, args...)
}
// RunComposePs runs docker-compose ps with the specified docker compose
// files and args.
func RunComposePs(project string, composePaths []string, args ...string) {
runDockerCompose("ps", project, composePaths, args...)
}
// RunComposeLogs runs docker-compose logs with the specified docker compose
// files and args.
func RunComposeLogs(project string, composePaths []string, args ...string) {
runDockerCompose("logs", project, composePaths, args...)
}
// RunComposeDown runs docker-compose down with the specified docker compose
// files and args.
func RunComposeDown(project string, composePaths []string, args ...string) {
runDockerCompose("down", project, composePaths, args...)
}
// RunOnContainer runs the commands on the container with the specified
// name using the 'docker' command.
func RunOnContainer(prefix, containerName string, cmds ...string) {
cmdLine := []string{"exec"}
// avoid "input device is not a tty error"
if isatty.IsTerminal(os.Stdout.Fd()) {
cmdLine = append(cmdLine, "-it")
}
cmdLine = append(cmdLine, docker.ExpandedContainerName(prefix, containerName))
cmdLine = append(cmdLine, cmds...)
err := runCommand("docker", cmdLine)
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
os.Exit(exitError.ExitCode())
} else {
log.Fatalf("runCommand: %v", err)
}
}
}