This repository has been archived on 2024-05-03. You can view files and clone it, but cannot push or open issues or pull requests.
treefmt/internal/cli/format.go

269 lines
5.7 KiB
Go
Raw Normal View History

2023-12-23 12:50:47 +00:00
package cli
import (
"bufio"
2023-12-23 12:50:47 +00:00
"context"
"errors"
2023-12-23 12:50:47 +00:00
"fmt"
"os"
"os/signal"
"strings"
"syscall"
2023-12-23 12:50:47 +00:00
"time"
"git.numtide.com/numtide/treefmt/internal/walk"
"git.numtide.com/numtide/treefmt/internal/config"
"git.numtide.com/numtide/treefmt/internal/cache"
"git.numtide.com/numtide/treefmt/internal/format"
2023-12-23 12:50:47 +00:00
"github.com/charmbracelet/log"
"golang.org/x/sync/errgroup"
)
var ErrFailOnChange = errors.New("unexpected changes detected, --fail-on-change is enabled")
2023-12-23 12:50:47 +00:00
func (f *Format) Run() error {
start := time.Now()
Cli.Configure()
2023-12-23 12:50:47 +00:00
l := log.WithPrefix("format")
defer func() {
if err := cache.Close(); err != nil {
l.Errorf("failed to close cache: %v", err)
}
}()
// create an overall context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// read config
cfg, err := config.ReadFile(Cli.ConfigFile)
2023-12-23 12:50:47 +00:00
if err != nil {
return fmt.Errorf("%w: failed to read config file", err)
2023-12-23 12:50:47 +00:00
}
globalExcludes, err := format.CompileGlobs(cfg.Global.Excludes)
// create optional formatter filter set
formatterSet := make(map[string]bool)
for _, name := range Cli.Formatters {
_, ok := cfg.Formatters[name]
if !ok {
return fmt.Errorf("%w: formatter not found in config: %v", err, name)
}
formatterSet[name] = true
}
includeFormatter := func(name string) bool {
if len(formatterSet) == 0 {
return true
} else {
_, include := formatterSet[name]
return include
}
}
formatters := make(map[string]*format.Formatter)
// detect broken dependencies
for name, formatterCfg := range cfg.Formatters {
before := formatterCfg.Before
if before != "" {
// check child formatter exists
_, ok := cfg.Formatters[before]
if !ok {
return fmt.Errorf("formatter %v is before %v but config for %v was not found", name, before, before)
}
}
}
// dependency cycle detection
for name, formatterCfg := range cfg.Formatters {
var ok bool
var history []string
childName := name
for {
// add to history
history = append(history, childName)
if formatterCfg.Before == "" {
break
} else if formatterCfg.Before == name {
return fmt.Errorf("formatter cycle detected %v", strings.Join(history, " -> "))
}
// load child config
childName = formatterCfg.Before
formatterCfg, ok = cfg.Formatters[formatterCfg.Before]
if !ok {
return fmt.Errorf("formatter not found: %v", formatterCfg.Before)
}
}
}
2023-12-23 12:50:47 +00:00
// init formatters
for name, formatterCfg := range cfg.Formatters {
if !includeFormatter(name) {
// remove this formatter
delete(cfg.Formatters, name)
l.Debugf("formatter %v is not in formatter list %v, skipping", name, Cli.Formatters)
continue
}
formatter, err := format.NewFormatter(name, formatterCfg, globalExcludes)
if errors.Is(err, format.ErrCommandNotFound) && Cli.AllowMissingFormatter {
l.Debugf("formatter not found: %v", name)
continue
} else if err != nil {
return fmt.Errorf("%w: failed to initialise formatter: %v", err, name)
2023-12-23 12:50:47 +00:00
}
formatters[name] = formatter
2023-12-23 12:50:47 +00:00
}
// iterate the initialised formatters configuring parent/child relationships
for _, formatter := range formatters {
if formatter.Before() != "" {
child, ok := formatters[formatter.Before()]
if !ok {
// formatter has been filtered out by the user
formatter.ResetBefore()
continue
}
formatter.SetChild(child)
child.SetParent(formatter)
}
}
2023-12-23 12:50:47 +00:00
if err = cache.Open(Cli.TreeRoot, Cli.ClearCache, formatters); err != nil {
2023-12-23 12:50:47 +00:00
return err
}
//
completedCh := make(chan string, 1024)
ctx = format.SetCompletedChannel(ctx, completedCh)
//
eg, ctx := errgroup.WithContext(ctx)
// start the formatters
for name := range formatters {
formatter := formatters[name]
2023-12-23 12:50:47 +00:00
eg.Go(func() error {
return formatter.Run(ctx)
})
}
// determine paths to be formatted
pathsCh := make(chan string, 1024)
// update cache as paths are completed
eg.Go(func() error {
batchSize := 1024
batch := make([]string, 0, batchSize)
2023-12-23 12:50:47 +00:00
var changes int
2023-12-23 12:50:47 +00:00
LOOP:
for {
select {
case <-ctx.Done():
return ctx.Err()
2023-12-23 12:50:47 +00:00
case path, ok := <-completedCh:
if !ok {
break LOOP
}
batch = append(batch, path)
if len(batch) == batchSize {
count, err := cache.Update(batch)
if err != nil {
2023-12-23 12:50:47 +00:00
return err
}
changes += count
2023-12-23 12:50:47 +00:00
batch = batch[:0]
}
}
}
// final flush
count, err := cache.Update(batch)
if err != nil {
2023-12-23 12:50:47 +00:00
return err
}
changes += count
2023-12-23 12:50:47 +00:00
if Cli.FailOnChange && changes != 0 {
return ErrFailOnChange
}
fmt.Printf("%v files changed in %v\n", changes, time.Now().Sub(start))
2023-12-23 12:50:47 +00:00
return nil
})
eg.Go(func() error {
// pass paths to each formatter
2023-12-23 12:50:47 +00:00
for path := range pathsCh {
for _, formatter := range formatters {
2023-12-23 12:50:47 +00:00
if formatter.Wants(path) {
formatter.Put(path)
}
}
}
// indicate no more paths for each formatter
for _, formatter := range formatters {
if formatter.Parent() != nil {
// this formatter is not a root, it will be closed by a parent
continue
}
2023-12-23 12:50:47 +00:00
formatter.Close()
}
// await completion
for _, formatter := range formatters {
formatter.AwaitCompletion()
2023-12-23 12:50:47 +00:00
}
// indicate no more completion events
close(completedCh)
2023-12-23 12:50:47 +00:00
return nil
})
eg.Go(func() (err error) {
paths := Cli.Paths
if len(paths) == 0 && Cli.Stdin {
// read in all the paths
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
paths = append(paths, scanner.Text())
}
}
walker, err := walk.New(Cli.Walk, Cli.TreeRoot, paths)
if err != nil {
return fmt.Errorf("%w: failed to create walker", err)
}
defer close(pathsCh)
return cache.ChangeSet(ctx, walker, pathsCh)
2023-12-23 12:50:47 +00:00
})
// listen for shutdown and call cancel if required
go func() {
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
<-exit
cancel()
}()
2023-12-23 12:50:47 +00:00
return eg.Wait()
}