Skip to content

Commit 3f656f3

Browse files
committed
fix: properly check if stdin has data before using it
1 parent ff20b0c commit 3f656f3

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

cmd/client/cmd/root.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/gen2brain/beeep"
2020
"github.com/spf13/cobra"
2121
"github.com/spf13/viper"
22-
"golang.org/x/term"
2322
)
2423

2524
type uploadOpts struct {
@@ -71,7 +70,7 @@ cat main.go | clip -l go`,
7170
r = os.Stdin
7271
opts.source = "stdin"
7372
default:
74-
if !term.IsTerminal(int(os.Stdin.Fd())) {
73+
if stdinHasData() {
7574
r = os.Stdin
7675
opts.source = "stdin"
7776
} else {
@@ -215,6 +214,19 @@ func detectContentType(r io.Reader, filename string) (string, io.Reader, error)
215214
return contentType, io.MultiReader(bytes.NewReader(head), r), nil
216215
}
217216

217+
// stdinHasData reports whether stdin is a pipe or regular file (i.e., has data
218+
// to read), as opposed to a TTY or a null/empty fd from a non-interactive
219+
// launcher like a desktop shortcut.
220+
func stdinHasData() bool {
221+
fi, err := os.Stdin.Stat()
222+
if err != nil {
223+
return false
224+
}
225+
mode := fi.Mode()
226+
// named pipe (|) or regular file redirected in
227+
return mode&os.ModeNamedPipe != 0 || mode.IsRegular()
228+
}
229+
218230
func looksLikeText(data []byte) bool {
219231
for _, b := range data {
220232
if b == 0 {

0 commit comments

Comments
 (0)