Correct way to read a pipe, for long content?
# help
d
What is the correct way to read stdin? if I run something like this:
Copy code
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:
Copy code
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.
f
read
reads the chunks was they come in.
Typically the system flushes bytes when the buffer is full or when a newline is seen.
If you just want to get the output of a call to a command use
pipe.backticks
That will collect everything
You can also have a look there if you need to have a customized version of it. It uses
fork
under the hood iirc
d
ah thx, "backticks" is not a common word for me, so duh I didn't even look
f
We discussed renaming it...
I'm pretty sure a v2 of the package will have a different name