mirror of
https://github.com/numtide/treefmt.git
synced 2024-10-26 09:19:18 +03:00
5a5c1ea03e
Signed-off-by: Brian McGee <brian@bmcgee.ie>
41 lines
740 B
Go
41 lines
740 B
Go
package format
|
|
|
|
import (
|
|
"context"
|
|
"slices"
|
|
|
|
"git.numtide.com/numtide/treefmt/walk"
|
|
)
|
|
|
|
type Pipeline struct {
|
|
sequence []*Formatter
|
|
}
|
|
|
|
func (p *Pipeline) Add(f *Formatter) {
|
|
p.sequence = append(p.sequence, f)
|
|
// sort by priority in ascending order
|
|
slices.SortFunc(p.sequence, func(a, b *Formatter) int {
|
|
return a.config.Priority - b.config.Priority
|
|
})
|
|
}
|
|
|
|
func (p *Pipeline) Wants(path *walk.File) bool {
|
|
var match bool
|
|
for _, f := range p.sequence {
|
|
match = f.Wants(path)
|
|
if match {
|
|
break
|
|
}
|
|
}
|
|
return match
|
|
}
|
|
|
|
func (p *Pipeline) Apply(ctx context.Context, paths []*walk.File) error {
|
|
for _, f := range p.sequence {
|
|
if err := f.Apply(ctx, paths, len(p.sequence) > 1); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|