From d83cbabc3579878d9f803ea274ca227acace7d34 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Thu, 8 Feb 2024 18:05:23 +0800 Subject: [PATCH] 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 --- README.md | 18 +++++++++--------- config.go | 18 +++++++++++------- example/server.yaml | 3 +++ 3 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 example/server.yaml diff --git a/README.md b/README.md index 40c4575..cd30c59 100644 --- a/README.md +++ b/README.md @@ -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 {}).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. diff --git a/config.go b/config.go index 5d3ec61..b4af331 100644 --- a/config.go +++ b/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 - 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 diff --git a/example/server.yaml b/example/server.yaml new file mode 100644 index 0000000..cee7957 --- /dev/null +++ b/example/server.yaml @@ -0,0 +1,3 @@ +api_key: + chatgpt: CHATGPT_API_KEY + deepl: DEEPL_API_KEY