-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.go
67 lines (63 loc) · 1.84 KB
/
compose.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
package wfi
import (
"fmt"
"io"
"log"
"os/exec"
)
// Up spins up docker-compose using `up` command
func Up(execLoc, fileName string, options ...string) error {
params := make([]string, 0)
if len(fileName) > 0 {
params = append(params, "-f", fileName)
}
params = append(params, "up", "-d")
params = append(params, options...)
cmd := exec.Command("docker-compose", params...)
if len(execLoc) > 0 {
cmd.Dir = execLoc
}
if msg, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("cannot spin up docker-compose %s \n %v", msg, err)
}
return nil
}
// UpWithLogs spins up docker-compose using `up` command but not in detach mode
// One should never pass in "-d" as option because it will display logs
func UpWithLogs(execLoc, fileName string, options ...string) (io.ReadCloser, error) {
params := make([]string, 0)
if len(fileName) > 0 {
params = append(params, "-f", fileName)
}
params = append(params, "up")
params = append(params, options...)
cmd := exec.Command("docker-compose", params...)
if len(execLoc) > 0 {
cmd.Dir = execLoc
}
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("cannot spin up docker-compose %v", err)
}
return rc, nil
}
// Down cleans up services with docker-compose using `down` command
func Down(execLoc, fileName string) {
params := make([]string, 0)
if len(fileName) > 0 {
params = append(params, "-f", fileName)
}
params = append(params, "down")
cmd := exec.Command("docker-compose", params...)
if len(execLoc) > 0 {
cmd.Dir = execLoc
}
if msg, err := cmd.CombinedOutput(); err != nil {
// Just exit here. In most cases, we'll have to fix the containers manually
log.Fatal(fmt.Errorf("cannot clean up with docker-compose %s \n %v", msg, err))
}
log.Printf("Succesfully cleaned up docker-compose %v", fileName)
}