-
Notifications
You must be signed in to change notification settings - Fork 8
/
pipeline_tree.go
235 lines (202 loc) · 6.5 KB
/
pipeline_tree.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
//go:build !tinywasm
package gtree
import (
"context"
"io"
"github.com/fatih/color"
"golang.org/x/sync/errgroup"
)
type treePipeline struct {
grower growerPipeline
spreader spreaderPipeline
mkdirer mkdirerPipeline
verifier verifierPipeline
walker walkerPipeline
}
var _ tree = (*treePipeline)(nil)
func newTreePipeline(cfg *config) tree {
growerFactory := func(lastNodeFormat, intermedialNodeFormat branchFormat, dryrun bool, encode encode) growerPipeline {
if encode != encodeDefault {
return newNopGrowerPipeline()
}
return newGrowerPipeline(lastNodeFormat, intermedialNodeFormat, dryrun)
}
spreaderFactory := func(encode encode, dryrun bool, fileExtensions []string) spreaderPipeline {
if dryrun {
return newColorizeSpreaderPipeline(fileExtensions)
}
return newSpreaderPipeline(encode)
}
mkdirerFactory := func(targetDir string, fileExtensions []string) mkdirerPipeline {
return newMkdirerPipeline(targetDir, fileExtensions)
}
verifierFactory := func(targetDir string, strict bool) verifierPipeline {
return newVerifierPipeline(targetDir, strict)
}
walkerFactory := func() walkerPipeline {
return newWalkerPipeline()
}
return &treePipeline{
grower: growerFactory(
cfg.lastNodeFormat,
cfg.intermedialNodeFormat,
cfg.dryrun,
cfg.encode,
),
spreader: spreaderFactory(
cfg.encode,
cfg.dryrun,
cfg.fileExtensions,
),
mkdirer: mkdirerFactory(
cfg.targetDir,
cfg.fileExtensions,
),
verifier: verifierFactory(
cfg.targetDir,
cfg.strictVerify,
),
walker: walkerFactory(),
}
}
func (t *treePipeline) output(w io.Writer, r io.Reader, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
splitStream, errcsl := split(ctx, r)
rootStream, errcr := newRootGeneratorPipeline().generate(ctx, splitStream)
growStream, errcg := t.grower.grow(ctx, rootStream)
errcs := t.spreader.spread(ctx, w, growStream)
return t.handlePipelineErr(ctx, errcsl, errcr, errcg, errcs)
}
func (t *treePipeline) outputProgrammably(w io.Writer, root *Node, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
rootStream := make(chan *Node)
go func() {
defer close(rootStream)
rootStream <- root
}()
growStream, errcg := t.grower.grow(ctx, rootStream)
errcs := t.spreader.spread(ctx, w, growStream)
return t.handlePipelineErr(ctx, errcg, errcs)
}
func (t *treePipeline) mkdir(r io.Reader, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
splitStream, errcsl := split(ctx, r)
rootStream, errcr := newRootGeneratorPipeline().generate(ctx, splitStream)
growStream, errcg := t.grower.grow(ctx, rootStream)
errcm := t.mkdirer.mkdir(ctx, growStream)
return t.handlePipelineErr(ctx, errcsl, errcr, errcg, errcm)
}
func (t *treePipeline) mkdirProgrammably(root *Node, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
rootStream := make(chan *Node)
go func() {
defer close(rootStream)
rootStream <- root
}()
t.grower.enableValidation()
// when detect invalid node name, return error. process end.
growStream, errcg := t.grower.grow(ctx, rootStream)
if cfg.dryrun {
// when detected no invalid node name, output tree.
errcs := t.spreader.spread(ctx, color.Output, growStream)
return t.handlePipelineErr(ctx, errcg, errcs)
}
// when detected no invalid node name, no output tree.
errcm := t.mkdirer.mkdir(ctx, growStream)
return t.handlePipelineErr(ctx, errcg, errcm)
}
func (t *treePipeline) verify(r io.Reader, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
t.grower.enableValidation()
splitStream, errcsl := split(ctx, r)
rootStream, errcr := newRootGeneratorPipeline().generate(ctx, splitStream)
growStream, errcg := t.grower.grow(ctx, rootStream)
errcv := t.verifier.verify(ctx, growStream)
return t.handlePipelineErr(ctx, errcsl, errcr, errcg, errcv)
}
func (t *treePipeline) verifyProgrammably(root *Node, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
rootStream := make(chan *Node)
go func() {
defer close(rootStream)
rootStream <- root
}()
t.grower.enableValidation()
// when detect invalid node name, return error. process end.
growStream, errcg := t.grower.grow(ctx, rootStream)
// when detected no invalid node name, no output tree.
errcv := t.verifier.verify(ctx, growStream)
return t.handlePipelineErr(ctx, errcg, errcv)
}
func (t *treePipeline) walk(r io.Reader, callback func(*WalkerNode) error, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
splitStream, errcsl := split(ctx, r)
rootStream, errcr := newRootGeneratorPipeline().generate(ctx, splitStream)
growStream, errcg := t.grower.grow(ctx, rootStream)
errcw := t.walker.walk(ctx, growStream, callback)
return t.handlePipelineErr(ctx, errcsl, errcr, errcg, errcw)
}
func (t *treePipeline) walkProgrammably(root *Node, callback func(*WalkerNode) error, cfg *config) error {
ctx, cancel := context.WithCancel(cfg.ctx)
defer cancel()
rootStream := make(chan *Node)
go func() {
defer close(rootStream)
rootStream <- root
}()
growStream, errcg := t.grower.grow(ctx, rootStream)
errcw := t.walker.walk(ctx, growStream, callback)
return t.handlePipelineErr(ctx, errcg, errcw)
}
// 関心事は各ノードの枝の形成
type growerPipeline interface {
grow(context.Context, <-chan *Node) (<-chan *Node, <-chan error)
enableValidation()
}
// 関心事はtreeの出力
type spreaderPipeline interface {
spread(context.Context, io.Writer, <-chan *Node) <-chan error
}
// 関心事はファイルの生成
// interfaceを使う必要はないが、growerPipeline/spreaderPipelineと合わせたいため
type mkdirerPipeline interface {
mkdir(context.Context, <-chan *Node) <-chan error
}
// 関心事はディレクトリの検証
// interfaceを使う必要はないが、growerPipeline/spreaderPipelineと合わせたいため
type verifierPipeline interface {
verify(context.Context, <-chan *Node) <-chan error
}
// TODO: add doc
type walkerPipeline interface {
walk(context.Context, <-chan *Node, func(*WalkerNode) error) <-chan error
}
// パイプラインの全ステージで最初のエラーを返却
func (*treePipeline) handlePipelineErr(ctx context.Context, echs ...<-chan error) error {
eg, ectx := errgroup.WithContext(ctx)
for i := range echs {
i := i
eg.Go(func() error {
select {
case err, ok := <-echs[i]:
if !ok {
return nil
}
if err != nil {
return err
}
case <-ectx.Done():
return ectx.Err()
}
return nil
})
}
return eg.Wait()
}