-
Notifications
You must be signed in to change notification settings - Fork 0
/
starter.go
86 lines (67 loc) · 2 KB
/
starter.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
package sidecar
import (
"fmt"
"os"
"os/exec"
"path"
)
// Starts docker compose on 'docker-compose.yml' from configuration folder
// Does not wait finish.
func StartServices() (stopServices func(), err error) {
confFolder, err := ConfFolder()
if err != nil {
return nil, err
}
composePath := path.Join(confFolder, "docker-compose.yml")
return StartServicesWithCompose(composePath)
}
func StartServicesWithCompose(composePath string) (stopServices func(), err error) {
_, err = os.ReadFile(composePath)
if err != nil {
fmt.Println("docker-compose.yml does not exist. Please start required services manually")
return func() {}, nil
}
go newCompositeCmd(composePath, "up").Run()
return newCompositeCmd(composePath, "down").Run, nil
}
type compositeCmd struct {
composePath string
command string
}
func newCompositeCmd(composePath, command string) *compositeCmd {
result := compositeCmd{command: command, composePath: composePath}
return &result
}
func (cmd *compositeCmd) Run() {
osCMD := exec.Command("docker")
osCMD.Args = append(osCMD.Args, "compose")
osCMD.Args = append(osCMD.Args, "-f")
osCMD.Args = append(osCMD.Args, cmd.composePath)
osCMD.Args = append(osCMD.Args, cmd.command)
osCMD.Run()
return
}
// Loads docker-compose.yml file to configuration folder with name "docker-compose.yml"
// for further using by StartServices
func LoadDockerComposeFile(url string) error {
if len(url) == 0 {
return fmt.Errorf("wrong url")
}
confFolder, err := ConfFolder()
if err != nil {
return err
}
composePath := path.Join(confFolder, "docker-compose.yml")
if _, err = os.ReadFile(composePath); err == nil {
os.Remove(composePath)
}
// curl -s https://memphisdev.github.io/memphis-docker/docker-compose-dev.yml -o docker-compose.yaml
osCMD := exec.Command("curl")
osCMD.Args = append(osCMD.Args, "-s")
osCMD.Args = append(osCMD.Args, url)
osCMD.Args = append(osCMD.Args, "-o")
osCMD.Args = append(osCMD.Args, composePath)
osCMD.Run()
_, err = os.ReadFile(composePath)
return err
}