guild icon
Toit
#Correct way to read a pipe, for long content?
Thread channel in help
davidg238
davidg238 10/30/2023 06:55 PM
What is the correct way to read stdin?
if I run something like this:
import host.pipe show * import encoding.json show * INHERIT ::= PIPE-INHERITED invoke command args -> string: complete-args := [command] + args output := OpenPipe false stdout := output.fd pipes := fork true // use path INHERIT stdout INHERIT command complete-args pid := pipes[3] wait_for pid return output.read.to-string.trim main args: /* print "Version: $(invoke "artemis" ["version"])" print "Org: $(invoke "artemis" ["org", "list", "--output-format=json"])" print "Org default: $(invoke "artemis" ["org", "default", "--output-format=json"])" print "Org members: $(invoke "artemis" ["org", "members", "list", "--output-format=json"])" print "Default device: $(invoke "artemis" ["device", "default", "--fleet-root=../home", "--output-format=json"])" // print "Default device status: $(invoke "artemis" ["device", "show", "--fleet-root=../home", "--output-format=json"])" */ a-string := invoke "artemis" ["fleet", "status", "--fleet-root=../home", "--output-format=json"] print "String size $a-string.size" print a-string a-list := parse a-string
The commented invocations work, but the "fleet status" fails because the list of devices string is truncated, thus:
david@MSI-7D43:~/workspaceToit/artemis-facade$ jag run -d host test.toit String size 1520 <long truncated string goes here> EXCEPTION error. OUT_OF_BOUNDS 0: Decoder.handle-error_ <sdk>/encoding/json.toit:479:5 1: Decoder.decode-list_ <sdk>/encoding/json.toit:472:25 2: Decoder.decode_ <sdk>/encoding/json.toit:316:30 3: Decoder.decode <sdk>/encoding/json.toit:306:15 4: parse <sdk>/encoding/json.toit:97:12 5: main test.toit:33:13 Error: exit status 1
I only see a read method on the pipe.
floitsch
floitsch 10/30/2023 06:57 PM
read reads the chunks was they come in.
floitsch
floitsch 10/30/2023 06:57 PM
Typically the system flushes bytes when the buffer is full or when a newline is seen.
floitsch
floitsch 10/30/2023 06:58 PM
If you just want to get the output of a call to a command use pipe.backticks
floitsch
floitsch 10/30/2023 06:58 PM
That will collect everything
floitsch
floitsch 10/30/2023 06:59 PM
You can also have a look there if you need to have a customized version of it. It uses fork under the hood iirc
davidg238
davidg238 10/30/2023 07:03 PM
ah thx, "backticks" is not a common word for me, so duh I didn't even look
floitsch
floitsch 10/30/2023 07:04 PM
We discussed renaming it...
floitsch
floitsch 10/30/2023 07:04 PM
I'm pretty sure a v2 of the package will have a different name(edited)
9 messages in total