-
Notifications
You must be signed in to change notification settings - Fork 6
/
command.go
118 lines (97 loc) · 2.04 KB
/
command.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
118
package main
import (
"errors"
"fmt"
"io"
"path/filepath"
"sync"
"github.com/go-git/go-billy/v5"
"github.com/logrusorgru/aurora/v3"
)
type command struct {
argumentParser *argumentParser
fileFinder *fileFinder
fileRenamer *fileRenamer
fileSystem billy.Filesystem
stdout io.Writer
stderr io.Writer
}
func newCommand(p *argumentParser, g *fileFinder, r *fileRenamer, fs billy.Filesystem, stdout, stderr io.Writer) *command {
return &command{p, g, r, fs, stdout, stderr}
}
func (c *command) Run(ss []string) error {
args, err := c.argumentParser.Parse(ss)
if err != nil {
return err
} else if args.Help {
fmt.Fprint(c.stdout, c.argumentParser.Help())
return nil
} else if args.Version {
fmt.Fprintln(c.stdout, version)
return nil
}
r, err := newCaseTextRenamer(args.From, args.To, args.CaseNames)
if err != nil {
return err
}
if args.Bare {
r = newBareTextRenamer(args.From, args.To)
} else if args.Regexp {
r, err = newRegexpTextRenamer(args.From, args.To)
if err != nil {
return err
}
}
i, err := c.fileSystem.Stat(args.Path)
if err != nil {
return err
} else if !i.IsDir() {
// Rename only filenames but not their directories.
return c.fileRenamer.Rename(
r,
args.Path,
filepath.Dir(args.Path),
args.Verbose,
)
}
ss, err = c.fileFinder.Find(
args.Path,
args.Include,
args.Exclude,
args.NoGit,
)
if err != nil {
return err
}
g := &sync.WaitGroup{}
sm := newSemaphore(maxOpenFiles)
ec := make(chan error, errorChannelCapacity)
for _, s := range ss {
g.Add(1)
go func(s string) {
defer g.Done()
sm.Request()
defer sm.Release()
err = c.fileRenamer.Rename(r, s, args.Path, args.Verbose)
if err != nil {
ec <- fmt.Errorf("%v: %v", s, err)
}
}(s)
}
go func() {
g.Wait()
close(ec)
}()
ok := true
for err := range ec {
ok = false
c.printError(err)
}
if !ok {
return errors.New("failed to rename some identifiers")
}
return nil
}
func (c *command) printError(err error) {
fmt.Fprintln(c.stderr, aurora.Red(err))
}