chec / cli

A command line interface for using the Chec API
https://chec.io
BSD 3-Clause "New" or "Revised" License
9 stars 7 forks source link

Add `--file` argument to `request` command #20

Closed robbieaverill closed 4 years ago

robbieaverill commented 4 years ago

If I want to use chec request POST /v1/assets to upload an asset, the file contents must be a base64 encoded string (for local uploads anyway). This easily exceeds bash's limit for length, so we could add a --file argument (-f short) to specify a file to load and provide as the request body. Maybe it could be JSON only for now.

ScopeyNZ commented 4 years ago

stdin doesn't have native support in oclif but I played around a little and came up with this patch that works pretty well:

diff --git a/src/commands/request.js b/src/commands/request.js
index 0ff7e10..d9c4fed 100644
--- a/src/commands/request.js
+++ b/src/commands/request.js
@@ -41,6 +41,29 @@ class RequestCommand extends Command {
       this.error(jsonHelper.prettify(error.body))
     }
   }
+
+  getStdin() {
+    return new Promise(resolve => {
+      let data = ''
+
+      if (process.stdin.readableLength === 0) {
+        return resolve(undefined)
+      }
+
+      const handler = () => {
+        let chunk
+        while ((chunk = process.stdin.read()) !== null) {
+          data += chunk
+        }
+      }
+      process.stdin.on('readable', handler)
+
+      process.stdin.once('end', () => {
+        process.stdin.off('readable', handler)
+        resolve(data)
+      })
+    })
+  }
 }

 RequestCommand.description = `Run abstract API request

Might not make sense as a method though because you should only really use it at the start of the command before you interact with the user.

robbieaverill commented 4 years ago

The request command doesn't need to be interactive, if that helps

ScopeyNZ commented 4 years ago

Yeah, just was talking about semantics. Doesn't really matter too much. Anyway - I'll come back to this later, I don't want to distract myself with this yet 😅

robbieaverill commented 4 years ago

PR at #22