Return error when stdin provides no paths

When using "-" to read paths from stdin, if stdin is empty or contains
only blank lines, return an explicit error instead of silently succeeding
with no work done.
This commit is contained in:
Jeffrey Paul 2026-02-02 13:43:18 -08:00
parent d848c5e51b
commit 144d2de243

View File

@ -99,8 +99,10 @@ func main() {
// expandPaths expands the given paths, reading from stdin if "-" is present
func expandPaths(args []string) ([]string, error) {
var paths []string
readFromStdin := false
for _, arg := range args {
if arg == "-" {
readFromStdin = true
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
@ -115,6 +117,12 @@ func expandPaths(args []string) ([]string, error) {
paths = append(paths, arg)
}
}
if len(paths) == 0 {
if readFromStdin {
return nil, errors.New("no paths provided on stdin")
}
return nil, errors.New("no paths provided")
}
return paths, nil
}