Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit d05a74a

Browse files
committed
Initial wrapper
1 parent 4a10953 commit d05a74a

File tree

7 files changed

+133
-1
lines changed

7 files changed

+133
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# docker-compose-wrapper
1+
# Docker-Compose wrapper

docker-compose-test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: "3.9"
2+
services:
3+
redis:
4+
image: "redis:alpine"

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/portainer/docker-compose-wrapper
2+
3+
go 1.15

utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package wrapper
2+
3+
import (
4+
"os/exec"
5+
"path"
6+
"runtime"
7+
)
8+
9+
func osProgram(program string) string {
10+
if runtime.GOOS == "windows" {
11+
program += ".exe"
12+
}
13+
return program
14+
}
15+
16+
func programPath(rootPath, program string) string {
17+
return path.Join(rootPath, osProgram(program))
18+
}
19+
20+
// IsBinaryPresent check if docker compose binary is present
21+
func IsBinaryPresent(program string) bool {
22+
_, err := exec.LookPath(osProgram(program))
23+
return err == nil
24+
}

utils_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package wrapper
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsBinaryPresent(t *testing.T) {
8+
type testCase struct {
9+
Name string
10+
Binary string
11+
Expected bool
12+
}
13+
testCases := []testCase{
14+
testCase{
15+
Name: "not existing",
16+
Binary: "qwgq-er-gerw",
17+
Expected: false,
18+
},
19+
testCase{
20+
Name: "docker-compose exists",
21+
Binary: "docker-compose",
22+
Expected: true,
23+
},
24+
}
25+
26+
for _, tc := range testCases {
27+
got := IsBinaryPresent(tc.Binary)
28+
if got != tc.Expected {
29+
t.Errorf("Error in test %s got = %v, and Expected = %v.", tc.Name, got, tc.Expected)
30+
}
31+
}
32+
33+
}

wrapper.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package wrapper
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"os/exec"
7+
)
8+
9+
// ComposeWrapper provide a type for managing docker compose commands
10+
type ComposeWrapper struct {
11+
binaryPath string
12+
}
13+
14+
// NewComposeWrapper initializes a new ComposeWrapper service with local docker-compose binary.
15+
func NewComposeWrapper() *ComposeWrapper {
16+
17+
return &ComposeWrapper{binaryPath: ""}
18+
}
19+
20+
// Up create and start containers
21+
func (wrapper *ComposeWrapper) Up(filePath string) ([]byte, error) {
22+
return wrapper.Command([]string{"up", "-d"}, filePath)
23+
}
24+
25+
// Down stop and remove containers
26+
func (wrapper *ComposeWrapper) Down(filePath string) ([]byte, error) {
27+
return wrapper.Command([]string{"down", "--remove-orphans"}, filePath)
28+
}
29+
30+
// Command exectue a docker-compose commanåd
31+
func (wrapper *ComposeWrapper) Command(args []string, filePath string) ([]byte, error) {
32+
program := programPath(wrapper.binaryPath, "docker-compose")
33+
34+
args = append([]string{"-f", filePath}, args...)
35+
36+
var stderr bytes.Buffer
37+
cmd := exec.Command(program, args...)
38+
cmd.Stderr = &stderr
39+
40+
output, err := cmd.Output()
41+
if err != nil {
42+
return nil, errors.New(stderr.String())
43+
}
44+
45+
return output, nil
46+
}

wrapper_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package wrapper
2+
3+
import (
4+
"log"
5+
"testing"
6+
)
7+
8+
func TestCommand(t *testing.T) {
9+
w := NewComposeWrapper()
10+
11+
file := "docker-compose-test.yml"
12+
_, err := w.Up(file)
13+
if err != nil {
14+
log.Fatalln(err)
15+
}
16+
17+
_, err = w.Down(file)
18+
if err != nil {
19+
log.Fatalln(err)
20+
}
21+
22+
}

0 commit comments

Comments
 (0)