tryPathMigration attempts to migrate from old pattern paths to new restructured paths
()
| 248 | |
| 249 | // tryPathMigration attempts to migrate from old pattern paths to new restructured paths |
| 250 | func (o *PatternsLoader) tryPathMigration() (err error) { |
| 251 | // Check if current path is the old "patterns" path |
| 252 | if o.DefaultFolder.Value == "patterns" { |
| 253 | fmt.Println(i18n.T("patterns_detected_old_path")) |
| 254 | |
| 255 | // Try the new restructured path |
| 256 | newPath := "data/patterns" |
| 257 | testTempFolder := filepath.Join(os.TempDir(), "fabric-patterns-test") |
| 258 | |
| 259 | // Clean up any existing test temp folder |
| 260 | if err := os.RemoveAll(testTempFolder); err != nil { |
| 261 | fmt.Printf(i18n.T("patterns_warning_remove_test_folder"), testTempFolder, err) |
| 262 | } |
| 263 | |
| 264 | // Test if the new path works |
| 265 | testErr := githelper.FetchFilesFromRepo(githelper.FetchOptions{ |
| 266 | RepoURL: o.DefaultGitRepoUrl.Value, |
| 267 | PathPrefix: newPath, |
| 268 | DestDir: testTempFolder, |
| 269 | }) |
| 270 | |
| 271 | if testErr == nil { |
| 272 | // Check if patterns exist in the new path |
| 273 | if patternCount, countErr := o.countPatternsInDirectory(testTempFolder); countErr == nil && patternCount > 0 { |
| 274 | fmt.Printf(i18n.T("patterns_found_new_path"), patternCount, newPath) |
| 275 | |
| 276 | // Update the configuration |
| 277 | o.DefaultFolder.Value = newPath |
| 278 | // Clean up the main temp folder and replace it with the test one |
| 279 | os.RemoveAll(o.tempPatternsFolder) |
| 280 | if renameErr := os.Rename(testTempFolder, o.tempPatternsFolder); renameErr != nil { |
| 281 | // If rename fails, try copy |
| 282 | if copyErr := copy.Copy(testTempFolder, o.tempPatternsFolder); copyErr != nil { |
| 283 | return fmt.Errorf(i18n.T("patterns_failed_move_test_patterns"), copyErr) |
| 284 | } |
| 285 | os.RemoveAll(testTempFolder) |
| 286 | } |
| 287 | |
| 288 | return nil |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Clean up test folder |
| 293 | os.RemoveAll(testTempFolder) |
| 294 | } |
| 295 | |
| 296 | return fmt.Errorf(i18n.T("patterns_unable_to_find_or_migrate"), o.DefaultFolder.Value) |
| 297 | } |
| 298 | |
| 299 | // countPatternsInDirectory counts the number of pattern directories in a given directory |
| 300 | func (o *PatternsLoader) countPatternsInDirectory(dir string) (int, error) { |
no test coverage detected