-
Notifications
You must be signed in to change notification settings - Fork 6
/
intellij_datasource_file.go
58 lines (46 loc) · 1.51 KB
/
intellij_datasource_file.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
package amanar
import (
"github.com/beevik/etree"
)
// Tested with DataGrip 2017.2
func NewIntellijDatasourceFile(filepath string) (*IntellijDatasourceFile, error) {
d := etree.NewDocument()
err := d.ReadFromFile(string(filepath))
if err != nil {
return nil, err
}
dc := &IntellijDatasourceFile{
Document: d,
Fullpath: filepath,
}
return dc, nil
}
// A IntellijDatasourceFile is an XML document containing
// This struct and methods allows updating of the username in
// such a configuration.
// DataGrip can store usernames in its configuration and passwords
// in the Keyring, or it can store both a username and password
// in a URL-like format in its config files. This updater assumes
// that the former is the case.
type IntellijDatasourceFile struct {
Document *etree.Document
Fullpath string
}
func (dc *IntellijDatasourceFile) UpdateUsername(databaseUuid string, newUsername string) (oldUsername string, err error) {
component := dc.Document.SelectElement("project").SelectElement("component")
for _, dataSource := range component.SelectElements("data-source") {
if uuid := dataSource.SelectAttrValue("uuid", ""); string(uuid) == databaseUuid {
username := dataSource.SelectElement("user-name")
if username == nil {
username = dataSource.CreateElement("user-name")
}
oldUsername = username.Text()
username.SetText(newUsername)
return oldUsername, nil
}
}
return "", nil
}
func (dc *IntellijDatasourceFile) WriteToFile() error {
return dc.Document.WriteToFile(string(dc.Fullpath))
}