-
Notifications
You must be signed in to change notification settings - Fork 7
/
command.go
151 lines (124 loc) · 4.12 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package dev
import (
"os"
"os/exec"
"strings"
"github.com/mattn/go-isatty"
log "github.com/sirupsen/logrus"
)
// 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(cwd string, 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
cmd.Dir = cwd
return cmd
}
return cmdExecutor(name, args...)
}
func RunCommandInDir(cwd string, name string, args []string) error {
log.Debugf("Running: %s %s", name, strings.Join(args, " "))
command := newExecutor(cwd, name, args...)
return command.Run()
}
func RunCommand(name string, args []string) error {
path, err := os.Getwd()
if err != nil {
log.Println(err)
}
return RunCommandInDir(path, name, args)
}
// RunDockerCompose runs docker-compose with the specified subcommand and
// arguments.
func runDockerCompose(cmd, project string, composePaths []string, args ...string) {
cmdLine := []string{"compose", "--compatibility", "-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
for _, arg := range args {
cmdLine = append(cmdLine, arg)
}
RunCommand("docker", 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...)
}
// RunComposePull runs docker-compose build with the specified docker compose
// files and args.
func RunComposePull(project string, composePaths []string, args ...string) {
runDockerCompose("pull", 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(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, containerName)
for _, cmd := range cmds {
cmdLine = append(cmdLine, cmd)
}
err := RunCommand("docker", cmdLine)
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
os.Exit(exitError.ExitCode())
} else {
log.Fatalf("RunCommand: %v", err)
}
}
}
// RunDobi runs dobi build with the specified args
func RunDobi(dir string, args ...string) {
// Unlike docker-compose, dobi needs to run in the same directory as
// the project.
RunCommandInDir(dir, "dobi", args)
}
// RunDockerPull runs docker pull with the specified remote image
func RunDockerPull(img string) {
cmdLine := []string{"pull", img}
RunCommand("docker", cmdLine)
}
// RunDockerPull runs docker tag
func RunDockerTag(from string, to string) {
cmdLine := []string{"tag", from, to}
RunCommand("docker", cmdLine)
}