Merge pull request #2056 from balena-io/scan-json

scan: Add '--json' option to help with scripting
This commit is contained in:
bulldozer-balena[bot] 2020-10-06 20:08:48 +00:00 committed by GitHub
commit 63d3402924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -1493,6 +1493,10 @@ display full info
scan timeout in seconds scan timeout in seconds
#### -j, --json
produce JSON output instead of tabular output
## ssh <applicationOrDevice> [service] ## ssh <applicationOrDevice> [service]
Start a shell on a local or remote device. If a service name is not provided, Start a shell on a local or remote device. If a service name is not provided,

View File

@ -22,6 +22,7 @@ import * as cf from '../utils/common-flags';
import { getVisuals, stripIndent } from '../utils/lazy'; import { getVisuals, stripIndent } from '../utils/lazy';
interface FlagsDef { interface FlagsDef {
json?: boolean;
verbose: boolean; verbose: boolean;
timeout?: number; timeout?: number;
help: void; help: void;
@ -53,6 +54,10 @@ export default class ScanCmd extends Command {
description: 'scan timeout in seconds', description: 'scan timeout in seconds',
}), }),
help: cf.help, help: cf.help,
json: flags.boolean({
char: 'j',
description: 'produce JSON output instead of tabular output',
}),
}; };
public static primary = true; public static primary = true;
@ -78,7 +83,7 @@ export default class ScanCmd extends Command {
const activeLocalDevices: LocalBalenaOsDevice[] = await new SpinnerPromise({ const activeLocalDevices: LocalBalenaOsDevice[] = await new SpinnerPromise({
promise: discover.discoverLocalBalenaOsDevices(discoverTimeout), promise: discover.discoverLocalBalenaOsDevices(discoverTimeout),
startMessage: 'Scanning for local balenaOS devices..', startMessage: 'Scanning for local balenaOS devices..',
stopMessage: 'Reporting scan results', stopMessage: options.json ? '' : 'Reporting scan results',
}).filter(async ({ address }: { address: string }) => { }).filter(async ({ address }: { address: string }) => {
const docker = dockerUtils.createClient({ const docker = dockerUtils.createClient({
host: address, host: address,
@ -137,7 +142,11 @@ export default class ScanCmd extends Command {
} }
// Output results // Output results
console.log(prettyjson.render(devicesInfo, { noColor: true })); console.log(
options.json
? JSON.stringify(devicesInfo, null, 4)
: prettyjson.render(devicesInfo, { noColor: true }),
);
} }
protected static dockerInfoProperties = [ protected static dockerInfoProperties = [