feat(xo-cli rest get): support NDJSON responses
Fixes https://xcp-ng.org/forum/post/69326
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
> Users must be able to say: “Nice enhancement, I'm eager to test it”
|
||||
|
||||
- [Settings/Logs] Use GitHub issue form with pre-filled fields when reporting a bug [#7142](https://github.com/vatesfr/xen-orchestra/issues/7142) (PR [#7274](https://github.com/vatesfr/xen-orchestra/pull/7274))
|
||||
- [xo-cli] Supports NDJSON response for the `rest get` command (it also respects the `--json` flag) [Forum#69326](https://xcp-ng.org/forum/post/69326)
|
||||
|
||||
### Bug fixes
|
||||
|
||||
@@ -37,7 +38,7 @@
|
||||
|
||||
- @xen-orchestra/backups patch
|
||||
- @xen-orchestra/xapi patch
|
||||
- xo-cli patch
|
||||
- xo-cli minor
|
||||
- xo-server patch
|
||||
- xo-server-load-balancer patch
|
||||
- xo-web minor
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
"node": ">=15.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@vates/read-chunk": "^1.2.0",
|
||||
"chalk": "^5.0.1",
|
||||
"content-type": "^1.0.5",
|
||||
"fs-extra": "^11.1.0",
|
||||
@@ -41,6 +42,7 @@
|
||||
"progress-stream": "^2.0.0",
|
||||
"promise-toolbox": "^0.21.0",
|
||||
"pw": "^0.0.4",
|
||||
"split2": "^4.2.0",
|
||||
"xdg-basedir": "^5.1.0",
|
||||
"xo-lib": "^0.11.1"
|
||||
},
|
||||
|
||||
@@ -2,10 +2,13 @@ import { basename, join } from 'node:path'
|
||||
import { createWriteStream } from 'node:fs'
|
||||
import { normalize } from 'node:path/posix'
|
||||
import { parse as parseContentType } from 'content-type'
|
||||
import { pipeline } from 'node:stream/promises'
|
||||
import { pipeline } from 'node:stream'
|
||||
import { pipeline as pPipeline } from 'node:stream/promises'
|
||||
import { readChunk } from '@vates/read-chunk'
|
||||
import getopts from 'getopts'
|
||||
import hrp from 'http-request-plus'
|
||||
import merge from 'lodash/merge.js'
|
||||
import split2 from 'split2'
|
||||
|
||||
import * as config from './config.mjs'
|
||||
|
||||
@@ -19,6 +22,8 @@ function addPrefix(suffix) {
|
||||
return path
|
||||
}
|
||||
|
||||
const noop = Function.prototype
|
||||
|
||||
function parseParams(args) {
|
||||
const params = {}
|
||||
for (const arg of args) {
|
||||
@@ -60,7 +65,7 @@ const COMMANDS = {
|
||||
const response = await this.exec(path, { query: parseParams(rest) })
|
||||
|
||||
if (output !== '') {
|
||||
return pipeline(
|
||||
return pPipeline(
|
||||
response,
|
||||
output === '-'
|
||||
? process.stdout
|
||||
@@ -84,6 +89,13 @@ const COMMANDS = {
|
||||
}
|
||||
|
||||
return this.json ? JSON.stringify(result, null, 2) : result
|
||||
} else if (type === 'application/x-ndjson') {
|
||||
const lines = pipeline(response, split2(), noop)
|
||||
let line
|
||||
while ((line = await readChunk(lines)) !== null) {
|
||||
const data = JSON.parse(line)
|
||||
console.log(this.json ? JSON.stringify(data, null, 2) : data)
|
||||
}
|
||||
} else {
|
||||
throw new Error('unsupported content-type ' + type)
|
||||
}
|
||||
|
||||
@@ -19490,7 +19490,7 @@ split2@^3.0.0:
|
||||
dependencies:
|
||||
readable-stream "^3.0.0"
|
||||
|
||||
split2@^4.0.0, split2@^4.1.0:
|
||||
split2@^4.0.0, split2@^4.1.0, split2@^4.2.0:
|
||||
version "4.2.0"
|
||||
resolved "https://registry.yarnpkg.com/split2/-/split2-4.2.0.tgz#c9c5920904d148bab0b9f67145f245a86aadbfa4"
|
||||
integrity sha512-UcjcJOWknrNkF6PLX83qcHM6KHgVKNkV62Y8a5uYDVv9ydGQVwAHMKqHdJje1VTWpljG0WYpCDhrCdAOYH4TWg==
|
||||
|
||||
Reference in New Issue
Block a user