-
Notifications
You must be signed in to change notification settings - Fork 4
/
connection.go
169 lines (158 loc) · 5.02 KB
/
connection.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
166
167
168
169
package mysqlworkbench
import (
"encoding/xml"
"fmt"
uuid "github.com/satori/go.uuid"
)
func ConnectionsXML(name string, host string, port int, sslCertPath, sslKeyPath, dbName string) (string, error) {
id := uuid.NewV4().String()
conns := xmlDoc{
GrtFormat: "2.0",
Connections: conns{
attrs: attrs{Ptr: "abc123", Type: "list", ContentType: "object", ContentStructName: "db.mgmt.Connection"},
Connections: []conn{
{
attrs: attrs{Type: "object", StructName: "db.mgmt.Connection", ID: id, StructChecksum: "def456"},
Link: link{
attrs: attrs{Type: "object", StructName: "db.mgmt.Driver", Key: "driver"},
Data: "com.mysql.rdbms.mysql.driver.native",
},
Values: []value{
{
attrs: attrs{Type: "string", Key: "name"},
Data: name,
},
{
attrs: attrs{Type: "string", Key: "hostIdentifier"},
Data: fmt.Sprintf("Mysql@%s:%d", host, port),
},
},
NestedValues: []params{
{
attrs: attrs{Ptr: "abc123", Type: "dict", Key: "modules"},
},
{
attrs: attrs{Ptr: "abc123", Type: "dict", Key: "parameterValues"},
Values: []value{
{
attrs: attrs{Type: "string", Key: "SQL_MODE"},
},
{
attrs: attrs{Type: "string", Key: "hostName"},
Data: host,
},
{
attrs: attrs{Type: "string", Key: "password"},
},
{
attrs: attrs{Type: "int", Key: "port"},
Data: fmt.Sprint(port),
},
{
attrs: attrs{Type: "string", Key: "schema"},
Data: dbName,
},
{
attrs: attrs{Type: "string", Key: "sslCA"},
},
{
attrs: attrs{Type: "string", Key: "sslCert"},
Data: sslCertPath,
},
{
attrs: attrs{Type: "string", Key: "sslCipher"},
},
{
attrs: attrs{Type: "string", Key: "sslKey"},
Data: sslKeyPath,
},
{
attrs: attrs{Type: "int", Key: "useSSL"},
Data: "1",
},
{
attrs: attrs{Type: "string", Key: "userName"},
Data: "placeholder",
},
},
},
},
},
},
},
}
output, err := xml.MarshalIndent(&conns, "", " ")
if err != nil {
return "", fmt.Errorf("error generating MySQL Workbench connections.xml: %w", err)
}
return xml.Header + string(output), nil
}
// Example:
//
// <?xml version="1.0"?>
// <data grt_format="2.0">
// <value _ptr_="abc123" type="list" content-type="object" content-struct-name="db.mgmt.Connection">
// <value type="object" struct-name="db.mgmt.Connection" id="7e4b13cc-a48c-11ec-8ef1-02a8c4b46216" struct-checksum="def456">
// <link type="object" struct-name="db.mgmt.Driver" key="driver">com.mysql.rdbms.mysql.driver.native</link>
// <value type="string" key="name">db.api.staging.us.border0</value>
// <value type="string" key="hostIdentifier">Mysql@db.api.staging.us.border0:30583</value>
// <value type="int" key="isDefault">0</value>
// <value _ptr_="abc123" type="dict" key="modules"/>
// <value _ptr_="abc123" type="dict" key="parameterValues">
// <value type="string" key="SQL_MODE"></value>
// <value type="string" key="hostName">db.api.staging.us.border0</value>
// <value type="string" key="password"></value>
// <value type="int" key="port">30583</value>
// <value type="string" key="schema">border0</value>
// <value type="string" key="sslCA"></value>
// <value type="string" key="sslCert">/Users/rollie/.border0/still-voice-9340.border0.io.crt</value>
// <value type="string" key="sslCipher"></value>
// <value type="string" key="sslKey">/Users/rollie/.border0/still-voice-9340.border0.io.key</value>
// <value type="int" key="useSSL">1</value>
// <value type="string" key="userName">placeholder</value>
// </value>
// </value>
// </value>
// </data>
type xmlDoc struct {
XMLName xml.Name `xml:"data"`
GrtFormat string `xml:"grt_format,attr"`
Connections conns
}
type conns struct {
XMLName xml.Name `xml:"value"`
Connections []conn
attrs
}
type conn struct {
XMLName xml.Name `xml:"value"`
Link link
Values []value
NestedValues []params
attrs
}
type link struct {
XMLName xml.Name `xml:"link"`
Data string `xml:",chardata"`
attrs
}
type value struct {
XMLName xml.Name `xml:"value"`
Data string `xml:",chardata"`
attrs
}
type params struct {
XMLName xml.Name `xml:"value"`
Values []value `xml:",omitempty"`
attrs
}
type attrs struct {
Ptr string `xml:"_ptr_,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
ContentType string `xml:"content-type,attr,omitempty"`
ContentStructName string `xml:"content-struct-name,attr,omitempty"`
StructName string `xml:"struct-name,attr,omitempty"`
ID string `xml:"id,attr,omitempty"`
StructChecksum string `xml:"struct-checksum,attr,omitempty"`
Key string `xml:"key,attr,omitempty"`
}