This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dockercompose.go
190 lines (158 loc) · 4.96 KB
/
dockercompose.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"path/filepath"
"strings"
"github.com/samber/lo"
)
// DockerCompose type
type DockerCompose struct {
_ExecCmd func([]string, bool) (string, error)
Command []string
Containers []string
Envs []string
Files []string
ProjectName string
Running bool
RunServices []string
Service string
User string
WorkDir string
}
func (d *DockerCompose) cmd(args ...string) []string {
cmd := []string{"docker", "compose", "--project-name", d.ProjectName}
for _, file := range d.Files {
cmd = append(cmd, "--file", file)
}
cmd = append(cmd, args...)
return cmd
}
// Init initialize compose settings
func (d *DockerCompose) Init(c *DevContainer) error {
d._ExecCmd = lo.Ternary(d._ExecCmd != nil, d._ExecCmd, execCmd)
d.Envs = lo.MapToSlice(
c.Config.GetStringMapString("remoteEnv"),
func(k string, v string) string { return k + "=" + v },
)
d.Files = lo.Map(
c.Config.GetStringSlice("dockerComposeFile"),
func(v string, _ int) string { return filepath.Join(c.ConfigDir, v) },
)
d.ProjectName = c.Config.GetString("name") + "_devcontainer"
d.RunServices = c.Config.GetStringSlice("runServices")
d.Service = c.Config.GetString("service")
d.User = c.Config.GetString("remoteUser")
d.WorkDir = c.Config.GetString("workspaceFolder")
// check if already started
if running, err := d.IsRunning(); err != nil {
return err
} else if running {
d.Running = true
}
return nil
}
// IsBuilt return the image build status
func (d *DockerCompose) IsBuilt() (bool, error) {
cmdArgs := d.cmd("images")
cmdArgs = append(cmdArgs, "--quiet")
out, err := d._ExecCmd(cmdArgs, true)
images := lo.Filter(strings.Split(out, "\n"), func(x string, _ int) bool { return x != "" })
built := len(images) > 0
return built, err
}
// IsCreated return the container creation status
func (d *DockerCompose) IsCreated() (bool, error) {
cmdArgs := d.cmd("ps")
cmdArgs = append(cmdArgs, "--quiet")
out, err := d._ExecCmd(cmdArgs, true)
containers := lo.Filter(strings.Split(out, "\n"), func(x string, _ int) bool { return x != "" })
created := len(containers) > 0
return created, err
}
// IsRunning return the container running status
func (d *DockerCompose) IsRunning() (bool, error) {
cmdArgs := d.cmd("ps")
cmdArgs = append(cmdArgs, "--quiet")
cmdArgs = append(cmdArgs, "--status", "running")
out, err := d._ExecCmd(cmdArgs, true)
containers := lo.Filter(strings.Split(out, "\n"), func(x string, _ int) bool { return x != "" })
running := len(containers) > 0
return running, err
}
// Build build the container with the given image
func (d *DockerCompose) Build() (string, error) {
cmdArgs := d.cmd("build")
if len(d.RunServices) > 0 {
cmdArgs = append(cmdArgs, d.RunServices...)
}
return d._ExecCmd(cmdArgs, false)
}
// Create create the container with the given image
func (d *DockerCompose) Create() (string, error) {
cmdArgs := d.cmd("create")
if len(d.RunServices) > 0 {
cmdArgs = append(cmdArgs, d.RunServices...)
}
return d._ExecCmd(cmdArgs, false)
}
// Start start the given container
func (d *DockerCompose) Start() (string, error) {
cmdArgs := d.cmd("up", "--detach")
if len(d.RunServices) > 0 {
cmdArgs = append(cmdArgs, d.RunServices...)
}
return d._ExecCmd(cmdArgs, false)
}
// Stop stop the given container
func (d *DockerCompose) Stop() (string, error) {
cmdArgs := d.cmd("stop")
if len(d.RunServices) > 0 {
cmdArgs = append(cmdArgs, d.RunServices...)
}
return d._ExecCmd(cmdArgs, false)
}
// Remove remove the given container
func (d *DockerCompose) Remove() (string, error) {
cmdArgs := d.cmd("down", "--volumes")
return d._ExecCmd(cmdArgs, false)
}
// List return the list of containers based on the given path
func (d *DockerCompose) List() (string, error) {
cmdArgs := d.cmd("ls")
return d._ExecCmd(cmdArgs, false)
}
// Exec execute the given command into the given container
func (d *DockerCompose) Run(command []string) (string, error) {
cmdArgs := d.cmd("run")
cmdArgs = append(cmdArgs, "--workdir", d.WorkDir)
if d.User != "" {
cmdArgs = append(cmdArgs, "--user", d.User)
}
cmdArgs = append(cmdArgs, d.Service)
cmdArgs = append(cmdArgs, command...)
return d._ExecCmd(cmdArgs, true)
}
// Exec execute the given command into the given container
func (d *DockerCompose) Exec(command []string) (string, error) {
cmdArgs := d.cmd("exec")
cmdArgs = append(cmdArgs, "--workdir", d.WorkDir)
if d.User != "" {
cmdArgs = append(cmdArgs, "--user", d.User)
}
// resolve containerEnv variables
d.Envs = lo.Map(d.Envs, func(v string, _ int) string { return resolveContainerEnv(d, v) })
for _, env := range d.Envs {
cmdArgs = append(cmdArgs, "--env", env)
}
cmdArgs = append(cmdArgs, d.Service)
cmdArgs = append(cmdArgs, command...)
return d._ExecCmd(cmdArgs, false)
}
// ResolveEnv resolve environment variable from inside the container
func (d *DockerCompose) ResolveEnv(env string) string {
cmd := []string{"echo", "$" + env}
resolved, err := d.Run(cmd)
if err != nil {
panic(err)
}
return resolved
}