From 09b6f3e5b8bab32eb8a2d9e2884f934e60ec02e7 Mon Sep 17 00:00:00 2001 From: Ayooluwa Isaiah Date: Sat, 24 Aug 2024 08:53:40 +0100 Subject: [PATCH] rename Change fields for clarity --- find/csv.go | 12 ++++---- find/find.go | 14 ++++----- internal/file/file.go | 30 +++++++++---------- internal/sortfiles/sortfiles.go | 16 +++++----- .../sortfiles_test/sortfiles_test.go | 6 ++-- internal/testutil/testutil.go | 4 +-- rename/rename.go | 10 +++---- rename/rename_test/rename_test.go | 10 +++---- .../testdata/rename_a_file_backup.golden | 2 +- replace/replace.go | 2 +- replace/replace_test/replace_test.go | 4 +-- replace/variables.go | 16 +++++----- report/report.go | 10 +++---- report/report_test/report_test.go | 12 ++++---- validate/validate.go | 20 ++++++------- validate/validate_test/validate_test.go | 6 ++-- 16 files changed, 87 insertions(+), 87 deletions(-) diff --git a/find/csv.go b/find/csv.go index 2859ccf..c5b0271 100644 --- a/find/csv.go +++ b/find/csv.go @@ -82,12 +82,12 @@ func handleCSV(conf *config.Config) (file.Changes, error) { processed[absSourcePath] = true match := &file.Change{ - BaseDir: sourceDir, - IsDir: fileInfo.IsDir(), - Source: fileName, - OriginalSource: fileName, - RelSourcePath: absSourcePath, - CSVRow: record, + BaseDir: sourceDir, + IsDir: fileInfo.IsDir(), + Source: fileName, + OriginalName: fileName, + SourcePath: absSourcePath, + CSVRow: record, } changes = append(changes, match) diff --git a/find/find.go b/find/find.go index 57bb845..91c5e9d 100644 --- a/find/find.go +++ b/find/find.go @@ -101,11 +101,11 @@ func createFileChange(dirPath string, fileInfo fs.FileInfo) *file.Change { fileName := fileInfo.Name() match := &file.Change{ - BaseDir: baseDir, - IsDir: fileInfo.IsDir(), - Source: fileName, - OriginalSource: fileName, - RelSourcePath: filepath.Join(baseDir, fileName), + BaseDir: baseDir, + IsDir: fileInfo.IsDir(), + Source: fileName, + OriginalName: fileName, + SourcePath: filepath.Join(baseDir, fileName), } return match @@ -247,8 +247,8 @@ func loadFromBackup(conf *config.Config) (file.Changes, error) { for i := range changes { ch := changes[i] ch.Source, ch.Target = ch.Target, ch.Source - ch.RelSourcePath = filepath.Join(ch.BaseDir, ch.Source) - ch.RelTargetPath = filepath.Join(ch.BaseDir, ch.Target) + ch.SourcePath = filepath.Join(ch.BaseDir, ch.Source) + ch.TargetPath = filepath.Join(ch.BaseDir, ch.Target) changes[i] = ch } diff --git a/internal/file/file.go b/internal/file/file.go index a0f6b79..27247ad 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -12,29 +12,29 @@ import ( ) // Change represents a single renaming change. -// TODO: review the naming of these fields especially -// OriginalSource, RelSourcePath, & RelTargetPath type Change struct { - Error error `json:"error,omitempty"` - OriginalSource string `json:"-"` - Status status.Status `json:"status"` - BaseDir string `json:"base_dir"` - Source string `json:"source"` - Target string `json:"target"` - // RelSourcePath is BaseDir + Source - RelSourcePath string `json:"-"` - // RelTargetPath is BaseDir + Target - RelTargetPath string `json:"-"` + Error error `json:"error,omitempty"` + // The original filename which can be different from source in + // a multi-step renaming operation + OriginalName string `json:"-"` + Status status.Status `json:"status"` + BaseDir string `json:"base_dir"` + Source string `json:"source"` + Target string `json:"target"` + // SourcePath is BaseDir + Source + SourcePath string `json:"-"` + // TargetPath is BaseDir + Target + TargetPath string `json:"-"` CSVRow []string `json:"-"` Position int `json:"-"` IsDir bool `json:"is_dir"` - WillOverwrite bool `json:"will_overwrite"` + WillOverwrite bool `json:"-"` } // AutoFixTarget sets the new target name func (c *Change) AutoFixTarget(newTarget string) { c.Target = newTarget - c.RelTargetPath = filepath.Join(c.BaseDir, c.Target) + c.TargetPath = filepath.Join(c.BaseDir, c.Target) c.Status = status.OK } @@ -81,7 +81,7 @@ func (c Changes) RenderTable(w io.Writer) { changeStatus = pterm.Red(strings.TrimPrefix(msg, ": ")) } - d := []string{change.RelSourcePath, change.RelTargetPath, changeStatus} + d := []string{change.SourcePath, change.TargetPath, changeStatus} data[change.Position] = d } diff --git a/internal/sortfiles/sortfiles.go b/internal/sortfiles/sortfiles.go index ccf8f6b..c1ccdce 100644 --- a/internal/sortfiles/sortfiles.go +++ b/internal/sortfiles/sortfiles.go @@ -61,8 +61,8 @@ func ByTime( sortPerDir bool, ) { slices.SortStableFunc(changes, func(a, b *file.Change) int { - sourceA, errA := times.Stat(a.RelSourcePath) - sourceB, errB := times.Stat(b.RelSourcePath) + sourceA, errA := times.Stat(a.SourcePath) + sourceB, errB := times.Stat(b.SourcePath) if errA != nil || errB != nil { pterm.Error.Printfln( @@ -99,7 +99,7 @@ func ByTime( } if sortPerDir && - filepath.Dir(a.RelSourcePath) != filepath.Dir(b.RelSourcePath) { + filepath.Dir(a.SourcePath) != filepath.Dir(b.SourcePath) { return 0 } @@ -116,8 +116,8 @@ func ByTime( func BySize(changes file.Changes, reverseSort, sortPerDir bool) { slices.SortStableFunc(changes, func(a, b *file.Change) int { var fileInfoA, fileInfoB fs.FileInfo - fileInfoA, errA := os.Stat(a.RelSourcePath) - fileInfoB, errB := os.Stat(b.RelSourcePath) + fileInfoA, errA := os.Stat(a.SourcePath) + fileInfoB, errB := os.Stat(b.SourcePath) if errA != nil || errB != nil { pterm.Error.Printfln("error getting file info: %v, %v", errA, errB) @@ -129,7 +129,7 @@ func BySize(changes file.Changes, reverseSort, sortPerDir bool) { // Don't sort files in different directories relative to each other if sortPerDir && - filepath.Dir(a.RelSourcePath) != filepath.Dir(b.RelSourcePath) { + filepath.Dir(a.SourcePath) != filepath.Dir(b.SourcePath) { return 0 } @@ -146,8 +146,8 @@ func BySize(changes file.Changes, reverseSort, sortPerDir bool) { // ASCII order. func Natural(changes file.Changes, reverseSort bool) { sort.SliceStable(changes, func(i, j int) bool { - sourceA := changes[i].RelSourcePath - sourceB := changes[j].RelSourcePath + sourceA := changes[i].SourcePath + sourceB := changes[j].SourcePath if reverseSort { return !natsort.Compare(sourceA, sourceB) diff --git a/internal/sortfiles/sortfiles_test/sortfiles_test.go b/internal/sortfiles/sortfiles_test/sortfiles_test.go index 953ac34..f803d8b 100644 --- a/internal/sortfiles/sortfiles_test/sortfiles_test.go +++ b/internal/sortfiles/sortfiles_test/sortfiles_test.go @@ -32,9 +32,9 @@ func sortTest(t *testing.T, unsorted []string) file.Changes { v := unsorted[i] changes[i] = &file.Change{ - Source: filepath.Base(v), - BaseDir: filepath.Dir(v), - RelSourcePath: v, + Source: filepath.Base(v), + BaseDir: filepath.Dir(v), + SourcePath: v, } f, err := os.Stat(v) diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index 11ee07d..05444d8 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -103,7 +103,7 @@ func CompareSourcePath(t *testing.T, want []string, changes file.Changes) { got := make([]string, len(changes)) for i := range changes { - got[i] = changes[i].RelSourcePath + got[i] = changes[i].SourcePath } assert.Equal(t, want, got) @@ -116,7 +116,7 @@ func CompareTargetPath(t *testing.T, want []string, changes file.Changes) { got := make([]string, len(changes)) for i := range changes { - got[i] = changes[i].RelTargetPath + got[i] = changes[i].TargetPath } assert.Equal(t, want, got) diff --git a/rename/rename.go b/rename/rename.go index 99ad73d..ef1e093 100644 --- a/rename/rename.go +++ b/rename/rename.go @@ -30,10 +30,10 @@ func commit(fileChanges file.Changes) []int { for i := range fileChanges { change := fileChanges[i] - targetPath := change.RelTargetPath + targetPath := change.TargetPath // skip paths that are unchanged in every aspect - if change.RelSourcePath == targetPath { + if change.SourcePath == targetPath { continue } @@ -44,7 +44,7 @@ func commit(fileChanges file.Changes) []int { // 2. Rename to // 3. Rename __