Skip to content

Commit 47c5688

Browse files
committed
feat: introduce tmp path for stdin files
Signed-off-by: Brian McGee <[email protected]>
1 parent 3a3b57c commit 47c5688

File tree

8 files changed

+173
-126
lines changed

8 files changed

+173
-126
lines changed

cmd/root_test.go

Lines changed: 139 additions & 113 deletions
Large diffs are not rendered by default.

format/formatter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ func (f *Formatter) Apply(ctx context.Context, files []*walk.File) error {
9090

9191
// append paths to the args
9292
for _, file := range files {
93-
args = append(args, file.RelPath)
93+
if file.TmpPath != "" {
94+
args = append(args, file.TmpPath)
95+
} else {
96+
args = append(args, file.RelPath)
97+
}
9498
}
9599

96100
// execute the command

nix/packages/treefmt/formatters.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ with pkgs; [
1717
shfmt
1818
statix
1919
deadnix
20+
just
2021
opentofu
2122
dos2unix
2223
yamlfmt

test/examples/just/justfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# print this message
2+
help:
3+
just --list --list-submodules --unsorted
4+
5+
# interactive recipes launcher
6+
choose:
7+
just --choose --unsorted

test/examples/treefmt.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ includes = ["*.yaml"]
8989

9090
[formatter.foo-fmt]
9191
command = "foo-fmt"
92+
93+
[formatter.just]
94+
command = "just"
95+
options = ["--fmt", "--unstable", "--justfile"]
96+
includes = ["**/justfile"]

walk/cached.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (c *CachedReader) Read(ctx context.Context, files []*File) (n int, err erro
106106

107107
// set a release function which inserts this file into the update channel
108108
file.AddReleaseFunc(func(ctx context.Context) error {
109-
if !GetNoCache(ctx) {
109+
if !GetNoCache(ctx) && file.TmpPath == "" {
110110
c.updateCh <- file
111111
}
112112

walk/stdin.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"path"
910
"path/filepath"
1011

1112
"github.com/numtide/treefmt/v2/stats"
@@ -25,10 +26,11 @@ func (s StdinReader) Read(_ context.Context, files []*File) (n int, err error) {
2526
return 0, io.EOF
2627
}
2728

28-
// read stdin into a temporary file with the same file extension
29-
pattern := "*" + filepath.Ext(s.path)
29+
// we attempt to preserve any file extensions as some formatters will not behave correctly without it
30+
pattern := "treefmt-stdin-*" + filepath.Ext(s.path)
3031

31-
file, err := os.CreateTemp(s.root, pattern)
32+
// create a temporary file to dump stdin into
33+
file, err := os.CreateTemp("", pattern)
3234
if err != nil {
3335
return 0, fmt.Errorf("failed to create a temporary file for processing stdin: %w", err)
3436
}
@@ -43,14 +45,10 @@ func (s StdinReader) Read(_ context.Context, files []*File) (n int, err error) {
4345
return 0, fmt.Errorf("failed to get file info for temporary file: %w", err)
4446
}
4547

46-
relPath, err := filepath.Rel(s.root, file.Name())
47-
if err != nil {
48-
return 0, fmt.Errorf("failed to get relative path for temporary file: %w", err)
49-
}
50-
5148
files[0] = &File{
52-
Path: file.Name(),
53-
RelPath: relPath,
49+
Path: path.Join(s.root, s.path),
50+
RelPath: s.path,
51+
TmpPath: file.Name(),
5452
Info: info,
5553
}
5654

walk/walk.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type ReleaseFunc func(ctx context.Context) error
3636
type File struct {
3737
Path string
3838
RelPath string
39+
TmpPath string
3940
Info fs.FileInfo
4041

4142
// FormattedInfo is the result of os.stat after formatting the file.
@@ -111,8 +112,13 @@ func (f *File) AddReleaseFunc(fn ReleaseFunc) {
111112
// Stat checks if the file has changed by comparing its current state (size, mod time) to when it was first read.
112113
// It returns a boolean indicating if the file has changed, the current file info, and an error if any.
113114
func (f *File) Stat() (changed bool, info fs.FileInfo, err error) {
115+
p := f.Path
116+
if f.TmpPath != "" {
117+
p = f.TmpPath
118+
}
119+
114120
// Get the file's current state
115-
current, err := os.Stat(f.Path)
121+
current, err := os.Stat(p)
116122
if err != nil {
117123
return false, nil, fmt.Errorf("failed to stat %s: %w", f.Path, err)
118124
}

0 commit comments

Comments
 (0)