scan: Add '--json' option to help with scripting

Change-type: minor
This commit is contained in:
Paulo Castro 2020-10-06 18:04:43 +01:00
parent a14d89fe10
commit 8a506bc4c0
2 changed files with 15 additions and 2 deletions

View File

@ -1493,6 +1493,10 @@ display full info
scan timeout in seconds
#### -j, --json
produce JSON output instead of tabular output
## ssh <applicationOrDevice> [service]
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';
interface FlagsDef {
json?: boolean;
verbose: boolean;
timeout?: number;
help: void;
@ -53,6 +54,10 @@ export default class ScanCmd extends Command {
description: 'scan timeout in seconds',
}),
help: cf.help,
json: flags.boolean({
char: 'j',
description: 'produce JSON output instead of tabular output',
}),
};
public static primary = true;
@ -78,7 +83,7 @@ export default class ScanCmd extends Command {
const activeLocalDevices: LocalBalenaOsDevice[] = await new SpinnerPromise({
promise: discover.discoverLocalBalenaOsDevices(discoverTimeout),
startMessage: 'Scanning for local balenaOS devices..',
stopMessage: 'Reporting scan results',
stopMessage: options.json ? '' : 'Reporting scan results',
}).filter(async ({ address }: { address: string }) => {
const docker = dockerUtils.createClient({
host: address,
@ -137,7 +142,11 @@ export default class ScanCmd extends Command {
}
// 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 = [