-
Notifications
You must be signed in to change notification settings - Fork 0
/
refactor.go
91 lines (75 loc) · 2.05 KB
/
refactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Karl Ramberg
Loupe v0.1.0
refactor.go
*/
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
)
func refactor(dir, typeStr, old, new string) error {
fmt.Println("Loupe", loupeVersion, "-", "Rename")
if dir == "" {
return errors.New("provide an archive directory using the -a flag")
}
stats, err := os.Stat(dir)
if os.IsNotExist(err) || !stats.IsDir() {
return errors.New("directory \"" + dir + "\" not found")
}
validType, err := validType(typeStr)
if !validType {
return err
}
validOld, err := validWord(old)
validNew, err2 := validWord(new)
if !validOld || !validNew {
return errors.Join(err, err2)
}
files, err := getImageFiles(dir)
if err != nil {
return errors.Join(errors.New("trouble getting images files from \""+dir+"\""), err)
}
if len(files) == 0 {
return errors.New("no image files found in \"" + dir + "\"")
}
var renameCount int
for _, file := range files {
var photograph Photograph
err := photograph.init(filepath.Base(file))
if err != nil {
continue
}
// Save the photograph's old location before it is changed
oldDir := photograph.directory()
if typeStr == "class" && photograph.class == old {
photograph.class = new
} else if typeStr == "group" && photograph.group == old {
photograph.group = new
} else if typeStr == "version" && photograph.version == old {
photograph.version = new
} else if typeStr == "subversion" && photograph.subversion == old {
photograph.subversion = new
}
// Update only the filename with the new grouping
oldpath := file
newpath := filepath.Join(dir, filepath.Join(oldDir, photograph.filename()))
_, err = os.Stat(newpath)
if os.IsNotExist(err) && oldpath != newpath {
err = os.Rename(oldpath, newpath)
if err != nil {
return errors.Join(errors.New("trouble while renaming \""+oldpath+"\""), err)
} else {
fmt.Println("Renamed", oldpath, "to", filepath.Base(newpath))
renameCount++
}
}
}
fmt.Printf("%d files renamed\n", renameCount)
fmt.Println()
// Sort the files using their new grouping
sort(dir)
return nil
}