-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc.go
50 lines (43 loc) · 1.14 KB
/
cc.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
package ezdc
import (
"context"
"os"
"os/exec"
)
type ComposeFace interface {
Up(ctx context.Context) *exec.Cmd
Down(ctx context.Context) *exec.Cmd
Pull(ctx context.Context, svc ...string) *exec.Cmd
Build(ctx context.Context) *exec.Cmd
}
type DefaultComposeCmd struct {
project string
file string
}
func (c DefaultComposeCmd) cmd(ctx context.Context, args ...string) *exec.Cmd {
file := c.file
if file == "" {
file = "./docker-compose.yml"
}
cmd := exec.CommandContext(ctx, "docker",
append([]string{"compose", "-p", c.project, "-f", file}, args...)...,
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func (c DefaultComposeCmd) Down(ctx context.Context) *exec.Cmd {
return c.cmd(ctx, "down", "-v", "--remove-orphans", "--rmi", "local", "--timeout", "0")
}
func (c DefaultComposeCmd) Pull(ctx context.Context, svcs ...string) *exec.Cmd {
if len(svcs) == 0 {
return nil
}
return c.cmd(ctx, append([]string{"pull"}, svcs...)...)
}
func (c DefaultComposeCmd) Build(ctx context.Context) *exec.Cmd {
return c.cmd(ctx, "build")
}
func (c DefaultComposeCmd) Up(ctx context.Context) *exec.Cmd {
return c.cmd(ctx, "up")
}