Skip to content

Commit

Permalink
added otable formatter (#277)
Browse files Browse the repository at this point in the history
* added otable formatter

* make core options more consistent in grammar
  • Loading branch information
pirog authored Jul 15, 2024
1 parent 162da23 commit 5a09a57
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
### New Features

* Added `examples`, `positional`, and `usage` `yargs` parsing to incomings tasks
* Added new `oclif` style tabling option

### Internals

* Added `yargs` to task middleware
* Added `appConfig` to `lando` for more powerful tasks
* Added `primary` to `appConfig` for more powerful tasks
* Added `otable` to `formatData`
* Updated to `[email protected]` for better `--` handling

## v3.21.2 - [June 20, 2024](https://github.com/lando/cli/releases/tag/v3.21.2)
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ module.exports = class Cli {
*/
confirm(message = 'Are you sure?') {
return {
describe: 'Auto answer yes to prompts',
describe: 'Answers yes to prompts',
alias: ['y'],
default: false,
boolean: true,
Expand Down
27 changes: 22 additions & 5 deletions lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const util = require('util');
const formats = ['default', 'json', 'table'];
const formatOpts = {
format: {
describe: `Output in given format: ${formats.join(', ')}`,
describe: `Outputs in given format: ${formats.join(', ')}`,
choices: formats,
string: true,
},
path: {
describe: 'Only return the value at the given path',
describe: 'Returns the value at the given path',
default: null,
string: true,
},
filter: {
describe: 'Filter data by "key=value"',
describe: 'Filters data by "key=value"',
array: true,
},
};
Expand All @@ -40,7 +40,25 @@ exports.formatData = (data, {path = '', format = 'default', filter = []} = {}, o
switch (format) {
case 'json':
return JSON.stringify(data);
break;
case 'otable':
const ux = require('@oclif/core').ux;
// rows
const rows = require('../utils/get-object-keys')(data, {expandArrays: false}).map(key => ({key, value: _.get(data, key)}));
// columes
const columns = {key: {}, value: {get: row => require('../utils/prettify')(row.value)}};

// in order to keep this API consistent with return we need to hack console.log
// so we can get the table output in a string
let output = '';
const ogcl = console.log;
console.log = data => output += `${data}\n`;

// print table
ux.ux.table(_.sortBy(rows, 'key'), columns);
// restore
console.log = ogcl;
// return
return output;
case 'table':
const Table = require('./table');
if (!_.isArray(data)) {
Expand All @@ -52,7 +70,6 @@ exports.formatData = (data, {path = '', format = 'default', filter = []} = {}, o
.map(table => table.toString())
.thru(data => data.join(os.EOL))
.value();
break;
default:
return util.inspect(data, {
colors: process.stdout.isTTY,
Expand Down
22 changes: 22 additions & 0 deletions utils/get-object-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

/*
* Returns an array of all keys, nested or otherwise, as "." separated paths but does not expand arrays
* @TODO: implement depth? this is needed for upstream things like get-object-size?
*/
module.exports = (data, {prefix = '', expandArrays = true, separator = '.'} = {}) => {
return Object.keys(data).reduce((keys, key) => {
// if we have a primitive then return the path
if (!data[key] || typeof data[key] !== 'object' || Object.keys(data[key]).length === 0) {
return !key.includes(separator) ? [...keys, `${prefix}${key}`] : [...keys, `${prefix}["${key}"]`];
}

// if we arent expanding arrays then dont return paths with array indexes
if (!expandArrays && Array.isArray(data[key])) {
return [...keys, `${prefix}${key}`];
}

// otherwise cycle through again
return [...keys, ...module.exports(data[key], {expandArrays, prefix: `${prefix}${key}${separator}`})];
}, []);
};

0 comments on commit 5a09a57

Please sign in to comment.