forked from garetht/amanar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_flow.go
86 lines (68 loc) · 2.32 KB
/
json_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package amanar
import (
"fmt"
"log"
"os"
"io/ioutil"
)
func NewJSONFlow(config *JSONDatasource) (*JSONFlow, error) {
file, err := os.OpenFile(config.Filepath, os.O_RDONLY|os.O_CREATE, 0644)
defer file.Close()
if err != nil {
return nil, fmt.Errorf("could not open and parse JSON file path '%s': %s", config.Filepath, err)
}
rawJSON, err := ioutil.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("could not read from JSON file '%s': %s", config.Filepath, err)
}
existingCredentials, err := UnmarshalJSONCredentials(rawJSON)
if err != nil {
existingCredentials = JSONCredentials{}
}
return &JSONFlow{
JSONDatasource: *config,
parsedFile: existingCredentials,
}, nil
}
type JSONFlow struct {
JSONDatasource
credentials *Credentials
parsedFile JSONCredentials
}
func (sf *JSONFlow) Name() string {
return "JSON"
}
func (sf *JSONFlow) UpdateWithCredentials(credentials *Credentials) error {
log.Printf("[%s DATASOURCE] Updating JSON credentials %s with new username %s and password %s", sf.Name(), sf.Filepath, credentials.Username, credentials.Password)
sf.credentials = credentials
found := false
for i, entry := range sf.parsedFile {
if entry.Identifier == sf.Identifier {
sf.parsedFile[i].Username = credentials.Username
sf.parsedFile[i].Password = credentials.Password
found = true
}
}
if !found {
sf.parsedFile = append(sf.parsedFile, JSONCredential{
Identifier: sf.Identifier,
Username: credentials.Username,
Password: credentials.Password,
})
}
log.Printf("[%s DATASOURCE] Updated JSON credentials %s with new username %s and password %s", sf.Name(), sf.Filepath, credentials.Username, credentials.Password)
return nil
}
func (sf *JSONFlow) PersistChanges() error {
log.Printf("[%s DATASOURCE] Writing new username %s and password %s to JSON file", sf.Name(), sf.credentials.Username, sf.credentials.Password)
writeData, err := sf.parsedFile.Marshal()
if err != nil {
return fmt.Errorf("could not marshal JSON to string: %#v", writeData)
}
err = ioutil.WriteFile(sf.Filepath, writeData, 0644)
if err != nil {
return fmt.Errorf("could not write JSON to file '%s': %s", sf.Filepath, writeData)
}
log.Printf("[%s DATASOURCE] Successfully wrote new username %s and password %s to JSON file", sf.Name(), sf.credentials.Username, sf.credentials.Password)
return nil
}