v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-16 07:40:44 -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:
eeeXun 2024-02-08 18:05:23 +08:00
parent b6a9d79237
commit d83cbabc35
3 changed files with 23 additions and 16 deletions

View File

@ -17,13 +17,13 @@ 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:
chatgpt: CHATGPT_API_KEY # <- Replace with your API Key
deepl: DEEPL_API_KEY # <- Replace with your API Key
chatgpt: CHATGPT_API_KEY # <- Replace with your API Key
deepl: DEEPL_API_KEY # <- Replace with your API Key
```
## ScreenShot
@ -47,21 +47,21 @@ For RedHat-based Linux, you need `alsa-lib-devel`.
```sh
yay -S gtt-bin
```
### Nix ❄️ ([nixpkgs-unstable](https://search.nixos.org/packages?channel=unstable&show=gtt&from=0&size=50&sort=relevance&type=packages&query=gtt))
add to your package list or run with:
```sh
nix-shell -p '(import <nixpkgs-unstable> {}).gtt' --run gtt
```
or with flakes enabled:
or with flakes enabled:
```sh
nix run github:nixos/nixpkgs#gtt
```
### Prebuild
Binary file is available in [Release Page](https://github.com/eeeXun/gtt/releases) for Linux and macOS on x86_64.

View File

@ -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
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)))
// Import api key if file exists
if err := serverConfig.ReadInConfig(); err == nil {
for _, name := range []string{"ChatGPT", "DeepL"} {
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
View File

@ -0,0 +1,3 @@
api_key:
chatgpt: CHATGPT_API_KEY
deepl: DEEPL_API_KEY