Skip to content

Commit

Permalink
Support raw flag for routes command. Support non-TTY usage. Closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy committed Jan 5, 2021
1 parent a72420a commit 6eba44e
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 53 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -95,6 +95,8 @@ Columns may be hidden or shown using the `-H` `--hide` and `-s` `--show` flags r

`method` `path` `id` `plugin` `vhost` `auth` `cors` `tags` `description`

The `-r` `--raw` flag will output a minimally formatted table with columns separated by tab characters. Non-TTY usage automatically defaults to raw output.

A summary of these options can be displayed with the `-h` `--help` flag.

#### `hpal run debug:repl`
Expand Down Expand Up @@ -158,7 +160,7 @@ The `-d` `--data` flag may be used to specify a request payload as a raw string.
The `-H` `--header` flag may be used to specify a request header in the format `header-name: header value`. This flag may be used multiple times to set multiple headers.
The `-r` `--raw` and `-v` `--verbose` flags affect the command's output, and may be used in tandem with each other or separately. The `-r` `--raw` flag ensures all output is unformatted, while the `-v` `--verbose` flag shows information about the request and response including timing, the request payload, request headers, response headers, status code, and response payload.
The `-r` `--raw` and `-v` `--verbose` flags affect the command's output, and may be used in tandem with each other or separately. The `-r` `--raw` flag ensures all output is unformatted, while the `-v` `--verbose` flag shows information about the request and response including timing, the request payload, request headers, response headers, status code, and response payload. Non-TTY usage automatically defaults to raw output.
A summary of these options can be displayed with the `-h` `--help` flag.
Expand Down
33 changes: 3 additions & 30 deletions lib/commands/curl.js
Expand Up @@ -4,7 +4,6 @@ const Util = require('util');
const Http = require('http');
const Querystring = require('querystring');
const Bossy = require('@hapi/bossy');
const CliTable = require('cli-table');
const WordWrap = require('word-wrap');
const Helpers = require('../helpers');

Expand Down Expand Up @@ -191,7 +190,7 @@ internals.parameters = (info, argv, ctx) => {
type: 'boolean',
alias: 'r',
description: 'Output only the unformatted response payload.',
default: null
default: !ctx.options.out.isTTY || null
}
}
);
Expand Down Expand Up @@ -375,7 +374,7 @@ internals.Display = class Display {

hr() {

const len = Math.round(this.width * (2 / 3));
const len = Math.round((this.width || 16) * (2 / 3));

return ('─').repeat(len);
}
Expand All @@ -400,7 +399,7 @@ internals.Display = class Display {
const leftColWidth = Math.max(...rows.map(([header]) => header.length)) + 2;
const rightColWidth = this.width - leftColWidth;

const table = new internals.InvisibleTable({
const table = new Helpers.InvisibleTable({
truncate: false,
colWidths: [leftColWidth, rightColWidth]
});
Expand All @@ -426,32 +425,6 @@ internals.Display = class Display {
}
};

internals.InvisibleTable = class InvisibleTable extends CliTable {
constructor(options) {

super({
chars: {
top: '',
'top-mid': '',
'top-left': '',
'top-right': '',
bottom: '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
left: '',
'left-mid': '',
mid: '',
'mid-mid': '',
right: '',
'right-mid': '',
middle: ''
},
...options
});
}
};

internals.getValidationDescription = (value) => {

return (value && typeof value.describe === 'function' && value.describe().keys) || {};
Expand Down
50 changes: 38 additions & 12 deletions lib/commands/routes.js
Expand Up @@ -34,7 +34,7 @@ module.exports = (server, argv, root, ctx) => {
const routes = server.table().filter((r) => !route || r.public === route);

const head = (col) => colors.bold(colors.yellow(col.name));
const colWidth = (col) => internals.colWidth(col.name, routes, (r) => col.get(r, server));
const colWidth = (col) => internals.colWidth(col.name, routes, (r) => col.get(r, server, parameters));
const adjustLastColumn = (widths) => {

const dividerWidths = widths.length + 1;
Expand Down Expand Up @@ -68,16 +68,27 @@ module.exports = (server, argv, root, ctx) => {

return order1 - order2;
})
.map((r) => displayColumns.map((col) => col.get(r, server)));
.map((r) => displayColumns.map((col) => col.get(r, server, parameters)));

const table = new CliTable({
const tableDisplay = {
head: displayColumns.map(head),
colWidths: adjustLastColumn(displayColumns.map(colWidth)),
style: {
head: [],
border: options.colors ? ['grey'] : []
}
});
};

const table = parameters.raw ?
new Helpers.InvisibleTable({
...tableDisplay,
chars: {
middle: '\t' // Delimeter allows cell content to contain spaces
}
}) :
new CliTable({
...tableDisplay,
colWidths: adjustLastColumn(displayColumns.map(colWidth))
});

table.push(...rows);

Expand Down Expand Up @@ -109,6 +120,12 @@ internals.parameters = (argv, ctx) => {
multiple: true,
description: 'Show specific columns. May be listed multiple times.',
valid: colNames
},
raw: {
type: 'boolean',
alias: 'r',
description: 'Output unformatted route table.',
default: !ctx.options.out.isTTY || null
}
};

Expand Down Expand Up @@ -181,23 +198,27 @@ internals.columns = [
{
name: 'auth',
display: false,
get: (r, srv) => {
get: (r, srv, params) => {

const auth = srv.auth.lookup(r);

if (!auth) {
return '(none)';
}

const mode = (auth.mode === 'required') ? '' : `(${auth.mode})\n`;
const mode = (auth.mode === 'required') ? '' : `(${auth.mode})`;

return mode + internals.listToString(auth.strategies);
if (mode) {
return mode + (params.raw ? ' ' : '\n') + internals.listToString(auth.strategies, params);
}

return internals.listToString(auth.strategies, params);
}
},
{
name: 'cors',
display: false,
get: (r) => {
get: (r, _, params) => {

if (!r.settings.cors) {
return '(off)';
Expand All @@ -207,13 +228,13 @@ internals.columns = [
return '(ignore)';
}

return internals.listToString(r.settings.cors.origin);
return internals.listToString(r.settings.cors.origin, params);
}
},
{
name: 'tags',
display: false,
get: (r) => internals.listToString(r.settings.tags)
get: (r, _, params) => internals.listToString(r.settings.tags, params)
},
{
name: 'description',
Expand All @@ -222,4 +243,9 @@ internals.columns = [
}
];

internals.listToString = (list) => [].concat(list || []).join('\n');
internals.listToString = (list, { raw }) => {

const delimeter = raw ? ', ' : '\n';

return [].concat(list || []).join(delimeter);
};
29 changes: 29 additions & 0 deletions lib/helpers.js
@@ -1,6 +1,7 @@
'use strict';

const Url = require('url');
const CliTable = require('cli-table');

const internals = {};

Expand Down Expand Up @@ -60,3 +61,31 @@ internals.isMethod = (str) => {

return ['get', 'post', 'patch', 'put', 'delete', 'options', 'head'].includes(str.toLowerCase());
};

exports.InvisibleTable = class InvisibleTable extends CliTable {
constructor(options = {}) {

super({
...options,
style: { 'padding-left': 0, 'padding-right': 0, ...options.style },
chars: {
top: '',
'top-mid': '',
'top-left': '',
'top-right': '',
bottom: '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
left: '',
'left-mid': '',
mid: '',
'mid-mid': '',
right: '',
'right-mid': '',
middle: '',
...options.chars
}
});
}
};

0 comments on commit 6eba44e

Please sign in to comment.