mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-16 15:50:52 -07:00
feat: move api_key from gtt.yaml to server.yaml
When you exist gtt, if you change something from the menu, gtt.yaml will be overwritten. Therefore, when you write your api key after opening gtt, you might lose what you wrote in gtt.yaml. So I change it from gtt.yaml to server.yaml
This commit is contained in:
parent
b6a9d79237
commit
d83cbabc35
@ -17,8 +17,8 @@ ChatGPT and DeepL translations require API keys, which can be obtained from
|
||||
[OpenAI API keys](https://platform.openai.com/account/api-keys) and
|
||||
[DeepL API signup](https://www.deepl.com/pro-api) pages, respectively. Note
|
||||
that only the free API is supported for DeepL currently. Once you have your
|
||||
API key add it to `$XDG_CONFIG_HOME/gtt/gtt.yaml` or
|
||||
`$HOME/.config/gtt/gtt.yaml`
|
||||
API key add it to `$XDG_CONFIG_HOME/gtt/server.yaml` or
|
||||
`$HOME/.config/gtt/server.yaml`
|
||||
|
||||
```yaml
|
||||
api_key:
|
||||
|
16
config.go
16
config.go
@ -20,6 +20,7 @@ func configInit() {
|
||||
defaultConfigPath string
|
||||
themeConfig = viper.New()
|
||||
keyMapConfig = viper.New()
|
||||
serverConfig = viper.New()
|
||||
defaultKeyMaps = map[string]string{
|
||||
"exit": "C-c",
|
||||
"translate": "C-j",
|
||||
@ -61,18 +62,19 @@ func configInit() {
|
||||
config.SetConfigName("gtt")
|
||||
themeConfig.SetConfigName("theme")
|
||||
keyMapConfig.SetConfigName("keymap")
|
||||
for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} {
|
||||
serverConfig.SetConfigName("server")
|
||||
for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig, serverConfig} {
|
||||
c.SetConfigType("yaml")
|
||||
}
|
||||
if len(os.Getenv("XDG_CONFIG_HOME")) > 0 {
|
||||
defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt"
|
||||
for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} {
|
||||
for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig, serverConfig} {
|
||||
c.AddConfigPath(defaultConfigPath)
|
||||
}
|
||||
} else {
|
||||
defaultConfigPath = os.Getenv("HOME") + "/.config/gtt"
|
||||
}
|
||||
for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig} {
|
||||
for _, c := range []*viper.Viper{config, themeConfig, keyMapConfig, serverConfig} {
|
||||
c.AddConfigPath("$HOME/.config/gtt")
|
||||
}
|
||||
|
||||
@ -152,10 +154,12 @@ func configInit() {
|
||||
uiStyle.Transparent = config.GetBool("transparent")
|
||||
uiStyle.SetSrcBorderColor(config.GetString("source.border_color")).
|
||||
SetDstBorderColor(config.GetString("destination.border_color"))
|
||||
// Set API Keys
|
||||
// Import api key if file exists
|
||||
if err := serverConfig.ReadInConfig(); err == nil {
|
||||
for _, name := range []string{"ChatGPT", "DeepL"} {
|
||||
if config.Get(fmt.Sprintf("api_key.%s", name)) != nil {
|
||||
translators[name].SetAPIKey(config.GetString(fmt.Sprintf("api_key.%s", name)))
|
||||
if serverConfig.Get(fmt.Sprintf("api_key.%s", name)) != nil {
|
||||
translators[name].SetAPIKey(serverConfig.GetString(fmt.Sprintf("api_key.%s", name)))
|
||||
}
|
||||
}
|
||||
}
|
||||
// Set argument language
|
||||
|
3
example/server.yaml
Normal file
3
example/server.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
api_key:
|
||||
chatgpt: CHATGPT_API_KEY
|
||||
deepl: DEEPL_API_KEY
|
Loading…
x
Reference in New Issue
Block a user