-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
70 lines (60 loc) · 1.66 KB
/
yaml.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
package dockerCompose
import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
)
type DockerYAML struct {
Version string `yaml:"version"`
Networks map[string]*Network `yaml:"networks"`
Services map[string]*Service `yaml:"services"`
// Volumes []string `yaml:"volumes"`
// Driver []string `yaml:"driver"`
}
func GenerateDockerYAML(version string) *DockerYAML {
return &DockerYAML{
Version: version,
Services: map[string]*Service{},
Networks: map[string]*Network{},
// Volumes: []string{},
// Driver: []string{},
}
}
func (that *DockerYAML) AddNetwork(network *Network) error {
if nil == network {
return errors.New("Nil pointer of network struct. ")
}
if nil == that.Networks[network.Name] {
that.Networks[network.Name] = network
} else {
return fmt.Errorf("Duplicate key for network %s. ", network.Name)
}
return nil
}
func (that *DockerYAML) AddService(service *Service) error {
if nil == service {
return errors.New("Nil pointer of service struct. ")
}
if nil == that.Networks[service.ContainerName] {
that.Services[service.ContainerName] = service
} else {
return fmt.Errorf("Duplicate key for service %s. ", service.ContainerName)
}
return nil
}
func (that *DockerYAML) ExportToByteArray() ([]byte, error) {
return yaml.Marshal(that)
}
func (that *DockerYAML) ExportToFile(path string, fileName string) error {
return nil
}
func UpWithDockerComposeFileCommand(filePath string) []string {
return []string{
fmt.Sprintf("docker-compose -f %s up -d", filePath),
}
}
func DownWithDockerComposeFileCommand(filePath string) []string {
return []string{
fmt.Sprintf("docker-compose -f %s down", filePath),
}
}