forked from garetht/amanar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intellij_run_config_file.go
67 lines (52 loc) · 1.7 KB
/
intellij_run_config_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
59
60
61
62
63
64
65
66
67
package amanar
import (
"github.com/beevik/etree"
"net/url"
"log"
)
func NewIntellijRunConfig(filepath string) (*IntellijRunConfig, error) {
d := etree.NewDocument()
err := d.ReadFromFile(string(filepath))
if err != nil {
return nil, err
}
rc := &IntellijRunConfig{
Document: d,
Fullpath: filepath,
}
return rc, nil
}
type IntellijRunConfig struct {
Document *etree.Document
Fullpath string
}
func (rc *IntellijRunConfig) UpdateEnvironmentVariable(environmentVariable, databaseHost string, credentials *Credentials) (err error) {
envVars := rc.Document.SelectElement("component").SelectElement("configuration").SelectElement("envs")
for _, envVar := range envVars.SelectElements("env") {
if envVarName := envVar.SelectAttrValue("name", ""); envVarName == environmentVariable {
value := envVar.SelectAttrValue("value", "")
updatedValue, err := createOrUpdateUsernamePasswordWithHost(value, databaseHost, credentials.Username, credentials.Password)
if err != nil {
return err
}
envVar.CreateAttr("value", updatedValue)
}
}
return nil
}
func (rc *IntellijRunConfig) WriteToFile() (err error) {
log.Printf("[RUN CONFIGS] Writing new run configuration to file %s", rc.Fullpath)
return rc.Document.WriteToFile(rc.Fullpath)
}
func createOrUpdateUsernamePasswordWithHost(urlValue, databaseHost, username, password string) (string, error) {
parsedUrl, err := url.Parse(urlValue)
if err != nil {
return "", err
}
if databaseHost == parsedUrl.Host {
log.Printf("[RUN CONFIGS] Updating URL %s with username %s and password %s", urlValue, username, password)
newUserInfo := url.UserPassword(username, password)
parsedUrl.User = newUserInfo
}
return parsedUrl.String(), nil
}