Skip to content

Commit

Permalink
feat: read API key from file
Browse files Browse the repository at this point in the history
Close #29
  • Loading branch information
eeeXun committed Feb 10, 2024
1 parent ad0cf55 commit 3b0edef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ See the example in [server.yaml](example/server.yaml) file.

```yaml
api_key:
chatgpt: CHATGPT_API_KEY # <- Replace with your API Key
deepl: DEEPL_API_KEY # <- Replace with your API Key
chatgpt:
value: CHATGPT_API_KEY # <- Replace with your API Key
# file: $HOME/secrets/chatgpt.txt # <- You can also specify the file where to read api key
deepl:
value: DEEPL_API_KEY # <- Replace with your API Key
# file: $HOME/secrets/deepl.txt # <- You can also specify the file where to read api key
```

## DeepLX
Expand All @@ -41,7 +45,9 @@ See the example in [server.yaml](example/server.yaml) file.

```yaml
api_key:
deeplx: DEEPLX_API_KEY # <- Replace with your API Key
deeplx:
value: DEEPLX_API_KEY # <- Replace with your API Key
# file: $HOME/secrets/deeplx.txt # <- You can also specify the file where to read api key
host:
deeplx: 127.0.0.1:1188 # <- Replace with your server IP address and port
```
Expand Down
11 changes: 9 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"

"github.com/eeeXun/gtt/internal/style"
"github.com/eeeXun/gtt/internal/translate"
Expand Down Expand Up @@ -160,8 +161,14 @@ func configInit() {
if err := serverConfig.ReadInConfig(); err == nil {
// api key
for _, name := range []string{"ChatGPT", "DeepL", "DeepLX"} {
if serverConfig.Get(fmt.Sprintf("api_key.%s", name)) != nil {
translators[name].SetAPIKey(serverConfig.GetString(fmt.Sprintf("api_key.%s", name)))
// Read from value first, then read from file
if serverConfig.Get(fmt.Sprintf("api_key.%s.value", name)) != nil {
translators[name].SetAPIKey(serverConfig.GetString(fmt.Sprintf("api_key.%s.value", name)))
} else if serverConfig.Get(fmt.Sprintf("api_key.%s.file", name)) != nil {
buff, err := os.ReadFile(os.ExpandEnv(serverConfig.GetString(fmt.Sprintf("api_key.%s.file", name))))
if err == nil {
translators[name].SetAPIKey(strings.TrimSpace(string(buff)))
}
}
}
// host
Expand Down
12 changes: 9 additions & 3 deletions example/server.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# This file should be located at $XDG_CONFIG_HOME/gtt/server.yaml or $HOME/.config/gtt/server.yaml.
api_key:
chatgpt: CHATGPT_API_KEY
deepl: DEEPL_API_KEY
deeplx: DEEPLX_API_KEY
chatgpt:
value: CHATGPT_API_KEY
# file: $HOME/secrets/chatgpt.txt
deepl:
value: DEEPL_API_KEY
# file: $HOME/secrets/deepl.txt
deeplx:
value: DEEPLX_API_KEY
# file: $HOME/secrets/deeplx.txt
host:
deeplx: 127.0.0.1:1188

0 comments on commit 3b0edef

Please sign in to comment.