forked from neosapience/cast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
190 lines (162 loc) · 3.85 KB
/
config.go
File metadata and controls
190 lines (162 loc) · 3.85 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package cmd
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func configPath() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(home, ".typecast", "config.yaml"), nil
}
func readConfig() (map[string]any, error) {
path, err := configPath()
if err != nil {
return nil, err
}
config := map[string]any{}
data, err := os.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("failed to parse config file: %w", err)
}
return config, nil
}
func writeConfig(config map[string]any) error {
path, err := configPath()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return err
}
data, err := yaml.Marshal(config)
if err != nil {
return err
}
return os.WriteFile(path, data, 0600)
}
func saveConfig(apiKey string) error {
config, err := readConfig()
if err != nil {
return err
}
config["api_key"] = apiKey
return writeConfig(config)
}
// configKeys maps CLI flag names to config file keys
var configKeys = map[string]string{
"voice-id": "voice_id",
"model": "model",
"language": "language",
"emotion": "emotion",
"emotion-preset": "emotion_preset",
"emotion-intensity": "emotion_intensity",
"volume": "volume",
"pitch": "pitch",
"tempo": "tempo",
"format": "format",
"base-url": "base_url",
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage default settings",
}
var configSetCmd = &cobra.Command{
Use: "set <key> <value>",
Short: "Set a default value",
Long: "Set a default value in ~/.typecast/config.yaml\n\nAvailable keys: " +
"voice-id, model, language, emotion, emotion-preset, emotion-intensity, volume, pitch, tempo, format, base-url",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
key, value := args[0], args[1]
configKey, ok := configKeys[key]
if !ok {
return fmt.Errorf("unknown key %q, available: %s", key, strings.Join(availableKeys(), ", "))
}
config, err := readConfig()
if err != nil {
return err
}
config[configKey] = value
if err := writeConfig(config); err != nil {
return err
}
fmt.Printf("%s = %s\n", configKey, value)
return nil
},
}
var configUnsetCmd = &cobra.Command{
Use: "unset <key>",
Short: "Remove a default value",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
key := args[0]
configKey, ok := configKeys[key]
if !ok {
return fmt.Errorf("unknown key %q, available: %s", key, strings.Join(availableKeys(), ", "))
}
config, err := readConfig()
if err != nil {
return err
}
delete(config, configKey)
if err := writeConfig(config); err != nil {
return err
}
fmt.Printf("unset %s\n", configKey)
return nil
},
}
var configListCmd = &cobra.Command{
Use: "list",
Short: "Show current config",
RunE: func(cmd *cobra.Command, args []string) error {
config, err := readConfig()
if err != nil {
return err
}
if len(config) == 0 {
fmt.Println("(empty)")
return nil
}
keys := make([]string, 0, len(config))
for k := range config {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := config[k]
if k == "api_key" {
s := fmt.Sprintf("%v", v)
if len(s) > 8 {
v = s[:8] + "..."
}
}
fmt.Printf("%s = %v\n", k, v)
}
return nil
},
}
func availableKeys() []string {
keys := make([]string, 0, len(configKeys))
for k := range configKeys {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func init() {
configCmd.AddCommand(configSetCmd)
configCmd.AddCommand(configUnsetCmd)
configCmd.AddCommand(configListCmd)
rootCmd.AddCommand(configCmd)
}