feat: support --no-cache
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
a8488986a7
commit
72bd5960a7
@ -11,6 +11,7 @@ var Cli = Format{}
|
|||||||
type Format struct {
|
type Format struct {
|
||||||
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
|
AllowMissingFormatter bool `default:"false" help:"Do not exit with error if a configured formatter is missing"`
|
||||||
WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"`
|
WorkingDirectory kong.ChangeDirFlag `default:"." short:"C" help:"Run as if treefmt was started in the specified working directory instead of the current working directory"`
|
||||||
|
NoCache bool `help:"Ignore the evaluation cache entirely. Useful for CI"`
|
||||||
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
|
ClearCache bool `short:"c" help:"Reset the evaluation cache. Use in case the cache is not precise enough"`
|
||||||
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
|
ConfigFile string `type:"existingfile" default:"./treefmt.toml"`
|
||||||
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."`
|
FailOnChange bool `help:"Exit with error if any changes were made. Useful for CI."`
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
@ -168,6 +169,20 @@ func (f *Format) Run() error {
|
|||||||
|
|
||||||
var changes int
|
var changes int
|
||||||
|
|
||||||
|
processBatch := func() error {
|
||||||
|
if Cli.NoCache {
|
||||||
|
changes += len(batch)
|
||||||
|
} else {
|
||||||
|
count, err := cache.Update(batch)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
changes += count
|
||||||
|
}
|
||||||
|
batch = batch[:0]
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
LOOP:
|
LOOP:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
@ -179,22 +194,17 @@ func (f *Format) Run() error {
|
|||||||
}
|
}
|
||||||
batch = append(batch, path)
|
batch = append(batch, path)
|
||||||
if len(batch) == batchSize {
|
if len(batch) == batchSize {
|
||||||
count, err := cache.Update(batch)
|
if err = processBatch(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
changes += count
|
|
||||||
batch = batch[:0]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// final flush
|
// final flush
|
||||||
count, err := cache.Update(batch)
|
if err = processBatch(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
changes += count
|
|
||||||
|
|
||||||
if Cli.FailOnChange && changes != 0 {
|
if Cli.FailOnChange && changes != 0 {
|
||||||
return ErrFailOnChange
|
return ErrFailOnChange
|
||||||
@ -251,6 +261,22 @@ func (f *Format) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer close(pathsCh)
|
defer close(pathsCh)
|
||||||
|
|
||||||
|
if Cli.NoCache {
|
||||||
|
return walker.Walk(ctx, func(path string, info fs.FileInfo, err error) error {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
default:
|
||||||
|
// ignore symlinks and directories
|
||||||
|
if !(info.IsDir() || info.Mode()&os.ModeSymlink == os.ModeSymlink) {
|
||||||
|
pathsCh <- path
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return cache.ChangeSet(ctx, walker, pathsCh)
|
return cache.ChangeSet(ctx, walker, pathsCh)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -206,6 +206,20 @@ func TestCache(t *testing.T) {
|
|||||||
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
||||||
as.NoError(err)
|
as.NoError(err)
|
||||||
as.Contains(string(out), "0 files changed")
|
as.Contains(string(out), "0 files changed")
|
||||||
|
|
||||||
|
// clear cache
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "-c")
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
|
||||||
|
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir)
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), "0 files changed")
|
||||||
|
|
||||||
|
// no cache
|
||||||
|
out, err = cmd(t, "--config-file", configPath, "--tree-root", tempDir, "--no-cache")
|
||||||
|
as.NoError(err)
|
||||||
|
as.Contains(string(out), fmt.Sprintf("%d files changed", 29))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestChangeWorkingDirectory(t *testing.T) {
|
func TestChangeWorkingDirectory(t *testing.T) {
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
packages = rec {
|
packages = rec {
|
||||||
treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec {
|
treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec {
|
||||||
pname = "treefmt";
|
pname = "treefmt";
|
||||||
version = "0.0.1+dev";
|
version = "2.0.0+dev";
|
||||||
|
|
||||||
# ensure we are using the same version of go to build with
|
# ensure we are using the same version of go to build with
|
||||||
inherit (pkgs) go;
|
inherit (pkgs) go;
|
||||||
|
Reference in New Issue
Block a user