forked from garetht/amanar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intellij_datasource_flow.go
65 lines (52 loc) · 1.87 KB
/
intellij_datasource_flow.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
package amanar
import (
"fmt"
"log"
)
func NewIntellijDatasourceFlow(config *IntellijDatasource) (*IntellijDatasourceFlow, error) {
datasourceFile, err := NewIntellijDatasourceFile(config.DatasourceFilePath)
if err != nil {
return nil, err
}
return &IntellijDatasourceFlow{
IntellijDatasource: *config,
datasourceFile: datasourceFile,
}, nil
}
// An IntellijDatasourceFlow knows how to update the username
// and password for a single database with a particular IntelliJ UUID.
// It does this by using dataSources.local.xml files.
type IntellijDatasourceFlow struct {
IntellijDatasource
datasourceFile *IntellijDatasourceFile
credentials *Credentials
}
func (ds *IntellijDatasourceFlow) Name() string {
return "INTELLIJ DATASOURCE"
}
func (ds *IntellijDatasourceFlow) UpdateWithCredentials(credentials *Credentials) (err error) {
_, err = ds.datasourceFile.UpdateUsername(ds.DatabaseUUID, credentials.Username)
if err != nil {
return
}
ds.credentials = credentials
return nil
}
func (ds *IntellijDatasourceFlow) PersistChanges() (err error) {
// Writing: side-effecting writes to files and forms of IO and things.
// In this case, we write to the IntellJ config file and the OSX keychain
// Should be done sequentially.
service := fmt.Sprintf("IntelliJ Platform DB — %s", ds.DatabaseUUID)
err = ds.datasourceFile.WriteToFile()
if err != nil {
return err
}
log.Printf("[INTELLIJ DATASOURCE %s] Writing new username %s and password %s to Keychain", service, ds.credentials.Username, ds.credentials.Password)
err = CreateOrUpdateKeychainEntriesForService(service, ds.credentials.Username, ds.credentials.Password, []string{})
if err != nil {
log.Print(err)
log.Fatalf("[INTELLIJ DATASOURCE %s] Could not create the new keychain entry with username %s and password %s", service, ds.credentials.Username, ds.credentials.Password)
return err
}
return
}