Skip to content

Commit

Permalink
feat: have each formatter filter paths again if part of a pipeline
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Apr 25, 2024
1 parent 8af5b3c commit c71d690
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
21 changes: 0 additions & 21 deletions format/context.go

This file was deleted.

61 changes: 45 additions & 16 deletions format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,62 @@ type Formatter struct {
// internal compiled versions of Includes and Excludes.
includes []glob.Glob
excludes []glob.Glob

batch []string
}

// Executable returns the path to the executable defined by Command
func (f *Formatter) Executable() string {
return f.executable
}

func (f *Formatter) Apply(ctx context.Context, paths []string) error {
// only apply if the resultant batch is not empty
if len(paths) > 0 {
// construct args, starting with config
args := f.config.Options
func (f *Formatter) Apply(ctx context.Context, paths []string, filter bool) error {
// construct args, starting with config
args := f.config.Options

// If filter is true it indicates we are executing as part of a pipeline.
// In such a scenario each formatter must sub filter the paths provided as different formatters might want different
// files in a pipeline.
if filter {
// reset the batch
f.batch = f.batch[:]

// append each file path
// filter paths
for _, path := range paths {
args = append(args, path)
if f.Wants(path) {
f.batch = append(f.batch, path)
}
}

// execute
start := time.Now()
cmd := exec.CommandContext(ctx, f.config.Command, args...)
// exit early if nothing to process
if len(f.batch) == 0 {
return nil
}

if out, err := cmd.CombinedOutput(); err != nil {
f.log.Debugf("\n%v", string(out))
// todo log output
return err
// append paths to the args
args = append(args, f.batch...)
} else {
// exit early if nothing to process
if len(paths) == 0 {
return nil
}

f.log.Infof("%v files processed in %v", len(paths), time.Now().Sub(start))
// append paths to the args
args = append(args, paths...)
}

// execute the command
start := time.Now()
cmd := exec.CommandContext(ctx, f.config.Command, args...)

if out, err := cmd.CombinedOutput(); err != nil {
f.log.Debugf("\n%v", string(out))
// todo log output
return err
}

f.log.Infof("%v files processed in %v", len(paths), time.Now().Sub(start))

return nil
}

Expand Down Expand Up @@ -95,7 +120,11 @@ func NewFormatter(
f.executable = executable

// initialise internal state
f.log = log.WithPrefix("format | " + name)
if config.Pipeline == "" {
f.log = log.WithPrefix(fmt.Sprintf("format | %s", name))
} else {
f.log = log.WithPrefix(fmt.Sprintf("format | %s[%s]", config.Pipeline, name))
}

f.includes, err = CompileGlobs(config.Includes)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion format/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (p *Pipeline) Wants(path string) bool {

func (p *Pipeline) Apply(ctx context.Context, paths []string) error {
for _, f := range p.sequence {
if err := f.Apply(ctx, paths); err != nil {
if err := f.Apply(ctx, paths, len(p.sequence) > 1); err != nil {
return err
}
}
Expand Down

0 comments on commit c71d690

Please sign in to comment.