-
Notifications
You must be signed in to change notification settings - Fork 12
/
update.go
47 lines (42 loc) · 1.23 KB
/
update.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
package utils
import (
"encoding/json"
"os"
)
func UpdateConfigFile(filePath string) error {
var ReadedFile, response Response
var file *os.File
var err error
content := ReadConfig(filePath)
var body []byte
var updatedContent []byte
if err = json.Unmarshal(content, &ReadedFile); err != nil {
panic(err)
}
defer file.Close()
if ReadedFile.Config.ReservedDec == nil || ReadedFile.Config.ReservedHex == "" {
response.Config.ReservedDec, response.Config.ReservedHex = clientIDtoReserved(ReadedFile.Config.ClientID)
} else {
response.Config.ReservedDec = ReadedFile.Config.ReservedDec
response.Config.ReservedHex = ReadedFile.Config.ReservedHex
}
if body, err = request([]byte(``), ReadedFile.Token, ReadedFile.ID, "update"); err != nil {
panic(err)
}
if err = json.Unmarshal(body, &response); err != nil {
panic(err)
}
if ReadedFile.Account.PrivateKey != "" {
response.Config.PrivateKey = ReadedFile.Account.PrivateKey
} else {
response.Config.PrivateKey = ReadedFile.Config.PrivateKey
}
response.Token = ReadedFile.Token
if updatedContent, err = json.MarshalIndent(response, "", " "); err != nil {
panic(err)
}
if err = os.WriteFile(filePath, updatedContent, 0600); err != nil {
panic(err)
}
return nil
}