forked from ayoisaiah/f2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
f2.go
78 lines (59 loc) · 1.47 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
package f2
import (
"io"
"github.com/urfave/cli/v2"
"github.com/ayoisaiah/f2/app"
"github.com/ayoisaiah/f2/find"
"github.com/ayoisaiah/f2/internal/apperr"
"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 = &apperr.Error{
Message: "conflict: resolve manually or use -F/--fix-conflicts",
}
// execute initiates a new renaming operation based on the provided CLI context.
func execute(_ *cli.Context) error {
appConfig := config.Get()
changes, err := find.Find(appConfig)
if err != nil {
return err
}
if len(changes) == 0 {
report.NoMatches(appConfig)
return nil
}
if !appConfig.Revert {
changes, err = replace.Replace(appConfig, changes)
if err != nil {
return err
}
}
hasConflicts := validate.Validate(
changes,
appConfig.AutoFixConflicts,
appConfig.AllowOverwrites,
)
if hasConflicts {
report.Report(appConfig, changes, hasConflicts)
return errConflictDetected
}
if !appConfig.Exec {
report.Report(appConfig, changes, hasConflicts)
return nil
}
err = rename.Rename(appConfig, changes)
rename.PostRename(appConfig, changes, err)
return err
}
// New creates a new CLI application for f2.
func New(reader io.Reader, writer io.Writer) (*cli.App, error) {
renamer, err := app.Get(reader, writer)
if err != nil {
return nil, err
}
renamer.Action = execute
return renamer, nil
}