Skip to content

Commit db19a1e

Browse files
Halehclaude
andcommitted
fix(cmd): show usage when run interactively with no arguments
Previously pgexplain silently blocked waiting for stdin when invoked directly in a terminal. Now it detects a TTY on stdin and prints usage with exit code 2 instead. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cae6061 commit db19a1e

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

cmd/pgexplain/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ func main() {
6565
}
6666

6767
// readInput returns the raw EXPLAIN JSON from stdin or the first file argument.
68+
// If no argument is given and stdin is an interactive terminal, it prints usage
69+
// and exits so the user is not left with a silently hanging process.
6870
func readInput(args []string) ([]byte, error) {
6971
if len(args) == 0 {
72+
if stdinIsTTY() {
73+
usage()
74+
os.Exit(2)
75+
}
7076
return io.ReadAll(os.Stdin)
7177
}
7278
f, err := os.Open(args[0])
@@ -77,6 +83,16 @@ func readInput(args []string) ([]byte, error) {
7783
return io.ReadAll(f)
7884
}
7985

86+
// stdinIsTTY reports whether stdin is connected to an interactive terminal
87+
// rather than a pipe or file redirect.
88+
func stdinIsTTY() bool {
89+
stat, err := os.Stdin.Stat()
90+
if err != nil {
91+
return false
92+
}
93+
return (stat.Mode() & os.ModeCharDevice) != 0
94+
}
95+
8096
// buildAdvisor creates an Advisor with all built-in rules at their defaults.
8197
func buildAdvisor() *advisor.Advisor {
8298
return advisor.New(

0 commit comments

Comments
 (0)