From 931be2d7dd0926875b5c58014b65399c9241f8f4 Mon Sep 17 00:00:00 2001 From: Gerald Mayr Date: Mon, 24 Oct 2022 22:59:42 +0200 Subject: [PATCH] Add -o|--output option to find command The output format option can be used to specify a different output format for the result. The default output format is `columns`. For better machine readability `json` and `yaml` have been added. --- README.md | 10 +++++++-- app/views/resources/index.js | 41 ++++++++++++++++++++++++++---------- passbolt-find.js | 3 ++- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 27c3008..b5fc3c5 100644 --- a/README.md +++ b/README.md @@ -202,9 +202,15 @@ free software foundation europe fsfe https://fsfe.org/index.en.h ftp user ftp://192.168.1.1 2016-05-15 16:04:49 4a2f98e8-b326-3384-aa2b-c3c9a81be3f7 ... ``` -Your can select the columns you want to display using the `--columns` arguments. -Non existing collumns will be ignored. +You can specify the output format by using the `-o` or `--output` argument and +set it to either `columns` (default), `json` or `yaml`. +``` +$ passbolt find --output json +``` + +You can select the columns you want to display using the `--columns` arguments. +Non existing columns will be ignored. ``` $ passbolt find --columns=name,uuid ``` diff --git a/app/views/resources/index.js b/app/views/resources/index.js index f23385f..aeb6f48 100644 --- a/app/views/resources/index.js +++ b/app/views/resources/index.js @@ -11,15 +11,22 @@ * @link https://www.passbolt.com Passbolt(tm) */ const AppView = require('../appView.js'); +const yaml = require('js-yaml'); /** * Resource Index View */ class ResourceIndexView extends AppView { - constructor(data, columns) { + constructor(data, output, columns) { super(); this.data = []; + this.defaultOutput = 'columns'; this.defaultColumns = ['name', 'username', 'uri', 'modified', 'uuid']; + if (output == null) { + this.output = this.defaultOutput; + } else { + this.output = output; + } if (Array.isArray(columns) && columns.length) { this.columns = columns.filter(value => this.defaultColumns.includes(value)); } else { @@ -44,16 +51,28 @@ class ResourceIndexView extends AppView { } render() { - if (this.data.length === 0) { - console.log('No resources to display. Create one first!'); - } else { - console.log(this.columnify(this.data, { - minWidth: 20, - columns: this.columns, - config: { - 'username': {maxWidth: 64} - } - })); + if (this.output === 'json') { + console.log(JSON.stringify(this.data, null, 2)); + } + else if (this.output === 'yaml') { + console.log(yaml.dump(this.data)); + } + else if (this.output === 'columns') { + if (this.data.length === 0) { + console.log('No resources to display. Create one first!'); + } + else { + console.log(this.columnify(this.data, { + minWidth: 20, + columns: this.columns, + config: { + 'username': {maxWidth: 64} + } + })); + } + } + else { + console.log('Output format is unknown.'); } } } diff --git a/passbolt-find.js b/passbolt-find.js index 2763b33..82784af 100755 --- a/passbolt-find.js +++ b/passbolt-find.js @@ -24,6 +24,7 @@ const {list} = require('./app/lib/coercion'); .usage('[options]', 'Search and list resources') .option('-u, --fingerprint ', 'The user key fingerprint to authenticate with') .option('-p, --passphrase ', 'The key passphrase') + .option('-o, --output ', 'The output format. One of: (columns,json,yaml)') .option('--columns ', 'Coma separated columns to display', list) .option('-v, --verbose', 'Display additional debug information') .parse(process.argv); @@ -32,7 +33,7 @@ const {list} = require('./app/lib/coercion'); await resourceController.loginIfNeeded(); try { let data = await resourceController.index(); - const view = new ResourceIndexView(data, program.opts().columns); + const view = new ResourceIndexView(data, program.opts().output, program.opts().columns); view.render(); } catch (err) { resourceController.error(err);