/
ini.go
160 lines (122 loc) · 4.99 KB
/
ini.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package serializer
import (
"gopkg.in/ini.v1"
"os"
)
/*
ToIni is responsible for serializing a Credential and Profile to an ini file. Attribute sections are directly
translatable to sections in the Profile. Username and Password will have an appropriate label (either the default or an
alternate set in the Credential's related Factory.
*/
func (thisSerializer *Serializer) ToIni(username string, password string, attributes map[string]map[string]string) error {
thisSerializer.Factory.Log.Info().Msg("Serializing credential and profile to ini file.")
credentialErr := thisSerializer.saveCredentialIni(username, password)
if credentialErr != nil {
return credentialErr
}
profileErr := thisSerializer.saveProfileIni(attributes)
if profileErr != nil {
return profileErr
}
return nil
}
func (thisSerializer *Serializer) saveCredentialIni(username string, password string) error {
thisSerializer.Factory.Log.Info().Msg("Serializing credential to ini file.")
credentialIni, credIniError := initIni(thisSerializer.CredentialFile)
thisSerializer.Factory.Log.Trace().Msg("Getting alternate username and password labels.")
usernameKey := thisSerializer.Factory.GetAlternateUsername()
passwordKey := thisSerializer.Factory.GetAlternatePassword()
if credIniError != nil {
return credIniError
}
thisSerializer.Factory.Log.Trace().Msg("Adding username and password to credentials file.")
credentialIni.Section(thisSerializer.ProfileName).Key(usernameKey).SetValue(username)
credentialIni.Section(thisSerializer.ProfileName).Key(passwordKey).SetValue(password)
thisSerializer.Factory.Log.Info().Msg("Saving credential ini file.")
saveErr := credentialIni.SaveTo(thisSerializer.CredentialFile)
if saveErr != nil {
thisSerializer.Factory.Log.Error().Str("file", thisSerializer.CredentialFile).Err(saveErr).Msg("Error saving ini file.")
return saveErr
}
thisSerializer.Factory.Log.Info().Msg("Credential ini file saved successfully.")
return nil
}
func (thisSerializer *Serializer) saveProfileIni(attributes map[string]map[string]string) error {
thisSerializer.Factory.Log.Trace().Msg("Serializing profile to ini file.")
profileIni := ini.Empty()
thisSerializer.Factory.Log.Trace().Msg("Processing attributes.")
for key, value := range attributes {
thisSerializer.Factory.Log.Trace().Str("attribute", key).Msg("Adding section.")
mySection, sectionErr := profileIni.NewSection(key)
if sectionErr != nil {
return sectionErr
}
for subKey, subValue := range value {
thisSerializer.Factory.Log.Trace().Str("attribute", subKey).Msg("Adding attribute.")
_, keyErr := mySection.NewKey(subKey, subValue)
if keyErr != nil {
return keyErr
}
}
}
thisSerializer.Factory.Log.Info().Msg("Saving profile ini file.")
saveErr := profileIni.SaveTo(thisSerializer.ConfigFile)
if saveErr != nil {
return saveErr
}
thisSerializer.Factory.Log.Info().Msg("Profile ini file saved successfully.")
return nil
}
func initIni(fileName string) (*ini.File, error) {
if _, statErr := os.Stat(fileName); os.IsNotExist(statErr) {
emptyFile, emptyErr := os.Create(fileName)
if emptyErr != nil {
return nil, emptyErr
}
closeErr := emptyFile.Close()
if closeErr != nil {
return nil, closeErr
}
}
return ini.Load(fileName)
}
/*
FromIni is responsible for deserializing a Credential and Profile from an ini file. Attribute sections are directly
translatable to sections in the Profile. Alternate field labels are restored from the ini, so the same Factory
object will need to be used when deserializing.
*/
func (thisSerializer *Serializer) FromIni() (string, string, map[string]map[string]string, error) {
thisSerializer.Factory.Log.Info().Msg("Deserializing credential and profile from ini file.")
username, password, credentialErr := thisSerializer.loadCredentialIni()
if credentialErr != nil {
return "", "", make(map[string]map[string]string), credentialErr
}
attributes, attributeErr := thisSerializer.loadProfileIni()
if attributeErr != nil {
return "", "", make(map[string]map[string]string), attributeErr
}
return username, password, attributes, nil
}
func (thisSerializer *Serializer) loadCredentialIni() (string, string, error) {
credentialIni, initErr := initIni(thisSerializer.CredentialFile)
usernameKey := thisSerializer.Factory.GetAlternateUsername()
passwordKey := thisSerializer.Factory.GetAlternatePassword()
if initErr != nil {
return "", "", initErr
}
username := credentialIni.Section(thisSerializer.ProfileName).Key(usernameKey).String()
password := credentialIni.Section(thisSerializer.ProfileName).Key(passwordKey).String()
return username, password, nil
}
func (thisSerializer *Serializer) loadProfileIni() (map[string]map[string]string, error) {
profileIni, initErr := initIni(thisSerializer.ConfigFile)
myAttributes := make(map[string]map[string]string)
if initErr != nil {
return make(map[string]map[string]string), initErr
}
for _, section := range profileIni.Sections() {
keys := section.KeysHash()
myAttributes[section.Name()] = keys
}
return myAttributes, nil
}