Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@ func (f *Formatter) Apply(ctx context.Context, files []*walk.File) error {

// append paths to the args
for _, file := range files {
args = append(args, file.RelPath)
// start with the path override, fallback to the normal path
path := file.PathToFormat
if path == "" {
path = file.RelPath
}

// sanity check that a path has been set
if path == "" {
return fmt.Errorf("file has no path: %v", file)
}

args = append(args, path)
}

// execute the command
Expand Down
11 changes: 6 additions & 5 deletions walk/stdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ func (s StdinReader) Read(_ context.Context, files []*File) (n int, err error) {
return 0, fmt.Errorf("failed to get file info for temporary file: %w", err)
}

relPath, err := filepath.Rel(s.root, file.Name())
relPath, err := filepath.Rel(s.root, s.path)
if err != nil {
return 0, fmt.Errorf("failed to get relative path for temporary file: %w", err)
return 0, fmt.Errorf("failed to get relative path for file: %w", err)
}

files[0] = &File{
Path: file.Name(),
RelPath: relPath,
Info: info,
Path: s.path,
RelPath: relPath,
Info: info,
PathToFormat: file.Name(),
}

// dump the temp file to stdout and remove it once the file is finished being processed
Expand Down
3 changes: 3 additions & 0 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type File struct {
RelPath string
Info fs.FileInfo

// PathToFormat is an optional override for Path, allowing us to match against Path but format PathToFormat.
PathToFormat string

// FormattedInfo is the result of os.stat after formatting the file.
FormattedInfo fs.FileInfo

Expand Down
Loading