Skip to content

Commit

Permalink
Add --read flag and revert v0.1.2 heuristics [BREAKING change]
Browse files Browse the repository at this point in the history
It turns out that calling FileInfo.Size() on a pipe is NOT a reliable
way to check if there is stdin data available. Because of that the
changed heuristic for detecting input in v0.1.2 broke scripts which rely
on piping data into gsheet.

Because it's probable that more people rely on the original behavior,
this release reverts the new v0.1.2 behavior. It also introduces the
`--read` flag to `csv` which forces gsheet to read data even if stdin is
not a tty. This is the recommended way to read sheets from scripts run
by cron.
  • Loading branch information
cristoper committed Jun 9, 2024
1 parent 099770c commit 4012da0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
6 changes: 6 additions & 0 deletions cmd/gsheet/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ var app = &cli.App{
Value: ",",
Usage: `Record separator (use '\t' for tab)`,
},
&cli.BoolFlag{
Name: "read",
Usage: "Force gsheet to read from range instead of write to range. This is useful if stdin is set to a non-character device such as when running a script from cron.",
Required: false,
Value: false,
},
},
},
{
Expand Down
14 changes: 2 additions & 12 deletions cmd/gsheet/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ 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 @@ -92,10 +84,8 @@ func rangeSheetAction(c *cli.Context) error {
}
sheetSvc.Sep = rune(sep[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) {
forceRead := c.Bool("read")
if forceRead || info.Mode()&os.ModeCharDevice != 0 {
// stdin is not connected to a pipe or file
// get data
vals, err := sheetSvc.GetRangeCSV(c.String("id"), c.String("range"))
Expand Down
1 change: 1 addition & 0 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Remember that for any of the commands to work you must have the GOOGLE_APPLICATI
The `csv` command is the heart of `gsheet`. If you pipe csv data to it on std input, it sends the data to the specified range of the Sheets document identified by the `--id` flag. If you pass the `--append` flag, data will be appended to the last row of data found in range.

If you don't connect stdin to a pipe, then it will read the specified range and output it to stdout in csv format.
To force `gsheet` to read a range even if stdin is not connected to a tty, you can pass the `--read` flag.

NOTE: `csv` does not clear the range before updating data in a Sheets document. If the piped data is smaller (fewer rows or columns) than the specified range, then any pre-existing data in the spreadsheet will remain after the update. Use `gsheet clear` to clear a range.

Expand Down

0 comments on commit 4012da0

Please sign in to comment.