|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// IncludeDirective defines how a config file includes its shelltime-managed version. |
| 12 | +// When pushing/pulling dotfiles, the original config file gets an include line added |
| 13 | +// at the top that sources the .shelltime version. The actual synced content lives |
| 14 | +// in the .shelltime file. |
| 15 | +type IncludeDirective struct { |
| 16 | + OriginalPath string // tilde path to original config, e.g., "~/.gitconfig" |
| 17 | + ShelltimePath string // tilde path to shelltime file, e.g., "~/.gitconfig.shelltime" |
| 18 | + IncludeLine string // The include line(s) to add at the top of the original file |
| 19 | + CheckString string // Substring to check if include already exists in file content |
| 20 | +} |
| 21 | + |
| 22 | +// ensureIncludeSetup ensures the include directive is properly set up for push. |
| 23 | +// - If the original file has no include line and no .shelltime file exists: |
| 24 | +// |
| 25 | +// copies content to .shelltime and adds include line to original. |
| 26 | +// |
| 27 | +// - If the include line is missing: adds it to the original. |
| 28 | +// - If .shelltime file is missing but include line exists: extracts content from original. |
| 29 | +func (b *BaseApp) ensureIncludeSetup(directive *IncludeDirective) error { |
| 30 | + expandedOriginal, err := b.expandPath(directive.OriginalPath) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + expandedShelltime, err := b.expandPath(directive.ShelltimePath) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + // Check if original file exists |
| 40 | + originalBytes, err := os.ReadFile(expandedOriginal) |
| 41 | + if err != nil { |
| 42 | + if os.IsNotExist(err) { |
| 43 | + return nil // Original doesn't exist, nothing to do |
| 44 | + } |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + content := string(originalBytes) |
| 49 | + hasInclude := strings.Contains(content, directive.CheckString) |
| 50 | + _, shelltimeErr := os.Stat(expandedShelltime) |
| 51 | + shelltimeExists := shelltimeErr == nil |
| 52 | + |
| 53 | + if hasInclude && shelltimeExists { |
| 54 | + return nil // Already set up |
| 55 | + } |
| 56 | + |
| 57 | + if !hasInclude && !shelltimeExists { |
| 58 | + // First time setup: copy content to .shelltime, add include to original |
| 59 | + if err := writeFileWithDir(expandedShelltime, content); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + newOriginal := directive.IncludeLine + "\n" + content |
| 63 | + return os.WriteFile(expandedOriginal, []byte(newOriginal), 0644) |
| 64 | + } |
| 65 | + |
| 66 | + if !hasInclude { |
| 67 | + // .shelltime exists but include line missing from original - add it |
| 68 | + newOriginal := directive.IncludeLine + "\n" + content |
| 69 | + return os.WriteFile(expandedOriginal, []byte(newOriginal), 0644) |
| 70 | + } |
| 71 | + |
| 72 | + // hasInclude && !shelltimeExists |
| 73 | + // Include line exists but .shelltime file was deleted - extract content |
| 74 | + contentWithoutInclude := removeIncludeLines(content, directive) |
| 75 | + return writeFileWithDir(expandedShelltime, contentWithoutInclude) |
| 76 | +} |
| 77 | + |
| 78 | +// ensureIncludeLineInFile ensures the include line exists in the original config file. |
| 79 | +// Used during pull to set up the include before saving the .shelltime file. |
| 80 | +func (b *BaseApp) ensureIncludeLineInFile(directive *IncludeDirective, isDryRun bool) error { |
| 81 | + expandedOriginal, err := b.expandPath(directive.OriginalPath) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + // Read original file (or treat as empty if it doesn't exist) |
| 87 | + var content string |
| 88 | + if data, err := os.ReadFile(expandedOriginal); err == nil { |
| 89 | + content = string(data) |
| 90 | + } else if !os.IsNotExist(err) { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + // Check if include already exists |
| 95 | + if strings.Contains(content, directive.CheckString) { |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + if isDryRun { |
| 100 | + slog.Info("[DRY RUN] Would add include line", slog.String("file", expandedOriginal)) |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + // Add include line at top |
| 105 | + var newContent string |
| 106 | + if content == "" { |
| 107 | + newContent = directive.IncludeLine + "\n" |
| 108 | + } else { |
| 109 | + newContent = directive.IncludeLine + "\n" + content |
| 110 | + } |
| 111 | + |
| 112 | + return writeFileWithDir(expandedOriginal, newContent) |
| 113 | +} |
| 114 | + |
| 115 | +// CollectWithIncludeSupport collects dotfiles, handling include directives. |
| 116 | +// For paths with include directives, it ensures the include setup and collects from .shelltime files. |
| 117 | +// For other paths (directories, non-includable files), it uses standard collection. |
| 118 | +func (b *BaseApp) CollectWithIncludeSupport(ctx context.Context, appName string, paths []string, skipIgnored *bool, directives []IncludeDirective) ([]DotfileItem, error) { |
| 119 | + // Build directive lookup by expanded original path |
| 120 | + directiveMap := make(map[string]*IncludeDirective) |
| 121 | + for i, d := range directives { |
| 122 | + expanded, err := b.expandPath(d.OriginalPath) |
| 123 | + if err == nil { |
| 124 | + directiveMap[expanded] = &directives[i] |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + var allDotfiles []DotfileItem |
| 129 | + var nonIncludePaths []string |
| 130 | + |
| 131 | + for _, path := range paths { |
| 132 | + expanded, err := b.expandPath(path) |
| 133 | + if err != nil { |
| 134 | + nonIncludePaths = append(nonIncludePaths, path) |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + directive, found := directiveMap[expanded] |
| 139 | + if !found { |
| 140 | + nonIncludePaths = append(nonIncludePaths, path) |
| 141 | + continue |
| 142 | + } |
| 143 | + |
| 144 | + // This path has include support |
| 145 | + // Check if original file exists |
| 146 | + if _, err := os.Stat(expanded); err != nil { |
| 147 | + slog.Debug("Original file not found, skipping include setup", slog.String("path", path)) |
| 148 | + continue |
| 149 | + } |
| 150 | + |
| 151 | + // Ensure include setup (adds include line, creates .shelltime file if needed) |
| 152 | + if err := b.ensureIncludeSetup(directive); err != nil { |
| 153 | + slog.Warn("Failed to ensure include setup", slog.String("path", path), slog.Any("err", err)) |
| 154 | + continue |
| 155 | + } |
| 156 | + |
| 157 | + // Collect from .shelltime file instead of original |
| 158 | + items, err := b.CollectFromPaths(ctx, appName, []string{directive.ShelltimePath}, skipIgnored) |
| 159 | + if err != nil { |
| 160 | + slog.Warn("Failed to collect from shelltime file", slog.String("path", directive.ShelltimePath), slog.Any("err", err)) |
| 161 | + continue |
| 162 | + } |
| 163 | + allDotfiles = append(allDotfiles, items...) |
| 164 | + } |
| 165 | + |
| 166 | + // Collect non-include paths normally (directories, non-includable files) |
| 167 | + if len(nonIncludePaths) > 0 { |
| 168 | + items, err := b.CollectFromPaths(ctx, appName, nonIncludePaths, skipIgnored) |
| 169 | + if err != nil { |
| 170 | + return allDotfiles, err |
| 171 | + } |
| 172 | + allDotfiles = append(allDotfiles, items...) |
| 173 | + } |
| 174 | + |
| 175 | + return allDotfiles, nil |
| 176 | +} |
| 177 | + |
| 178 | +// SaveWithIncludeSupport saves files, ensuring include directives for .shelltime files. |
| 179 | +// For .shelltime paths that match a known directive, it also ensures the include line |
| 180 | +// exists in the original config file. |
| 181 | +func (b *BaseApp) SaveWithIncludeSupport(ctx context.Context, files map[string]string, isDryRun bool, directives []IncludeDirective) error { |
| 182 | + // Build directive lookup by shelltime path (both tilde and expanded) |
| 183 | + shelltimeMap := make(map[string]*IncludeDirective) |
| 184 | + for i, d := range directives { |
| 185 | + shelltimeMap[d.ShelltimePath] = &directives[i] |
| 186 | + expanded, err := b.expandPath(d.ShelltimePath) |
| 187 | + if err == nil { |
| 188 | + shelltimeMap[expanded] = &directives[i] |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // For .shelltime files, ensure include line in the original config |
| 193 | + for filePath := range files { |
| 194 | + if directive, found := shelltimeMap[filePath]; found { |
| 195 | + if err := b.ensureIncludeLineInFile(directive, isDryRun); err != nil { |
| 196 | + slog.Warn("Failed to ensure include line", slog.String("path", filePath), slog.Any("err", err)) |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + // Use base Save for actual file writing |
| 202 | + return b.Save(ctx, files, isDryRun) |
| 203 | +} |
| 204 | + |
| 205 | +// writeFileWithDir writes content to a file, creating parent directories if needed. |
| 206 | +func writeFileWithDir(path, content string) error { |
| 207 | + dir := filepath.Dir(path) |
| 208 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + return os.WriteFile(path, []byte(content), 0644) |
| 212 | +} |
| 213 | + |
| 214 | +// removeIncludeLines removes the include directive lines from content. |
| 215 | +// First tries to match and remove from the top of the file. |
| 216 | +// Falls back to removing any lines containing the check string. |
| 217 | +func removeIncludeLines(content string, directive *IncludeDirective) string { |
| 218 | + lines := strings.Split(content, "\n") |
| 219 | + includeLines := strings.Split(directive.IncludeLine, "\n") |
| 220 | + |
| 221 | + // Try to find and remove the include lines from the top |
| 222 | + if len(lines) >= len(includeLines) { |
| 223 | + allMatch := true |
| 224 | + for i, il := range includeLines { |
| 225 | + if strings.TrimSpace(lines[i]) != strings.TrimSpace(il) { |
| 226 | + allMatch = false |
| 227 | + break |
| 228 | + } |
| 229 | + } |
| 230 | + if allMatch { |
| 231 | + remaining := lines[len(includeLines):] |
| 232 | + // Remove leading empty line if present (we add \n after include line) |
| 233 | + if len(remaining) > 0 && strings.TrimSpace(remaining[0]) == "" { |
| 234 | + remaining = remaining[1:] |
| 235 | + } |
| 236 | + return strings.Join(remaining, "\n") |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + // Fallback: remove any line containing the check string |
| 241 | + var filtered []string |
| 242 | + for _, line := range lines { |
| 243 | + if !strings.Contains(line, directive.CheckString) { |
| 244 | + filtered = append(filtered, line) |
| 245 | + } |
| 246 | + } |
| 247 | + return strings.Join(filtered, "\n") |
| 248 | +} |
0 commit comments