Skip to content

Commit

Permalink
Better detection of interactive stdin
Browse files Browse the repository at this point in the history
When run from cron, stdin is not a tty so gsheet would always expect
input making it impossible to read data. This change makes it so gsheet
only expects input when stdin is a tty OR if stdin and stdout are both
pipes and there is no data in stdin.

Should fix #7
  • Loading branch information
cristoper committed Apr 24, 2024
1 parent 6cd9a7a commit f1fa7d8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmd/gsheet/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func rangeSheetAction(c *cli.Context) error {
return err
}

outInfo, err := os.Stdout.Stat()
if err != nil {
return err
}

inTty := info.Mode()&os.ModeCharDevice > 0
outTty := outInfo.Mode()&os.ModeCharDevice > 0

// Set sep based on --sep flag, parsing escape sequences like \t into a rune
sep, err := strconv.Unquote(`"` + c.String("sep") + `"`)
if err != nil || len(sep) == 0 {
Expand All @@ -45,7 +53,10 @@ func rangeSheetAction(c *cli.Context) error {
}
sheetSvc.Sep = rune(sep[0])

if info.Mode()&os.ModeCharDevice > 0 {
// if stdin is connected to a tty
// or if neither stdin nor stdout are connected to a tty and there is no
// stdin data to read (like when run from cron)
if inTty || (!inTty && !outTty && info.Size() == 0) {
// stdin is not connected to a pipe or file
// get data
vals, err := sheetSvc.GetRangeCSV(c.String("id"), c.String("range"))
Expand Down

0 comments on commit f1fa7d8

Please sign in to comment.