Skip to content

Commit

Permalink
Better detection of stdin input (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilotoole authored Dec 6, 2023
1 parent e7a28a7 commit 67a30d7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cli/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"context"
"os"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -157,16 +158,22 @@ func processFlagActiveSchema(cmd *cobra.Command, activeSrc *source.Source) error
// and returned. If the pipe has no data (size is zero),
// then (nil,nil) is returned.
func checkStdinSource(ctx context.Context, ru *run.Run) (*source.Source, error) {
log := lg.FromContext(ctx)
cmd := ru.Cmd

f := ru.Stdin
info, err := f.Stat()
fi, err := f.Stat()
if err != nil {
return nil, errz.Wrap(err, "failed to get stat on stdin")
}

if info.Size() <= 0 {
// Doesn't make sense to have zero-data pipe? just ignore.
switch {
case os.ModeNamedPipe&fi.Mode() > 0:
log.Info("Detected stdin pipe via os.ModeNamedPipe")
case fi.Size() > 0:
log.Info("Detected stdin redirect via size > 0", lga.Size, fi.Size())
default:
log.Info("No stdin data detected")
return nil, nil //nolint:nilnil
}

Expand Down
1 change: 1 addition & 0 deletions libsq/core/lg/lga/lga.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
Path = "path"
Pid = "pid"
Query = "query"
Size = "size"
SLQ = "slq"
SQL = "sql"
Src = "src"
Expand Down

0 comments on commit 67a30d7

Please sign in to comment.