-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
141 lines (116 loc) · 3.77 KB
/
config.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
package conf
import (
"github.com/Ericwyn/EzeTranslate/log"
"github.com/spf13/viper"
"os"
"path"
)
const Version = "V1.6-Release"
const ReleaseDate = "2024.03.01"
const ConfigKeyMiniMode = "miniMode"
const ConfigKeyBaiduTransAppId = "baiduTransAppId"
const ConfigKeyBaiduTransAppSecret = "baiduTransAppSecret"
const ConfigKeyYouDaoTransAppId = "youdaoTransAppId"
const ConfigKeyYouDaoTransAppSecret = "youdaoTransAppSecret"
const ConfigKeyGoogleTranslateProxy = "googleTranslateProxy"
const ConfigKeyGoogleTranslateUrl = "googleTranslateUrl"
const ConfigKeyFormatSpace = "formatSpace"
const ConfigKeyFormatCarriageReturn = "formatCarriageReturn"
const ConfigKeyFormatAnnotation = "formatAnnotation"
const ConfigKeyFormatCamelCase = "formatCamelCase"
// ConfigKeyTranslateSelect 选择哪个翻译
const ConfigKeyTranslateSelect = "translateSelect"
const configFileDirName = "EzeTranslate"
const configFileName = "config"
const configFileType = "yaml"
func InitConfig() {
viper.SetDefault(ConfigKeyMiniMode, false)
viper.SetDefault(ConfigKeyBaiduTransAppId, "baiduTransAppId-xxxxxxxxxxxxxxx")
viper.SetDefault(ConfigKeyBaiduTransAppSecret, "baiduTransAppSecret-xxxxxxxxxxxxxxx")
viper.SetDefault(ConfigKeyYouDaoTransAppId, "youdaoTransAppId-xxxxxxxxxxxxxxx")
viper.SetDefault(ConfigKeyYouDaoTransAppSecret, "youdaoTransAppSecret-xxxxxxxxxxxxxxx")
viper.SetDefault(ConfigKeyGoogleTranslateProxy, "")
viper.SetDefault(ConfigKeyGoogleTranslateUrl, "translate.google.com")
viper.SetDefault(ConfigKeyTranslateSelect, "google")
viper.SetDefault(ConfigKeyFormatSpace, false)
viper.SetDefault(ConfigKeyFormatCarriageReturn, false)
viper.SetDefault(ConfigKeyFormatAnnotation, false)
viper.SetDefault(ConfigKeyFormatCamelCase, false)
viper.SetConfigName(configFileName)
viper.SetConfigType(configFileType)
//viper.AddConfigPath(resource.GetRunnerPath() + "/.conf")
//viper.AddConfigPath(resource.GetRunnerPath())
// 初始化配置文件
GetConfigFilePath()
viper.AddConfigPath(GetConfigFileDirPath())
err := viper.ReadInConfig()
if err != nil {
log.E("载入配置时候出错")
panic(err)
}
//printConfigs()
}
// GetConfigFilePath
// 获取配置文件路径
// linux 下在 ~/.config/EzeTranslate/config.yaml
func GetConfigFilePath() string {
dir := GetConfigFileDirPath()
// 如果 dir 不存在的话, 我们就创建
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, os.ModePerm)
}
configFilePath := path.Join(dir, configFileName+"."+configFileType)
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
log.I("创建配置文件:" + configFilePath)
// 如果文件不存在的话, 我们就创建
f, err := os.Create(configFilePath)
if err != nil {
log.E("无法创建配置文件")
log.E(err)
os.Exit(-1)
}
f.Close()
return configFilePath
} else {
log.I("找到配置文件:" + configFilePath)
return configFilePath
}
}
// GetConfigFileDirPath
// 获取配置文件目录
func GetConfigFileDirPath() string {
configDir, err := os.UserConfigDir()
if err != nil {
log.E("无法获取配置目录", err)
os.Exit(-1)
}
return path.Join(configDir, configFileDirName)
}
func printConfigs() {
configList := []string{
ConfigKeyBaiduTransAppId,
ConfigKeyBaiduTransAppSecret,
ConfigKeyYouDaoTransAppId,
ConfigKeyYouDaoTransAppSecret,
ConfigKeyGoogleTranslateUrl,
ConfigKeyGoogleTranslateProxy,
ConfigKeyFormatSpace,
ConfigKeyFormatCarriageReturn,
ConfigKeyFormatAnnotation,
ConfigKeyTranslateSelect,
}
for _, key := range configList {
log.D("config " + key + " : " + viper.GetString(key))
}
}
// 返回百度翻译 api 的 appId 和 appSecret
func GetBaiduTransApiMsg() (string, string) {
return "", ""
}
func SaveConfig() {
e := viper.WriteConfig()
if e != nil {
log.E("配置文件保存失败")
log.E(e)
}
}