2023-12-23 12:50:47 +00:00
|
|
|
package format
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-02 10:33:50 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-04-25 11:16:04 +00:00
|
|
|
"os"
|
2023-12-23 12:50:47 +00:00
|
|
|
"os/exec"
|
|
|
|
"time"
|
|
|
|
|
2024-02-15 09:20:01 +00:00
|
|
|
"git.numtide.com/numtide/treefmt/config"
|
2024-01-15 10:46:49 +00:00
|
|
|
|
2023-12-23 12:50:47 +00:00
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
"github.com/gobwas/glob"
|
|
|
|
)
|
|
|
|
|
2024-01-15 10:46:49 +00:00
|
|
|
// ErrCommandNotFound is returned when the Command for a Formatter is not available.
|
|
|
|
var ErrCommandNotFound = errors.New("formatter command not found in PATH")
|
2024-01-12 11:46:04 +00:00
|
|
|
|
|
|
|
// Formatter represents a command which should be applied to a filesystem.
|
|
|
|
type Formatter struct {
|
|
|
|
name string
|
2024-01-15 10:46:49 +00:00
|
|
|
config *config.Formatter
|
2023-12-23 12:50:47 +00:00
|
|
|
|
2023-12-26 11:01:35 +00:00
|
|
|
log *log.Logger
|
|
|
|
executable string // path to the executable described by Command
|
2024-04-25 11:16:04 +00:00
|
|
|
workingDir string
|
2023-12-23 12:50:47 +00:00
|
|
|
|
2023-12-24 11:59:05 +00:00
|
|
|
// internal compiled versions of Includes and Excludes.
|
2023-12-23 12:50:47 +00:00
|
|
|
includes []glob.Glob
|
|
|
|
excludes []glob.Glob
|
2024-04-25 08:17:51 +00:00
|
|
|
|
|
|
|
batch []string
|
2023-12-23 12:50:47 +00:00
|
|
|
}
|
|
|
|
|
2024-04-19 09:57:41 +00:00
|
|
|
// Executable returns the path to the executable defined by Command
|
|
|
|
func (f *Formatter) Executable() string {
|
|
|
|
return f.executable
|
2024-01-12 11:46:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 08:17:51 +00:00
|
|
|
func (f *Formatter) Apply(ctx context.Context, paths []string, filter bool) error {
|
2024-04-25 11:16:04 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2024-04-25 08:17:51 +00:00
|
|
|
// 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
|
2024-04-25 11:16:04 +00:00
|
|
|
f.batch = f.batch[:0]
|
2024-04-19 09:57:41 +00:00
|
|
|
|
2024-04-25 08:17:51 +00:00
|
|
|
// filter paths
|
2024-04-19 09:57:41 +00:00
|
|
|
for _, path := range paths {
|
2024-04-25 08:17:51 +00:00
|
|
|
if f.Wants(path) {
|
|
|
|
f.batch = append(f.batch, path)
|
|
|
|
}
|
2024-04-19 09:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 08:17:51 +00:00
|
|
|
// exit early if nothing to process
|
|
|
|
if len(f.batch) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2024-04-19 09:57:41 +00:00
|
|
|
|
2024-04-25 08:17:51 +00:00
|
|
|
// append paths to the args
|
|
|
|
args = append(args, f.batch...)
|
|
|
|
} else {
|
|
|
|
// exit early if nothing to process
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return nil
|
2024-04-19 09:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 08:17:51 +00:00
|
|
|
// append paths to the args
|
|
|
|
args = append(args, paths...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute the command
|
|
|
|
cmd := exec.CommandContext(ctx, f.config.Command, args...)
|
2024-04-25 11:16:04 +00:00
|
|
|
cmd.Dir = f.workingDir
|
2024-04-25 08:17:51 +00:00
|
|
|
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
2024-04-25 11:16:04 +00:00
|
|
|
if len(out) > 0 {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s error:\n%s\n", f.name, out)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%w: formatter %s failed to apply", err, f.name)
|
2024-04-19 09:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-25 11:16:04 +00:00
|
|
|
//
|
|
|
|
|
2024-04-25 08:17:51 +00:00
|
|
|
f.log.Infof("%v files processed in %v", len(paths), time.Now().Sub(start))
|
|
|
|
|
2024-04-19 09:57:41 +00:00
|
|
|
return nil
|
2024-01-12 11:46:04 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 09:27:11 +00:00
|
|
|
// Wants is used to test if a Formatter wants a path based on it's configured Includes and Excludes patterns.
|
2024-04-19 09:57:41 +00:00
|
|
|
// Returns true if the Formatter should be applied to path, false otherwise.
|
|
|
|
func (f *Formatter) Wants(path string) bool {
|
|
|
|
match := !PathMatches(path, f.excludes) && PathMatches(path, f.includes)
|
|
|
|
if match {
|
|
|
|
f.log.Debugf("match: %v", path)
|
|
|
|
}
|
|
|
|
return match
|
2023-12-26 11:01:35 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 11:46:04 +00:00
|
|
|
// NewFormatter is used to create a new Formatter.
|
2024-04-19 09:57:41 +00:00
|
|
|
func NewFormatter(
|
|
|
|
name string,
|
2024-04-25 11:16:04 +00:00
|
|
|
treeRoot string,
|
2024-04-19 09:57:41 +00:00
|
|
|
config *config.Formatter,
|
|
|
|
globalExcludes []glob.Glob,
|
|
|
|
) (*Formatter, error) {
|
2024-01-02 12:12:47 +00:00
|
|
|
var err error
|
|
|
|
|
2024-01-12 11:46:04 +00:00
|
|
|
f := Formatter{}
|
2024-04-19 09:57:41 +00:00
|
|
|
|
|
|
|
// capture config and the formatter's name
|
2023-12-24 11:59:05 +00:00
|
|
|
f.name = name
|
2024-01-12 11:46:04 +00:00
|
|
|
f.config = config
|
2024-04-25 11:16:04 +00:00
|
|
|
f.workingDir = treeRoot
|
2023-12-23 12:50:47 +00:00
|
|
|
|
2023-12-23 15:00:39 +00:00
|
|
|
// test if the formatter is available
|
2024-01-12 11:46:04 +00:00
|
|
|
executable, err := exec.LookPath(config.Command)
|
2023-12-26 11:01:35 +00:00
|
|
|
if errors.Is(err, exec.ErrNotFound) {
|
2024-01-15 10:46:49 +00:00
|
|
|
return nil, ErrCommandNotFound
|
2023-12-26 11:01:35 +00:00
|
|
|
} else if err != nil {
|
2024-01-12 11:46:04 +00:00
|
|
|
return nil, err
|
2023-12-23 15:00:39 +00:00
|
|
|
}
|
2023-12-26 11:01:35 +00:00
|
|
|
f.executable = executable
|
2023-12-23 15:00:39 +00:00
|
|
|
|
2023-12-24 11:59:05 +00:00
|
|
|
// initialise internal state
|
2024-04-25 08:17:51 +00:00
|
|
|
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))
|
|
|
|
}
|
2023-12-23 12:50:47 +00:00
|
|
|
|
2024-01-12 11:46:04 +00:00
|
|
|
f.includes, err = CompileGlobs(config.Includes)
|
2024-01-02 12:12:47 +00:00
|
|
|
if err != nil {
|
2024-01-12 11:46:04 +00:00
|
|
|
return nil, fmt.Errorf("%w: formatter '%v' includes", err, f.name)
|
2023-12-23 12:50:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 11:46:04 +00:00
|
|
|
f.excludes, err = CompileGlobs(config.Excludes)
|
2024-01-02 12:12:47 +00:00
|
|
|
if err != nil {
|
2024-01-12 11:46:04 +00:00
|
|
|
return nil, fmt.Errorf("%w: formatter '%v' excludes", err, f.name)
|
2023-12-23 12:50:47 +00:00
|
|
|
}
|
2024-01-02 12:12:47 +00:00
|
|
|
f.excludes = append(f.excludes, globalExcludes...)
|
2023-12-23 12:50:47 +00:00
|
|
|
|
2024-01-12 11:46:04 +00:00
|
|
|
return &f, nil
|
|
|
|
}
|