-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwildcat.go
202 lines (184 loc) · 4.87 KB
/
wildcat.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package wildcat
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"github.com/tamada/wildcat/errors"
)
// Wildcat is the struct treating to count the specified files, directories, and urls.
type Wildcat struct {
config *Config
eitherChan chan *Either
generator Generator
progress Progress
}
// NewWildcat creates an instance of Wildcat.
func NewWildcat(opts *ReadOptions, runtimeOpts *RuntimeOptions, generator Generator) *Wildcat {
channel := make(chan *Either)
return &Wildcat{
config: NewConfig(ignores(".", !opts.NoIgnore, nil), opts, runtimeOpts, errors.New()),
eitherChan: channel,
generator: generator,
progress: NewProgress(runtimeOpts.ShowProgress, runtimeOpts.ThreadNumber),
}
}
func (wc *Wildcat) run(f func(Generator, *Config) *Either) {
wc.progress.UpdateTarget()
go func() {
defer wc.progress.Done()
either := f(wc.generator, wc.config)
wc.eitherChan <- either
}()
}
func (wc *Wildcat) CountEntries(entries []Entry) (*ResultSet, *errors.Center) {
for _, entry := range entries {
e := entry
err := wc.handleItem(e)
wc.config.ec.Push(err)
}
go func() {
wc.progress.Wait()
wc.Close()
}()
return wc.receiveImpl()
}
// CountAll counts the arguments in the given Argf.
func (wc *Wildcat) CountAll(argf *Argf) (*ResultSet, *errors.Center) {
wc.progress.UpdateTarget()
go func() {
for _, arg := range argf.Arguments {
err := wc.handleItem(arg)
wc.config.ec.Push(err)
}
if len(argf.Arguments) == 0 {
wc.handleEntry(&stdinEntry{index: NewOrder()})
}
wc.progress.Done()
}()
go func() {
wc.progress.Wait()
wc.Close()
}()
return wc.receiveImpl()
}
func (wc *Wildcat) receiveImpl() (*ResultSet, *errors.Center) {
rs := NewResultSet()
for either := range wc.eitherChan {
receiveEither(either, rs, wc.config.ec)
}
return rs, wc.config.ec
}
// Close finishes the receiver object.
func (wc *Wildcat) Close() {
close(wc.eitherChan)
}
func (wc *Wildcat) updateFileList(fileList bool) *Wildcat {
newOpts := *wc.config.readOpts
newOpts.FileList = fileList
return wc.updateOpts(&newOpts)
}
// ReadFileListFromReader reads data from the given reader as the file list.
func (wc *Wildcat) ReadFileListFromReader(in io.Reader, index *Order) {
reader := bufio.NewReader(in)
order := index.Sub()
newWc := wc.updateFileList(false)
for {
line, err := reader.ReadString('\n')
line = strings.TrimSpace(line)
if line != "" && !newWc.config.IsIgnore(line) {
err := newWc.handleItem(NewArgWithIndex(order, line))
newWc.config.ec.Push(err)
}
if err == io.EOF {
break
}
order = order.Next()
}
}
func (wc *Wildcat) handleDir(arg NameAndIndex) *Either {
currentIgnore := ignores(arg.Name(), !wc.config.readOpts.NoIgnore, wc.config.ignore)
fileInfos, err := ioutil.ReadDir(arg.Name())
if err != nil {
return &Either{Err: err}
}
index := arg.Index().Sub()
for _, info := range fileInfos {
newName := filepath.Join(arg.Name(), info.Name())
if !isIgnore(wc.config.readOpts, currentIgnore, newName) {
newWc := wc.updateIgnore(currentIgnore)
err := newWc.handleItem(NewArgWithIndex(index, newName))
newWc.config.ec.Push(err)
index = index.Next()
}
}
return &Either{Results: []*Result{}}
}
func (wc *Wildcat) handleEntryAsFileList(entry Entry) *Either {
reader, err := entry.Open()
if err != nil {
return &Either{Err: err}
}
defer reader.Close()
wc.ReadFileListFromReader(reader, entry.Index())
return &Either{Results: []*Result{}}
}
func (wc *Wildcat) handleEntry(entry Entry) *Either {
targetEntry := entry
if !wc.config.readOpts.NoExtract {
newEntry, _ := ConvertToArchiveEntry(entry)
targetEntry = newEntry
}
if wc.config.readOpts.FileList {
return wc.handleEntryAsFileList(targetEntry)
}
wc.run(func(arg1 Generator, arg2 *Config) *Either {
return targetEntry.Count(wc.generator)
})
return &Either{Results: []*Result{}}
}
func (wc *Wildcat) handleItem(oldArg NameAndIndex) error {
arg := NormalizePath(oldArg)
name := arg.Name()
entry, ok := arg.(Entry)
switch {
case ok:
wc.handleEntry(entry)
case IsURL(name):
wc.handleEntry(toURLEntry(arg, wc.config.runtimeOpts))
case ExistDir(name):
wc.handleDir(arg)
case ExistFile(name):
wc.handleEntry(NewFileEntryWithIndex(arg))
default:
return fmt.Errorf("%s: file or directory not found", name)
}
return nil
}
func (wc *Wildcat) updateIgnore(newIgnore Ignore) *Wildcat {
return &Wildcat{
config: wc.config.updateIgnore(newIgnore),
eitherChan: wc.eitherChan,
generator: wc.generator,
progress: wc.progress,
}
}
func (wc *Wildcat) updateOpts(newOpts *ReadOptions) *Wildcat {
return &Wildcat{
config: wc.config.updateOpts(newOpts),
eitherChan: wc.eitherChan,
generator: wc.generator,
progress: wc.progress,
}
}
func receiveEither(either *Either, rs *ResultSet, ec *errors.Center) {
if either.Err != nil {
ec.Push(either.Err)
} else {
for _, result := range either.Results {
rs.Push(result)
}
}
}