Skip to content

Commit

Permalink
rename Change fields for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Aug 24, 2024
1 parent 476b381 commit 09b6f3e
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 87 deletions.
12 changes: 6 additions & 6 deletions find/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions find/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
30 changes: 15 additions & 15 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 8 additions & 8 deletions internal/sortfiles/sortfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand All @@ -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
}

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/sortfiles/sortfiles_test/sortfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions rename/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -44,7 +44,7 @@ func commit(fileChanges file.Changes) []int {
// 2. Rename <source> to <target>
// 3. Rename __<time>__<target>__<time>__ to <target>
var isCaseChangeOnly bool // only the target case is changing
if strings.EqualFold(change.RelSourcePath, targetPath) {
if strings.EqualFold(change.SourcePath, targetPath) {
isCaseChangeOnly = true
timeStr := fmt.Sprintf("%d", time.Now().UnixNano())
targetPath = filepath.Join(
Expand Down Expand Up @@ -74,11 +74,11 @@ func commit(fileChanges file.Changes) []int {
}
}

err := os.Rename(change.RelSourcePath, targetPath) // step 2
err := os.Rename(change.SourcePath, targetPath) // step 2
// if the intermediate rename is successful,
// proceed with the original renaming operation
if err == nil && isCaseChangeOnly {
err = os.Rename(targetPath, change.RelTargetPath) // step 3
err = os.Rename(targetPath, change.TargetPath) // step 3
}

if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions rename/rename_test/rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func renameTest(t *testing.T, cases []testutil.TestCase) {
ch := tc.Changes[j]

cases[i].Changes[j].BaseDir = baseDirPath
cases[i].Changes[j].RelSourcePath = filepath.Join(
cases[i].Changes[j].SourcePath = filepath.Join(
ch.BaseDir,
ch.Source,
)
cases[i].Changes[j].RelTargetPath = filepath.Join(
cases[i].Changes[j].TargetPath = filepath.Join(
ch.BaseDir,
ch.Target,
)
Expand Down Expand Up @@ -136,12 +136,12 @@ func postRename(t *testing.T, cases []testutil.TestCase) {
for j := range tc.Changes {
ch := tc.Changes[j]

cases[i].Changes[j].OriginalSource = ch.Source
cases[i].Changes[j].RelSourcePath = filepath.Join(
cases[i].Changes[j].OriginalName = ch.Source
cases[i].Changes[j].SourcePath = filepath.Join(
ch.BaseDir,
ch.Source,
)
cases[i].Changes[j].RelTargetPath = filepath.Join(
cases[i].Changes[j].TargetPath = filepath.Join(
ch.BaseDir,
ch.Target,
)
Expand Down
2 changes: 1 addition & 1 deletion rename/rename_test/testdata/rename_a_file_backup.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"status":"","base_dir":"","source":"File.txt","target":"myFile.txt","is_dir":false,"will_overwrite":false}]
[{"status":"","base_dir":"","source":"File.txt","target":"myFile.txt","is_dir":false}]
2 changes: 1 addition & 1 deletion replace/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func replaceMatches(

change.Target = strings.TrimSpace(filepath.Clean(change.Target))
change.Status = status.OK
change.RelTargetPath = filepath.Join(change.BaseDir, change.Target)
change.TargetPath = filepath.Join(change.BaseDir, change.Target)
matches[i] = change
}

Expand Down
4 changes: 2 additions & 2 deletions replace/replace_test/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func replaceTest(t *testing.T, cases []testutil.TestCase) {
for j := range tc.Changes {
ch := tc.Changes[j]

cases[i].Changes[j].OriginalSource = ch.Source
cases[i].Changes[j].RelSourcePath = filepath.Join(
cases[i].Changes[j].OriginalName = ch.Source
cases[i].Changes[j].SourcePath = filepath.Join(
ch.BaseDir,
ch.Source,
)
Expand Down
16 changes: 8 additions & 8 deletions replace/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,10 +924,10 @@ func replaceVariables(
change *file.Change,
vars *variables,
) error {
fileExt := filepath.Ext(change.OriginalSource)
fileExt := filepath.Ext(change.OriginalName)

if len(vars.filename.matches) > 0 {
sourceName := filepath.Base(change.OriginalSource)
sourceName := filepath.Base(change.OriginalName)
if !change.IsDir {
sourceName = pathutil.StripExtension(sourceName)
}
Expand All @@ -948,7 +948,7 @@ func replaceVariables(
}

if len(vars.parentDir.matches) > 0 {
abspath, err := filepath.Abs(change.RelSourcePath)
abspath, err := filepath.Abs(change.SourcePath)
if err != nil {
return err
}
Expand All @@ -963,7 +963,7 @@ func replaceVariables(
if len(vars.date.matches) > 0 {
out, err := replaceDateVars(
change.Target,
change.RelSourcePath,
change.SourcePath,
vars.date,
)
if err != nil {
Expand All @@ -976,7 +976,7 @@ func replaceVariables(
if len(vars.exiftool.matches) > 0 {
out, err := replaceExifToolVars(
change.Target,
change.RelSourcePath,
change.SourcePath,
vars.exiftool,
)
if err != nil {
Expand All @@ -989,7 +989,7 @@ func replaceVariables(
if len(vars.exif.matches) > 0 {
out, err := replaceExifVars(
change.Target,
change.RelSourcePath,
change.SourcePath,
vars.exif,
)
if err != nil {
Expand All @@ -1002,7 +1002,7 @@ func replaceVariables(
if len(vars.id3.matches) > 0 {
out, err := replaceID3Variables(
change.Target,
change.RelSourcePath,
change.SourcePath,
vars.id3,
)
if err != nil {
Expand All @@ -1021,7 +1021,7 @@ func replaceVariables(
if len(vars.hash.matches) > 0 {
out, err := replaceFileHashVars(
change.Target,
change.RelSourcePath,
change.SourcePath,
vars.hash,
)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func PrintResults(conf *config.Config, fileChanges file.Changes) {
change := fileChanges[i]

if conf.IsOutputToPipe && change.Error != nil {
pterm.Println(change.RelTargetPath)
pterm.Println(change.TargetPath)
}

if !conf.Verbose {
Expand All @@ -93,8 +93,8 @@ func PrintResults(conf *config.Config, fileChanges file.Changes) {
pterm.Fprintln(config.Stderr,
pterm.Error.Sprintf(
"error renaming '%s' to '%s': %v",
change.RelSourcePath,
change.RelTargetPath,
change.SourcePath,
change.TargetPath,
change.Error,
),
)
Expand All @@ -105,8 +105,8 @@ func PrintResults(conf *config.Config, fileChanges file.Changes) {
pterm.Fprintln(config.Stderr,
pterm.Success.Sprintf(
"renamed: '%s' to '%s'",
change.RelSourcePath,
change.RelTargetPath,
change.SourcePath,
change.TargetPath,
),
)
}
Expand Down
12 changes: 6 additions & 6 deletions report/report_test/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func TestReport(t *testing.T) {
Name: "report unchanged file names",
Changes: file.Changes{
{
RelSourcePath: "macos_update_notes_2023.txt",
RelTargetPath: "macos_update_notes_2023.txt",
Status: status.Unchanged,
SourcePath: "macos_update_notes_2023.txt",
TargetPath: "macos_update_notes_2023.txt",
Status: status.Unchanged,
},
{
RelSourcePath: "macos_user_guide_macos_sierra.pdf",
RelTargetPath: "macos_user_guide_macos_sierra.pdf",
Status: status.Unchanged,
SourcePath: "macos_user_guide_macos_sierra.pdf",
TargetPath: "macos_user_guide_macos_sierra.pdf",
Status: status.Unchanged,
},
},
Args: []string{"-r"},
Expand Down
Loading

0 comments on commit 09b6f3e

Please sign in to comment.