diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-01 13:00:40 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-01 13:00:40 +0300 |
| commit | b006582e4c7323821eb836080a9bc9ec24243a47 (patch) | |
| tree | a8e96ecdc74cd2a5bb19148d8a6a7cf90f6cbbb4 /internal/cli | |
| parent | a574e5290261f9ea2cc39fd0a9a2ba756fc1f67a (diff) | |
zr: add Gemini-backed translation provider
Diffstat (limited to 'internal/cli')
| -rw-r--r-- | internal/cli/command.go | 11 | ||||
| -rw-r--r-- | internal/cli/command_test.go | 65 |
2 files changed, 76 insertions, 0 deletions
diff --git a/internal/cli/command.go b/internal/cli/command.go index b07bd26..a5a8c13 100644 --- a/internal/cli/command.go +++ b/internal/cli/command.go @@ -155,3 +155,14 @@ func GetOpenAIKey() string { // Then check config file return viper.GetString("audio.openai_key") } + +// GetGoogleAPIKey retrieves the Google API key from environment or config. +func GetGoogleAPIKey() string { + // First check environment variable + if key := os.Getenv("GOOGLE_API_KEY"); key != "" { + return key + } + + // Then check config file + return viper.GetString("google.api_key") +} diff --git a/internal/cli/command_test.go b/internal/cli/command_test.go index c6fa3d8..2361cd2 100644 --- a/internal/cli/command_test.go +++ b/internal/cli/command_test.go @@ -237,6 +237,71 @@ func TestGetOpenAIKey(t *testing.T) { } } +func TestGetGoogleAPIKey(t *testing.T) { + // Save original viper state + originalConfig := viper.New() + *originalConfig = *viper.GetViper() + defer func() { + *viper.GetViper() = *originalConfig + }() + + tests := []struct { + name string + envKey string + configKey string + expected string + }{ + { + name: "from environment", + envKey: "env-google-key", + configKey: "config-google-key", + expected: "env-google-key", + }, + { + name: "from config when no env", + envKey: "", + configKey: "config-google-key", + expected: "config-google-key", + }, + { + name: "empty when neither set", + envKey: "", + configKey: "", + expected: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + viper.Reset() + + if tt.envKey != "" { + if err := os.Setenv("GOOGLE_API_KEY", tt.envKey); err != nil { + t.Fatalf("Failed to set GOOGLE_API_KEY: %v", err) + } + defer func() { + if err := os.Unsetenv("GOOGLE_API_KEY"); err != nil { + t.Errorf("Failed to unset GOOGLE_API_KEY: %v", err) + } + }() + } else { + if err := os.Unsetenv("GOOGLE_API_KEY"); err != nil { + t.Fatalf("Failed to unset GOOGLE_API_KEY: %v", err) + } + } + + if tt.configKey != "" { + viper.Set("google.api_key", tt.configKey) + } + + got := GetGoogleAPIKey() + if got != tt.expected { + t.Errorf("GetGoogleAPIKey() = %v, want %v", got, tt.expected) + } + }) + } +} + func TestBindFlagsToViper(t *testing.T) { // Save original viper state originalConfig := viper.New() |
