Skip to content

Commit

Permalink
feat: add api-key-cmd to run external command to get API_KEY (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
sedlund authored Dec 4, 2024
1 parent 21af83d commit 9b63a4a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type API struct {
Name string
APIKey string `yaml:"api-key"`
APIKeyEnv string `yaml:"api-key-env"`
APIKeyCmd string `yaml:"api-key-cmd"`
Version string `yaml:"version"`
BaseURL string `yaml:"base-url"`
Models map[string]Model `yaml:"models"`
Expand Down
1 change: 1 addition & 0 deletions config_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ apis:
base-url: https://api.openai.com/v1
api-key:
api-key-env: OPENAI_API_KEY
# api-key-cmd: rbw get -f OPENAI_API_KEY chat.openai.com
models: # https://platform.openai.com/docs/models
gpt-4o-mini:
aliases: ["4o-mini"]
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ require (
github.com/aws/smithy-go v1.20.3 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/caarlos0/go-shellwords v1.0.12 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7 h1:kJP/C2eL9DCKr
github.com/caarlos0/duration v0.0.0-20240108180406-5d492514f3c7/go.mod h1:mSkwb/eZEwOJJJ4tqAKiuhLIPe0e9+FKhlU0oMCpbf8=
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
github.com/caarlos0/go-shellwords v1.0.12 h1:HWrUnu6lGbWfrDcFiHcZiwOLzHWjjrPVehULaTFgPp8=
github.com/caarlos0/go-shellwords v1.0.12/go.mod h1:bYeeX1GrTLPl5cAMYEzdm272qdsQAZiaHgeF0KTk1Gw=
github.com/caarlos0/timea.go v1.2.0 h1:JkjyWSUheN4nGO/OmYVGKbEv4ozHP/zuTZWD5Ih3Gog=
github.com/caarlos0/timea.go v1.2.0/go.mod h1:p4uopjR7K+y0Oxh7j0vLh3vSo58jjzOgXHKcyKwQjuY=
github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA=
Expand Down
15 changes: 14 additions & 1 deletion mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"time"
"unicode"

"github.com/caarlos0/go-shellwords"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
Expand Down Expand Up @@ -405,9 +407,20 @@ func (m *Mods) startCompletionCmd(content string) tea.Cmd {

func (m Mods) ensureKey(api API, defaultEnv, docsURL string) (string, error) {
key := api.APIKey
if key == "" && api.APIKeyEnv != "" {
if key == "" && api.APIKeyEnv != "" && api.APIKeyCmd == "" {
key = os.Getenv(api.APIKeyEnv)
}
if key == "" && api.APIKeyCmd != "" {
args, err := shellwords.Parse(api.APIKeyCmd)
if err != nil {
return "", modsError{err, "Failed to parse api-key-cmd"}
}
out, err := exec.Command(args[0], args[1:]...).CombinedOutput() //nolint:gosec
if err != nil {
return "", modsError{err, "Cannot exec api-key-cmd"}
}
key = strings.TrimSpace(string(out))
}
if key == "" {
key = os.Getenv(defaultEnv)
}
Expand Down

0 comments on commit 9b63a4a

Please sign in to comment.