feat: support reading paths from stdin
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
878a4264c6
commit
1f55163b93
|
@ -20,6 +20,7 @@ type Format struct {
|
|||
Verbosity int `name:"verbose" short:"v" type:"counter" default:"0" env:"LOG_LEVEL" help:"Set the verbosity of logs e.g. -vv."`
|
||||
|
||||
Paths []string `name:"paths" arg:"" type:"path" optional:"" help:"Paths to format. Defaults to formatting the whole tree."`
|
||||
Stdin bool `help:"Format the context passed in via stdin"`
|
||||
}
|
||||
|
||||
func (f *Format) Configure() {
|
||||
|
|
|
@ -238,7 +238,9 @@ func (f *Format) Run() error {
|
|||
eg.Go(func() (err error) {
|
||||
var walker walk.Walker
|
||||
|
||||
if len(Cli.Paths) > 0 {
|
||||
if Cli.Stdin {
|
||||
walker, err = walk.NewPathReader(os.Stdin)
|
||||
} else if len(Cli.Paths) > 0 {
|
||||
walker, err = walk.NewPathList(Cli.Paths)
|
||||
} else {
|
||||
walker, err = walk.New(Cli.Walk, Cli.TreeRoot)
|
||||
|
|
|
@ -525,3 +525,59 @@ func TestPathsArg(t *testing.T) {
|
|||
out, err = cmd(t, "-C", tempDir, "-c", "elm/elm.json", "haskell/Nested/Bar.hs")
|
||||
as.ErrorContains(err, "no such file or directory")
|
||||
}
|
||||
|
||||
func TestStdIn(t *testing.T) {
|
||||
as := require.New(t)
|
||||
|
||||
// capture current cwd, so we can replace it after the test is finished
|
||||
cwd, err := os.Getwd()
|
||||
as.NoError(err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
// return to the previous working directory
|
||||
as.NoError(os.Chdir(cwd))
|
||||
})
|
||||
|
||||
tempDir := test.TempExamples(t)
|
||||
configPath := filepath.Join(tempDir, "/treefmt.toml")
|
||||
|
||||
// change working directory to temp root
|
||||
as.NoError(os.Chdir(tempDir))
|
||||
|
||||
// basic config
|
||||
cfg := config.Config{
|
||||
Formatters: map[string]*config.Formatter{
|
||||
"echo": {
|
||||
Command: "echo",
|
||||
Includes: []string{"*"},
|
||||
},
|
||||
},
|
||||
}
|
||||
test.WriteConfig(t, configPath, cfg)
|
||||
|
||||
// swap out stdin
|
||||
prevStdIn := os.Stdin
|
||||
stdin, err := os.CreateTemp("", "stdin")
|
||||
as.NoError(err)
|
||||
|
||||
os.Stdin = stdin
|
||||
|
||||
t.Cleanup(func() {
|
||||
os.Stdin = prevStdIn
|
||||
_ = os.Remove(stdin.Name())
|
||||
})
|
||||
|
||||
go func() {
|
||||
_, err := stdin.WriteString(`treefmt.toml
|
||||
elm/elm.json
|
||||
go/main.go
|
||||
`)
|
||||
as.NoError(err, "failed to write to stdin")
|
||||
as.NoError(stdin.Sync())
|
||||
_, _ = stdin.Seek(0, 0)
|
||||
}()
|
||||
|
||||
out, err := cmd(t, "-C", tempDir, "--stdin")
|
||||
as.NoError(err)
|
||||
as.Contains(string(out), fmt.Sprintf("%d files changed", 3))
|
||||
}
|
||||
|
|
Reference in New Issue
Block a user