Skip to content

Commit

Permalink
feat: move api_key from gtt.yaml to server.yaml
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eeeXun committed Feb 8, 2024
1 parent b6a9d79 commit d83cbab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions example/server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
api_key:
chatgpt: CHATGPT_API_KEY
deepl: DEEPL_API_KEY

0 comments on commit d83cbab

Please sign in to comment.