|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// FileConfig represents the user configuration stored in TOML. |
| 13 | +type FileConfig struct { |
| 14 | + APIKey string |
| 15 | + RcloneRemote string |
| 16 | + TemplateHubID string |
| 17 | + TemplateCoverID string |
| 18 | + TemplateReviewID string |
| 19 | +} |
| 20 | + |
| 21 | +// DefaultConfigPath returns ~/.tess/config.toml. |
| 22 | +func DefaultConfigPath() (string, error) { |
| 23 | + home, err := os.UserHomeDir() |
| 24 | + if err != nil { |
| 25 | + return "", err |
| 26 | + } |
| 27 | + return filepath.Join(home, ".tess", "config.toml"), nil |
| 28 | +} |
| 29 | + |
| 30 | +// LoadConfig reads a minimal TOML and returns the FileConfig. |
| 31 | +func LoadConfig(path string) (FileConfig, error) { |
| 32 | + f, err := os.Open(path) |
| 33 | + if err != nil { |
| 34 | + if errors.Is(err, os.ErrNotExist) { |
| 35 | + return FileConfig{}, fmt.Errorf("config file not found: %s", path) |
| 36 | + } |
| 37 | + return FileConfig{}, err |
| 38 | + } |
| 39 | + defer f.Close() |
| 40 | + var cfg FileConfig |
| 41 | + scanner := bufio.NewScanner(f) |
| 42 | + for scanner.Scan() { |
| 43 | + line := scanner.Text() |
| 44 | + if i := strings.Index(line, "#"); i >= 0 { |
| 45 | + line = line[:i] |
| 46 | + } |
| 47 | + line = strings.TrimSpace(line) |
| 48 | + if line == "" || strings.HasPrefix(line, "[") { |
| 49 | + continue |
| 50 | + } |
| 51 | + parts := strings.SplitN(line, "=", 2) |
| 52 | + if len(parts) != 2 { |
| 53 | + continue |
| 54 | + } |
| 55 | + key := strings.TrimSpace(parts[0]) |
| 56 | + val := strings.TrimSpace(parts[1]) |
| 57 | + val = strings.Trim(val, " \t") |
| 58 | + if len(val) >= 2 { |
| 59 | + if (val[0] == '"' && val[len(val)-1] == '"') || (val[0] == '\'' && val[len(val)-1] == '\'') { |
| 60 | + val = val[1 : len(val)-1] |
| 61 | + } |
| 62 | + } |
| 63 | + switch key { |
| 64 | + case "api_key": |
| 65 | + cfg.APIKey = val |
| 66 | + case "rclone_remote": |
| 67 | + cfg.RcloneRemote = strings.TrimSpace(val) |
| 68 | + case "template_hub_id": |
| 69 | + cfg.TemplateHubID = strings.TrimSpace(val) |
| 70 | + case "template_cover_id": |
| 71 | + cfg.TemplateCoverID = strings.TrimSpace(val) |
| 72 | + case "template_review_id": |
| 73 | + cfg.TemplateReviewID = strings.TrimSpace(val) |
| 74 | + } |
| 75 | + } |
| 76 | + if err := scanner.Err(); err != nil { |
| 77 | + return FileConfig{}, err |
| 78 | + } |
| 79 | + if strings.TrimSpace(cfg.APIKey) == "" { |
| 80 | + return FileConfig{}, fmt.Errorf("missing 'api_key' in config: %s", path) |
| 81 | + } |
| 82 | + return cfg, nil |
| 83 | +} |
| 84 | + |
| 85 | +// EnsureConfigDir ensures the parent directory for path exists. |
| 86 | +func EnsureConfigDir(path string) error { |
| 87 | + dir := filepath.Dir(path) |
| 88 | + return os.MkdirAll(dir, 0o755) |
| 89 | +} |
| 90 | + |
| 91 | +// SaveConfig writes a minimal TOML to path. |
| 92 | +func SaveConfig(path string, cfg FileConfig) error { |
| 93 | + if err := EnsureConfigDir(path); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + var b strings.Builder |
| 97 | + if strings.TrimSpace(cfg.APIKey) != "" { |
| 98 | + fmt.Fprintf(&b, "api_key = \"%s\"\n", escape(cfg.APIKey)) |
| 99 | + } |
| 100 | + if strings.TrimSpace(cfg.RcloneRemote) != "" { |
| 101 | + fmt.Fprintf(&b, "rclone_remote = \"%s\"\n", escape(cfg.RcloneRemote)) |
| 102 | + } |
| 103 | + if strings.TrimSpace(cfg.TemplateHubID) != "" { |
| 104 | + fmt.Fprintf(&b, "template_hub_id = \"%s\"\n", escape(cfg.TemplateHubID)) |
| 105 | + } |
| 106 | + if strings.TrimSpace(cfg.TemplateCoverID) != "" { |
| 107 | + fmt.Fprintf(&b, "template_cover_id = \"%s\"\n", escape(cfg.TemplateCoverID)) |
| 108 | + } |
| 109 | + if strings.TrimSpace(cfg.TemplateReviewID) != "" { |
| 110 | + fmt.Fprintf(&b, "template_review_id = \"%s\"\n", escape(cfg.TemplateReviewID)) |
| 111 | + } |
| 112 | + return os.WriteFile(path, []byte(b.String()), 0o600) |
| 113 | +} |
| 114 | + |
| 115 | +func escape(s string) string { |
| 116 | + // Very small escape to avoid stray quotes in TOML values we write. |
| 117 | + s = strings.ReplaceAll(s, "\\", "\\\\") |
| 118 | + s = strings.ReplaceAll(s, "\"", "\\\"") |
| 119 | + return s |
| 120 | +} |
0 commit comments