-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_windows.go
50 lines (39 loc) · 1.21 KB
/
database_windows.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
//go:build windows
package utils
import (
"encoding/binary"
"fmt"
"github.com/danieljoos/wincred"
"gopkg.in/ini.v1"
)
func CreatePostgresDatabaseURL() (string, error) {
var err error
// Open ini file
configFile := GetConfigFile()
cfg, err := ini.Load(configFile)
if err != nil {
return "", err
}
user, err := cfg.Section("DB").GetKey("PostgresUser")
if err != nil {
return "", fmt.Errorf("could not read PostgresUser from INI")
}
host, err := cfg.Section("DB").GetKey("PostgresHost")
if err != nil {
return "", fmt.Errorf("could not read PostgresHost from INI")
}
port, err := cfg.Section("DB").GetKey("PostgresPort")
if err != nil {
return "", fmt.Errorf("could not read PostgresPort from INI")
}
database, err := cfg.Section("DB").GetKey("PostgresDatabase")
if err != nil {
return "", fmt.Errorf("could not read PostgresDatabase from INI")
}
pass, err := wincred.GetGenericCredential(host.String() + ":" + port.String())
if err != nil {
return "", fmt.Errorf("could not read password from Windows Credential Manager")
}
decodedPass := UTF16BytesToString(pass.CredentialBlob, binary.LittleEndian)
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, decodedPass, host, port, database), nil
}