forked from ayoisaiah/f2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf2.go
87 lines (66 loc) · 1.62 KB
/
f2.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
package f2
import (
"errors"
"fmt"
"io"
"log/slog"
"github.com/urfave/cli/v2"
"github.com/ayoisaiah/f2/app"
"github.com/ayoisaiah/f2/find"
"github.com/ayoisaiah/f2/internal/config"
"github.com/ayoisaiah/f2/rename"
"github.com/ayoisaiah/f2/replace"
"github.com/ayoisaiah/f2/report"
"github.com/ayoisaiah/f2/validate"
)
var errConflictDetected = errors.New(
"resolve conflicts before proceeding or use -F/--fix-conflicts to auto-fix",
)
// run starts a new renaming operation.
func run(ctx *cli.Context) error {
// TODO: Log the final context
conf, err := config.Init(ctx)
if err != nil {
return err
}
slog.Debug("configuration", slog.Any("config", conf))
report.Stdout = conf.Stdout
report.Stderr = conf.Stderr
if conf.Revert {
return rename.Undo(conf)
}
matches, err := find.Find(conf)
if err != nil {
return err
}
if len(matches) == 0 {
slog.Debug("no matches were found", slog.Any("find_matches", matches))
report.NoMatches(conf.JSON)
return nil
}
slog.Debug(
fmt.Sprintf("found %d matches", len(matches)),
slog.Any("find_matches", matches),
slog.Int("num_matches", len(matches)),
)
changes, err := replace.Replace(conf, matches)
if err != nil {
return err
}
slog.Debug("replacements done", slog.Any("changes", changes))
conflicts := validate.Validate(
changes,
conf.AutoFixConflicts,
conf.AllowOverwrites,
)
if len(conflicts) > 0 {
report.Conflicts(conflicts, conf.JSON)
return errConflictDetected
}
return rename.Rename(conf, changes)
}
func New(reader io.Reader, writer io.Writer) *cli.App {
f2App := app.Get(reader, writer)
f2App.Action = run
return f2App
}