-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix.go
117 lines (109 loc) · 2.75 KB
/
fix.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package people
import (
"fmt"
"strings"
"time"
"github.com/Defacto2/df2/lib/database"
"github.com/Defacto2/df2/lib/logs"
"github.com/Defacto2/df2/lib/str"
"github.com/gookit/color" //nolint:misspell
)
// Fix any malformed group names found in the database.
func Fix(simulate bool) error {
c, start := 0, time.Now()
for _, r := range []Role{Artists, Coders, Musicians, Writers} {
credits, _, err := List(r)
if err != nil {
return err
}
for _, credit := range credits {
if r := cleanPeople(credit, r, simulate); r {
c++
}
}
}
switch {
case c > 0 && simulate:
logs.Printcrf("%d fixes required", c)
logs.Simulate()
case c == 1:
logs.Printcr("1 fix applied")
case c > 0:
logs.Printcrf("%d fixes applied", c)
default:
logs.Printcr("no people fixes needed")
}
elapsed := time.Since(start).Seconds()
logs.Print(fmt.Sprintf(", time taken %.1f seconds\n", elapsed))
return nil
}
// cleanPeople fixes and saves a malformed group name.
func cleanPeople(credit string, r Role, sim bool) (ok bool) {
rep := cleanPerson(cleanString(credit))
if rep == credit {
return false
}
if sim {
logs.Printf("\n%s %q %s %s", color.Question.Sprint("?"), credit,
color.Question.Sprint("!="), color.Info.Sprint(r))
return true
}
s := str.Y()
ok = true
c, err := rename(rep, credit, r)
if err != nil {
s = str.X()
ok = false
}
logs.Printf("\n%s %q %s %s (%d)", s, credit, color.Question.Sprint("⟫"), color.Info.Sprint(rep), c)
return ok
}
func cleanPerson(s string) string {
ppl := strings.Split(s, ",")
for i, person := range ppl {
ss := database.StripStart(person)
if ss != person {
ppl[i] = ss
}
}
return strings.Join(ppl, ",")
}
func rename(replacement, credits string, r Role) (count int64, err error) {
query := ""
switch r {
case Artists:
query = "UPDATE `files` SET credit_illustration=? WHERE credit_illustration=?"
case Coders:
query = "UPDATE `files` SET credit_program=? WHERE credit_program=?"
case Musicians:
query = "UPDATE `files` SET credit_audio=? WHERE credit_audio=?"
case Writers:
query = "UPDATE `files` SET credit_text=? WHERE credit_text=?"
case Everyone:
query = ""
}
db := database.Connect()
defer db.Close()
stmt, err := db.Prepare(query)
if err != nil {
return 0, fmt.Errorf("rename people statement: %w", err)
}
defer stmt.Close()
res, err := stmt.Exec(replacement, credits)
if err != nil {
return 0, fmt.Errorf("rename people exec: %w", err)
}
count, err = res.RowsAffected()
if err != nil {
return 0, fmt.Errorf("rename people rows affected: %w", err)
}
return count, db.Close()
}
// cleanString fixes any malformed strings.
func cleanString(s string) string {
f := database.TrimSP(s)
f = database.StripChars(f)
f = database.StripStart(f)
f = strings.TrimSpace(f)
return f
}