cristoper / gsheet

gsheet is a CLI tool (and Golang package) for piping csv data to and from Google Sheets
MIT License
83 stars 5 forks source link

Unable to pipe data from Google Sheet to stdout when running gsheet from crontab #7

Closed rleeden closed 5 months ago

rleeden commented 5 months ago

Not necessarily an issue with gsheet, but I'm struggling to find where the problem lies.

I'm trying to simply read a Google Sheet to stdout with the following simple shell script:

#!/bin/bash
export GOOGLE_APPLICATION_CREDENTIALS="/root/.config/gsheet/gsheet.json"

/usr/local/bin/gsheet csv --id "###########' --range 'Test Sheet' > /tmp/test.csv

This works fine when run from the command-line - i.e. I get the contents of my 'Test Sheet' in my test.csv file. When I run this same script from cron, I find 'Updated 0 cells' in my test.csv file.

This, to me, suggests that the gsheet command has run, but it thinks it's getting stdin so tries to update the Google sheet instead. I've tried all sorts of variations of the command, script and how I call it from cron with no luck. Is there any way to force gsheet to pipe the Google sheet contents to stdout, rather than the other way round? Or any other suggestions?

NOTE: This is on a Centos 7 machine, running bash 4.2.46, with gsheet v0.1.1

cristoper commented 5 months ago

Thanks for the report. When run from cron, stdin is not a tty, so gsheet was incorrectly expecting input. The fix in v0.1.2 improves the heuristics for detecting input: if stdin is a pipe but NOT if both stdin and stdout are pipes and stdin is empty (as when running from cron).

One corner case is if you're trying to write an empty string to a range from within a cron script, gsheet will try to read the range instead. In that case use gsheet clear (instead of gsheet csv)

I'm also open to adding a flag to force reading data instead of sending it if anyone runs into any more trouble with the input detection.

rleeden commented 5 months ago

That's great - just downloaded v0.1.2, tested, and it's solved the issue I was having. Thanks for the fix.

cristoper commented 3 months ago

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.

Since it's probable that more people rely on the original behavior, I've reverted this change in v0.1.4 and also introduced a --read flag to csv which forces gsheet to read data even if stdin is not a tty.

@rleeden this means that if you upgrade gsheet to v0.1.4 or later you will need to change your script to use the --read flag to force it to read instead of try to write! Sorry about that.

rleeden commented 3 months ago

@rleeden this means that if you upgrade gsheet to v0.1.4 or later you will need to change your script to use the --read flag to force it to read instead of try to write! Sorry about that.

Thanks for the heads-up. Easy fix to my script, and all working as expected after upgrading to v0.1.4