Commit Graph

21 Commits

Author SHA1 Message Date
Brian McGee
56d8561125
feat: improve unmatched logic
Separates global excludes processing from `Formatter.Wants`. This removes redundant processing of global excludes in each `Formatter.Wants` call.

If a file has been globally excluded, we do not emit an `on-unmatched` log message. This should help reduce as reported in #317.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-06-14 10:31:47 +01:00
Paul Meyer
c07305e6b0 cli: remove global state, init function usage
Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
2024-06-06 20:28:37 +02:00
Brian McGee
089eb171a0
fix: --stdin flag
This was incorrectly ported from Rust to Go.

When `--stdin` is provided, `treefmt` copy the `stdin` into a temporary file, using the first path argument as the filename. This allows the user to control which formatters will match this temp file based on their `treefmt` config.

After the formatters have been applied, the contents of this temporary file are then printed to stdout and the temp file is removed.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-06-05 15:06:10 +01:00
Brian McGee
4f086c204e
Revert "fix: --stdin flag"
This reverts commit 9934a5764d.
2024-06-04 17:05:56 +01:00
Brian McGee
9934a5764d
fix: --stdin flag
This was incorrectly ported from Rust to Go.

When provided, `treefmt` will take the contents of stdin and place them into the file provided with the `--stdin` flag, then format it according to the configured formatters.

If the file doesn't exist it is created. If it exists, it is first truncated and then populated with stdin.

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-06-03 17:07:34 +01:00
zimbatm
bc89625cb9 fix TreeRootFile type
kong resolves path types to absolute paths
2024-05-30 19:44:17 +02:00
zimbatm
bc928bdf2b fixup! Update cli/cli.go 2024-05-30 19:31:31 +02:00
zimbatm
0511e9acf1 fixes https://github.com/numtide/treefmt/pull/309#discussion_r1620949820 2024-05-30 19:17:57 +02:00
Jonas Chevalier
088479eb27
Update cli/cli.go
Co-authored-by: Brian McGee <brian@bmcgee.ie>
2024-05-30 19:10:07 +02:00
zimbatm
bd32d36a33 cli: search for the tree root by default
Restore the treefmt 1.x behaviour where it would search for the tree
root by recursively searching for the treefmt.toml file up the
filesystem, starting from the current directory.

The `--tree-root-file` option will be useful to remove this bash wrapper: 2fba33a182/module-options.nix (L116-L135)

Fixes #308
2024-05-30 15:46:00 +02:00
Brian McGee
bbe50fbe75
doc: refine --on-unmatched usage
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-29 10:32:02 +01:00
Brian McGee
1b517c6502
feat: add --on-unmatched
By default, if a path does not match any formatter a log message at WARN level will be emitted. A user can change this by providing the `--on-unmatched` or `-u` flag and specifying a log level `debug,info,warn,error,fatal`.

If fatal, the process will exit with an error on the first unmatched path encountered.

Closes #302

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-29 10:32:02 +01:00
Brian McGee
4d07521d97
fix: -f short flag for specifying formatters
Preserves backwards compatibility with v1

Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-14 20:29:42 +01:00
Brian McGee
e5da10e873
doc: refine docs and usage
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-10 10:46:39 +01:00
Brian McGee
a79ae9583c
feat: add cpu profile flag 2024-05-06 16:05:10 +01:00
Brian McGee
fb9493884c
chore: refactor logging initialisation
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-05-02 10:31:25 +01:00
Brian McGee
2ad87c2504
feat: implement init
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-02-28 09:18:45 +00:00
Brian McGee
cb8565d683
fix: reduce log verbosity
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-02-15 13:59:56 +00:00
Brian McGee
da82b80f29
feat: support --no-cache
Signed-off-by: Brian McGee <brian@bmcgee.ie>

diff --git a/cli/cli.go b/cli/cli.go
index 8b23262..b370ee7 100644
--- a/cli/cli.go
+++ b/cli/cli.go
@@ -11,6 +11,7 @@ var Cli = Format{}
 type Format struct {
 	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"`
+	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"`
 	ConfigFile            string             `type:"existingfile" default:"./treefmt.toml"`
 	FailOnChange          bool               `help:"Exit with error if any changes were made. Useful for CI."`
diff --git a/cli/format.go b/cli/format.go
index 6c46096..14ac16c 100644
--- a/cli/format.go
+++ b/cli/format.go
@@ -5,6 +5,7 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"io/fs"
 	"os"
 	"os/signal"
 	"strings"
@@ -168,6 +169,20 @@ func (f *Format) Run() error {

 		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:
 		for {
 			select {
@@ -179,22 +194,17 @@ func (f *Format) Run() error {
 				}
 				batch = append(batch, path)
 				if len(batch) == batchSize {
-					count, err := cache.Update(batch)
-					if err != nil {
+					if err = processBatch(); err != nil {
 						return err
 					}
-					changes += count
-					batch = batch[:0]
 				}
 			}
 		}

 		// final flush
-		count, err := cache.Update(batch)
-		if err != nil {
+		if err = processBatch(); err != nil {
 			return err
 		}
-		changes += count

 		if Cli.FailOnChange && changes != 0 {
 			return ErrFailOnChange
@@ -251,6 +261,22 @@ func (f *Format) Run() error {
 		}

 		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)
 	})

diff --git a/cli/format_test.go b/cli/format_test.go
index fb389fe..2349767 100644
--- a/cli/format_test.go
+++ b/cli/format_test.go
@@ -216,6 +216,15 @@ func TestCache(t *testing.T) {
 	as.NoError(err)
 	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)
diff --git a/nix/packages.nix b/nix/packages.nix
index 127eb08..e0f8604 100644
--- a/nix/packages.nix
+++ b/nix/packages.nix
@@ -13,7 +13,7 @@
     packages = rec {
       treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec {
         pname = "treefmt";
-        version = "0.0.1+dev";
+        version = "2.0.0+dev";

         # ensure we are using the same version of go to build with
         inherit (pkgs) go;

diff --git a/cli/cli.go b/cli/cli.go
index 8b23262..b370ee7 100644
--- a/cli/cli.go
+++ b/cli/cli.go
@@ -11,6 +11,7 @@ var Cli = Format{}
 type Format struct {
 	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"`
+	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"`
 	ConfigFile            string             `type:"existingfile" default:"./treefmt.toml"`
 	FailOnChange          bool               `help:"Exit with error if any changes were made. Useful for CI."`
diff --git a/cli/format.go b/cli/format.go
index 6c46096..14ac16c 100644
--- a/cli/format.go
+++ b/cli/format.go
@@ -5,6 +5,7 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"io/fs"
 	"os"
 	"os/signal"
 	"strings"
@@ -168,6 +169,20 @@ func (f *Format) Run() error {

 		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:
 		for {
 			select {
@@ -179,22 +194,17 @@ func (f *Format) Run() error {
 				}
 				batch = append(batch, path)
 				if len(batch) == batchSize {
-					count, err := cache.Update(batch)
-					if err != nil {
+					if err = processBatch(); err != nil {
 						return err
 					}
-					changes += count
-					batch = batch[:0]
 				}
 			}
 		}

 		// final flush
-		count, err := cache.Update(batch)
-		if err != nil {
+		if err = processBatch(); err != nil {
 			return err
 		}
-		changes += count

 		if Cli.FailOnChange && changes != 0 {
 			return ErrFailOnChange
@@ -251,6 +261,22 @@ func (f *Format) Run() error {
 		}

 		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)
 	})

diff --git a/nix/packages.nix b/nix/packages.nix
index 127eb08..e0f8604 100644
--- a/nix/packages.nix
+++ b/nix/packages.nix
@@ -13,7 +13,7 @@
     packages = rec {
       treefmt = inputs'.gomod2nix.legacyPackages.buildGoApplication rec {
         pname = "treefmt";
-        version = "0.0.1+dev";
+        version = "2.0.0+dev";

         # ensure we are using the same version of go to build with
         inherit (pkgs) go;
2024-02-15 13:59:56 +00:00
Brian McGee
d53f98ea05
feat: support --version
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-02-15 13:59:56 +00:00
Brian McGee
d4ab015bc6
chore: remove internal directory
Signed-off-by: Brian McGee <brian@bmcgee.ie>
2024-02-15 13:59:55 +00:00