-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfigure.go
124 lines (104 loc) · 3.24 KB
/
configure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"os"
"path"
"strings"
"github.com/charmbracelet/huh"
"github.com/pelletier/go-toml"
)
type Config struct {
AssemblyAIAPIKey string `toml:"assembly-ai-api-key" json:"assembly_ai_api_key"`
DeepgramAPIKey string `toml:"deepgram-api-key" json:"deepgram_api_key"`
GroqAPIKey string `toml:"groq-api-key" json:"groq_api_key"`
AnthropicAPIKey string `toml:"anthropic-api-key" json:"anthropic_api_key"`
OpenAIAPIKey string `toml:"openai-api-key" json:"openai_api_key"`
GeminiAPIKey string `toml:"gemini-api-key" json:"gemini_api_key"`
AWSRegion string `toml:"aws-region" json:"aws_region"`
AWSAccessKeyID string `toml:"aws-access-key-id" json:"aws_access_key_id"`
AWSSecretAccessKey string `toml:"aws-secret-access-key" json:"aws_secret_access_key"`
AWSSessionToken string `toml:"aws-session-token" json:"aws_session_token"`
}
type ConfigureCmd struct{}
const configFileName = ".podscript.toml"
func (c *ConfigureCmd) Run() error {
config := &Config{}
// Try to read existing config
existingConfig, err := ReadConfig()
if err == nil {
config = existingConfig
}
// Prompt for each API key
prompts := []struct {
title string
value *string
}{
{"OpenAI API key", &config.OpenAIAPIKey},
{"Anthropic API key", &config.AnthropicAPIKey},
{"Deepgram API key", &config.DeepgramAPIKey},
{"Groq API key", &config.GroqAPIKey},
{"AssemblyAI API key", &config.AssemblyAIAPIKey},
{"Gemini API key", &config.GeminiAPIKey},
{"AWS Region", &config.AWSRegion},
{"AWS Access Key ID", &config.AWSAccessKeyID},
{"AWS Secret Access Key", &config.AWSSecretAccessKey},
{"AWS Session Token", &config.AWSSessionToken},
}
for _, p := range prompts {
if err := promptAndSet(p.title, p.value); err != nil {
return err
}
}
return WriteConfig(config)
}
func promptAndSet(promptTitle string, currentValue *string) error {
var value string
textInput := huh.NewInput().
Title(promptTitle).
Prompt("> ").
Placeholder("press Enter to skip or leave unchanged").
EchoMode(huh.EchoModePassword).
Value(&value)
err := textInput.Run()
if err != nil && err != huh.ErrUserAborted {
return err
}
value = strings.TrimSpace(value)
if value != "" {
*currentValue = value
fmt.Printf("%s set\n", promptTitle)
} else {
fmt.Printf("skipping %s\n", promptTitle)
}
return nil
}
func ReadConfig() (*Config, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("error getting home directory: %w", err)
}
configPath := path.Join(homeDir, configFileName)
config := &Config{}
data, err := os.ReadFile(configPath)
if !os.IsNotExist(err) { // ignore if file doesn't exist
if err != nil {
return nil, fmt.Errorf("error reading config file: %w", err)
}
if err := toml.Unmarshal(data, config); err != nil {
return nil, fmt.Errorf("error parsing config file: %w", err)
}
}
return config, nil
}
func WriteConfig(config *Config) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("error getting home directory: %w", err)
}
configPath := path.Join(homeDir, configFileName)
data, err := toml.Marshal(config)
if err != nil {
return fmt.Errorf("error marshaling config: %w", err)
}
return os.WriteFile(configPath, data, 0600)
}