-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings.go
More file actions
34 lines (31 loc) · 803 Bytes
/
settings.go
File metadata and controls
34 lines (31 loc) · 803 Bytes
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
package ig
import (
"encoding/json"
"os"
)
// LoadSettingsFromFile reads and unmarshals a JSON session file produced by DumpSettingsToFile or DumpSettings.
func LoadSettingsFromFile(path string) (*Settings, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var s Settings
if err := json.Unmarshal(b, &s); err != nil {
return nil, err
}
if s.Cookies == nil {
s.Cookies = map[string]string{}
}
if s.AuthorizationData == nil {
s.AuthorizationData = map[string]string{}
}
return &s, nil
}
// DumpSettingsToFile writes settings to path as indented JSON with restrictive file permissions.
func DumpSettingsToFile(path string, s *Settings) error {
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, b, 0o600)
}