-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
165 lines (146 loc) · 4.19 KB
/
output.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package output
import (
"bytes"
"fmt"
urlparser "net/url"
"os"
"strconv"
"strings"
compose "github.com/benjdewan/gocomposeapi"
"github.com/ghodss/yaml"
)
type outputYAML struct {
Type string `json:"type"`
CACert string `json:"cacert,omitempty"`
Version string `json:"version"`
Connections []connectionYAML `json:"connections"`
}
// codebeat:disable[TOO_MANY_IVARS]
type connectionYAML struct {
Scheme string `json:"scheme"`
Host string `json:"host"`
Port int `json:"port,omitempty"`
Path string `json:"path,omitempty"`
Query string `json:"query,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
// codebeat:enable[TOO_MANY_IVARS]
// Builder is the stateful object used to build pachelbel's output files
type Builder struct {
endpointMap map[string]string
yml [][]byte
}
// New returns an initialized Builder object
func New(endpointMap map[string]string) *Builder {
return &Builder{
endpointMap: endpointMap,
yml: [][]byte{},
}
}
// Add takes a compose.Deployment object and converts its connection
// information into Builder's internal representation
func (b *Builder) Add(deployment *compose.Deployment) error {
yamlObject, err := b.convert(deployment)
b.yml = append(b.yml, yamlObject)
return err
}
func (b *Builder) Write(file string) error {
outBytes := bytes.Join(b.yml, []byte("\n"))
handle, err := os.Create(file)
if err != nil {
return err
}
defer func() {
if closeErr := handle.Close(); closeErr != nil {
// Our fd became invalid, or the underlying
// syscall was interrupted
panic(closeErr)
}
}()
_, err = handle.Write(outBytes)
return err
}
func (b *Builder) convert(deployment *compose.Deployment) ([]byte, error) {
connections, err := b.convertConnections(deployment.Connection.Direct)
if err != nil {
return []byte{}, err
}
outYAML := make(map[string]outputYAML)
outYAML[deployment.Name] = outputYAML{
Type: deployment.Type,
CACert: deployment.CACertificateBase64,
Version: deployment.Version,
Connections: connections,
}
return yaml.Marshal(outYAML)
}
func (b *Builder) convertConnections(cStrings []string) ([]connectionYAML, error) {
connections := []connectionYAML{}
for _, cString := range cStrings {
newCXNs, err := b.convertConnection(cString)
if err != nil {
return connections, err
}
connections = append(connections, newCXNs...)
}
return connections, nil
}
func (b *Builder) convertConnection(cString string) ([]connectionYAML, error) {
if strings.HasPrefix(cString, "mongodb") && strings.Contains(cString, ",") {
return b.convertMongoConnection(cString)
}
url, err := urlparser.Parse(cString)
if err != nil {
return []connectionYAML{}, err
}
host, err := b.resolveHost(url.Hostname())
if err != nil {
return []connectionYAML{}, err
}
password, _ := url.User.Password()
connection := connectionYAML{
Scheme: url.Scheme,
Host: host,
Path: url.Path,
Query: url.Query().Encode(),
Username: url.User.Username(),
Password: password,
}
if port, err := strconv.Atoi(url.Port()); err == nil {
connection.Port = port
}
return []connectionYAML{connection}, nil
}
func (b *Builder) convertMongoConnection(mongoString string) ([]connectionYAML, error) {
prefix, hosts, suffix := mongoSplit(mongoString)
cStrings := []string{}
for _, host := range strings.Split(hosts, ",") {
cStrings = append(cStrings, mongoJoin(prefix, host, suffix))
}
return b.convertConnections(cStrings)
}
func mongoSplit(mongoStr string) (string, string, string) {
splitPrefix := strings.Split(mongoStr, "@")
prefix := splitPrefix[0]
splitSuffix := strings.Split(splitPrefix[1], "/")
return prefix, splitSuffix[0], splitSuffix[1]
}
func mongoJoin(prefix, host, suffix string) string {
return strings.Join([]string{strings.Join([]string{prefix, host}, "@"), suffix}, "/")
}
func (b *Builder) resolveHost(host string) (string, error) {
i := 0
for {
if i >= 8 {
return host, fmt.Errorf("Recursive endpoint mapping is limited to 8 layers. '%s' exceeds that", host)
}
if rename, ok := b.endpointMap[host]; ok {
host = rename
i++
continue
}
break
}
return host, nil
}