-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal.go
79 lines (64 loc) · 1.47 KB
/
internal.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
package main
import (
"encoding/json"
"fmt"
"os"
)
type Internal struct{}
type Settings struct {
ApiKey string `json:"apiKey"`
InputDelay string `json:"inputDelay"`
InputLanguage string `json:"inputLanguage"`
OutputLanguage string `json:"outputLanguage"`
}
func (i *Internal) GetDataPath() string {
p, _ := os.UserConfigDir()
return p + "/franslate"
}
func (i *Internal) GetSettingsFile() *Settings {
p := i.GetDataPath()
f := "settings.json"
fullPath := fmt.Sprintf("%s/%s", p, f)
_, err := os.Stat(fullPath)
if os.IsNotExist(err) {
return &Settings{}
}
file, _ := os.ReadFile(fullPath)
var s Settings
_ = json.Unmarshal(file, &s)
return &s
}
func (i *Internal) SyncSettingsFile(t *Settings) {
p := i.GetDataPath()
f := "settings.json"
fullPath := fmt.Sprintf("%s/%s", p, f)
if _, err := os.Stat(p); os.IsNotExist(err) {
err := os.MkdirAll(p, os.ModePerm)
if err != nil {
return
}
}
_, err := os.Stat(fullPath)
if os.IsNotExist(err) {
file, _ := json.Marshal(t)
_ = os.WriteFile(fullPath, file, 0644)
} else {
file, _ := os.ReadFile(fullPath)
var s Settings
_ = json.Unmarshal(file, &s)
if t.ApiKey != "" {
s.ApiKey = t.ApiKey
}
if t.InputDelay != "" {
s.InputDelay = t.InputDelay
}
if t.InputLanguage != "" {
s.InputLanguage = t.InputLanguage
}
if t.OutputLanguage != "" {
s.OutputLanguage = t.OutputLanguage
}
updatedFile, _ := json.Marshal(s)
_ = os.WriteFile(fullPath, updatedFile, 0644)
}
}