This repository was archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcu.go
99 lines (86 loc) · 3.18 KB
/
dcu.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
package dcu
import (
"errors"
"io/ioutil"
"os"
"strings"
"github.com/compose-generator/dcu/model"
yaml "gopkg.in/yaml.v3"
)
// ---------------------------------------------------------- Serializing / Deserializing ----------------------------------------------------------
// DeserializeFromString takes a yaml string and converts it to a ComposeFile object
func DeserializeFromString(yamlString string) (composeFile model.ComposeFile, err error) {
err = yaml.Unmarshal([]byte(yamlString), &composeFile)
return
}
// DeserializeFromFile reads from a yaml file and converts it to a ComposeFile object
func DeserializeFromFile(path string) (composeFile model.ComposeFile, err error) {
if !strings.HasSuffix(path, ".yml") && !strings.HasSuffix(path, ".yaml") {
return model.ComposeFile{}, errors.New("the file must be of file type yml or yaml")
}
yamlFile, err := os.Open(path)
if err != nil {
return
}
bytes, err := ioutil.ReadAll(yamlFile)
if err != nil {
return
}
err = yaml.Unmarshal(bytes, &composeFile)
return
}
// SerializeToString converts a ComposeFile object to a yaml string
func SerializeToString(composeFile model.ComposeFile) (yamlString string, err error) {
bytes, err := yaml.Marshal(&composeFile)
return string(bytes), err
}
// SerializeToFile writes a ComposeFile object to a yaml file
func SerializeToFile(composeFile model.ComposeFile, path string) (err error) {
if !strings.HasSuffix(path, ".yml") && !strings.HasSuffix(path, ".yaml") {
return errors.New("the file must be of file type yml or yaml")
}
output, err := yaml.Marshal(&composeFile)
if err != nil {
return
}
err = ioutil.WriteFile(path, output, 0777)
return
}
// ---------------------------------------------------------------- Helper functions ---------------------------------------------------------------
// GetVolumePathsFromComposeFile deserializes a compose file and returns all paths of volumes
func GetVolumePathsFromComposeFilePath(composeFilePath string) []string {
composeFile, err := DeserializeFromFile(composeFilePath)
if err != nil {
panic(err)
}
return GetVolumePathsFromComposeFile(composeFile)
}
// GetVolumePathsFromComposeFile returns all paths of volumes
func GetVolumePathsFromComposeFile(composeFile model.ComposeFile) (filePaths []string) {
for _, service := range composeFile.Services {
for _, volumeStmt := range service.Volumes {
volumePath := strings.Split(volumeStmt, ":")[0]
if strings.HasPrefix(volumePath, "./") || strings.HasPrefix(volumePath, "/") {
filePaths = append(filePaths, volumePath)
}
}
}
return
}
// GetEnvFilePathsFromComposeFilePath deserializes a compose file and returns all paths of env files
func GetEnvFilePathsFromComposeFilePath(composeFilePath string) []string {
composeFile, err := DeserializeFromFile(composeFilePath)
if err != nil {
panic(err)
}
return GetEnvFilePathsFromComposeFile(composeFile)
}
// GetEnvFilePathsFromComposeFile returns all paths of env files
func GetEnvFilePathsFromComposeFile(composeFile model.ComposeFile) (filePaths []string) {
for _, service := range composeFile.Services {
for _, envFilePath := range service.EnvFile {
filePaths = appendStringToSliceIfMissing(filePaths, envFilePath)
}
}
return
}