-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (102 loc) · 2.87 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"gopkg.in/yaml.v3"
)
func main() {
dockerFilesPaths := os.Args[1:]
if len(dockerFilesPaths) < 1 {
log.Fatal("You must supply at least one docker compose file and in the format docker-compose_project.yml")
return
}
projectNames := make([]string, 0)
for _, path := range dockerFilesPaths {
projectNames = append(projectNames, getProjectName(path))
}
resultCompose := DockerCompose{}
for i, path := range dockerFilesPaths {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatal(err)
}
p := DockerCompose{}
err = yaml.Unmarshal(content, &p)
if err != nil {
log.Fatal(err)
}
for serviceKey, serviceValue := range p.Services {
if resultCompose.Services == nil {
resultCompose.Services = make(map[string]Service)
}
for networkKey, networkValue := range serviceValue.Networks {
if !isProjectPrefixPresent(projectNames, networkKey) {
serviceValue.Networks[projectNames[i]+"_"+networkKey] = networkValue
delete(serviceValue.Networks, networkKey)
}
}
resultCompose.Services[serviceKey] = serviceValue
}
if p.Networks != nil {
for networkKey, networkValue := range p.Networks {
if resultCompose.Networks == nil {
resultCompose.Networks = make(map[string]Network)
}
networkValue.Name = projectNames[i]+"_"+networkKey
if !p.Networks[networkKey].External {
resultCompose.Networks[projectNames[i]+"_"+networkKey] = networkValue
}
}
}
if p.Volumes != nil {
for volumeKey, volumeValue := range p.Volumes {
if resultCompose.Volumes == nil {
resultCompose.Volumes = make(map[string]Volume)
}
resultCompose.Volumes[volumeKey] = volumeValue
}
}
if p.Secrets != nil {
for secretKey, secretValue := range p.Secrets {
if resultCompose.Secrets == nil {
resultCompose.Secrets = make(map[string]Secret)
}
resultCompose.Secrets[secretKey] = secretValue
}
}
if p.Configs != nil {
for configKey, configValue := range p.Configs {
if resultCompose.Configs == nil {
resultCompose.Configs = make(map[string]Config)
}
resultCompose.Configs[configKey] = configValue
}
}
}
fmt.Println(resultCompose)
resultComposeFile, err := os.OpenFile("docker-compose_full.yml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
log.Fatalf("Error opening/creating file: %v", err)
}
defer resultComposeFile.Close()
enc := yaml.NewEncoder(resultComposeFile)
err = enc.Encode(resultCompose)
if err != nil {
log.Fatal(err)
}
}
func isProjectPrefixPresent(projectNames []string, networkKey string) bool {
for _, projectName := range projectNames {
if strings.HasPrefix(networkKey, projectName+"_") {
return true
}
}
return false
}
func getProjectName(filename string) string {
parts := strings.Split(filename, "_")
return strings.Replace(parts[len(parts)-1], ".yml", "", -1)
}