|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "context" |
5 | 6 | "crypto/tls" |
6 | 7 | "fmt" |
7 | 8 | "io" |
8 | 9 | "net" |
9 | 10 | "net/url" |
10 | 11 | "os" |
| 12 | + "path/filepath" |
11 | 13 | "regexp" |
12 | 14 | "slices" |
13 | 15 | "strings" |
@@ -206,8 +208,50 @@ func parseServer(s string) (string, transport.Type, error) { |
206 | 208 | return server, ts, nil |
207 | 209 | } |
208 | 210 |
|
| 211 | +func loadConfig() []string { |
| 212 | + home, err := os.UserHomeDir() |
| 213 | + if err != nil { |
| 214 | + log.Debugf("Could not find user home directory: %s", err) |
| 215 | + return nil |
| 216 | + } |
| 217 | + |
| 218 | + configPath := filepath.Join(home, ".qrc") |
| 219 | + file, err := os.Open(configPath) |
| 220 | + if os.IsNotExist(err) { |
| 221 | + return nil |
| 222 | + } |
| 223 | + if err != nil { |
| 224 | + log.Warnf("Could not open config file %s: %s", configPath, err) |
| 225 | + return nil |
| 226 | + } |
| 227 | + defer file.Close() |
| 228 | + |
| 229 | + var configArgs []string |
| 230 | + scanner := bufio.NewScanner(file) |
| 231 | + for scanner.Scan() { |
| 232 | + line := strings.TrimSpace(scanner.Text()) |
| 233 | + if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") { |
| 234 | + continue |
| 235 | + } |
| 236 | + parts := strings.Fields(line) |
| 237 | + configArgs = append(configArgs, parts...) |
| 238 | + } |
| 239 | + |
| 240 | + if err := scanner.Err(); err != nil { |
| 241 | + log.Warnf("Error reading config file: %s", err) |
| 242 | + } |
| 243 | + |
| 244 | + if len(configArgs) > 0 { |
| 245 | + log.Debugf("Loaded default options from %s: %v", configPath, configArgs) |
| 246 | + } |
| 247 | + |
| 248 | + return configArgs |
| 249 | +} |
| 250 | + |
209 | 251 | // driver is the "main" function for this program that accepts a flag slice for testing |
210 | 252 | func driver(args []string, out io.Writer) error { |
| 253 | + configArgs := loadConfig() |
| 254 | + args = append(configArgs, args...) |
211 | 255 | args = cli.SetFalseBooleans(&opts, args) |
212 | 256 | args = cli.AddEqualSigns(args) |
213 | 257 | parser := flags.NewParser(&opts, flags.Default) |
|
0 commit comments