Skip to content

Commit 0342f53

Browse files
philnashdkundel
authored andcommitted
feat(twilio-run): adds json output to twilio-run list
1 parent d49d7e0 commit 0342f53

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

packages/twilio-run/src/commands/list.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function handler(
7171
try {
7272
const client = new TwilioServerlessApiClient(config);
7373
const result = await client.list({ ...config });
74-
printListResult(result, config);
74+
printListResult(result, config, config.outputFormat);
7575
} catch (err) {
7676
handleError(err);
7777
}
@@ -89,6 +89,7 @@ export const cliInfo: CliInfo = {
8989
'properties',
9090
'extended-output',
9191
'service-sid',
92+
'output-format',
9293
]),
9394
environment: {
9495
...ALL_FLAGS['environment'],

packages/twilio-run/src/config/list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type ListConfig = ApiListConfig & {
2626
cwd: string;
2727
properties?: string[];
2828
extendedOutput: boolean;
29+
outputFormat?: string;
2930
};
3031

3132
export type ConfigurableListCliFlags = Pick<
@@ -36,6 +37,7 @@ export type ConfigurableListCliFlags = Pick<
3637
| 'extendedOutput'
3738
| 'environment'
3839
| 'serviceSid'
40+
| 'outputFormat'
3941
>;
4042
export type ListCliFlags = Arguments<
4143
ConfigurableListCliFlags & {
@@ -91,6 +93,7 @@ export async function getConfigFromFlags(
9193
const types = flags.types.split(',').map(trim) as ListOptions[];
9294
const region = flags.region;
9395
const edge = flags.edge;
96+
const outputFormat = flags.outputFormat || externalCliOptions?.outputFormat;
9497

9598
return {
9699
cwd,
@@ -106,6 +109,7 @@ export async function getConfigFromFlags(
106109
types,
107110
region,
108111
edge,
112+
outputFormat,
109113
userAgentExtensions: getUserAgentExtensions('list', externalCliOptions),
110114
};
111115
}

packages/twilio-run/src/flags.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export const ALL_FLAGS = {
159159
type: 'string',
160160
alias: 'o',
161161
default: '',
162-
describe: 'Output the log in a different format',
162+
describe: 'Output the results in a different format',
163163
choices: ['', 'json'],
164164
} as Options,
165165
'log-cache-size': {
@@ -251,9 +251,9 @@ export const ALL_FLAGS = {
251251
export type AvailableFlags = typeof ALL_FLAGS;
252252
export type FlagNames = keyof AvailableFlags;
253253

254-
export function getRelevantFlags(
255-
flags: FlagNames[]
256-
): { [flagName: string]: Options } {
254+
export function getRelevantFlags(flags: FlagNames[]): {
255+
[flagName: string]: Options;
256+
} {
257257
return flags.reduce((current: { [flagName: string]: Options }, flagName) => {
258258
return { ...current, [flagName]: { ...ALL_FLAGS[flagName] } };
259259
}, {});

packages/twilio-run/src/printers/list.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import { stripIndent } from 'common-tags';
1414
import startCase from 'lodash.startcase';
1515
import logSymbols from 'log-symbols';
1616
import title from 'title';
17+
import { OutputFormat } from '../commands/shared';
1718
import { ListConfig } from '../config/list';
1819
import { logger } from '../utils/logger';
19-
import { writeOutput } from '../utils/output';
20+
import { writeJSONOutput, writeOutput } from '../utils/output';
2021
import { redactPartOfString, shouldPrettyPrint, windowSize } from './utils';
2122

2223
type KeyMaps = {
@@ -292,7 +293,7 @@ function prettyPrintSection<T extends ListOptions>(
292293
function printListResultTerminal(result: ListResult, config: ListConfig): void {
293294
const sections = Object.keys(result) as ListOptions[];
294295
const output = sections
295-
.map(section => prettyPrintSection(section, result[section]))
296+
.map((section) => prettyPrintSection(section, result[section]))
296297
.join(`\n\n${chalk.dim(LONG_LINE)}\n\n`);
297298

298299
let metaInfo = stripIndent(chalk`
@@ -301,8 +302,9 @@ function printListResultTerminal(result: ListResult, config: ListConfig): void {
301302
`);
302303

303304
if (config.serviceSid || config.serviceName) {
304-
metaInfo += chalk`\n{cyan.bold Service} ${config.serviceSid ||
305-
config.serviceName}`;
305+
metaInfo += chalk`\n{cyan.bold Service} ${
306+
config.serviceSid || config.serviceName
307+
}`;
306308
}
307309

308310
if (config.environment) {
@@ -313,7 +315,15 @@ function printListResultTerminal(result: ListResult, config: ListConfig): void {
313315
writeOutput(output);
314316
}
315317

316-
export function printListResult(result: ListResult, config: ListConfig): void {
318+
export function printListResult(
319+
result: ListResult,
320+
config: ListConfig,
321+
outputFormat: OutputFormat
322+
): void {
323+
if (outputFormat === 'json') {
324+
writeJSONOutput(result);
325+
return;
326+
}
317327
if (shouldPrettyPrint && !config.properties && !config.extendedOutput) {
318328
printListResultTerminal(result, config);
319329
} else {

0 commit comments

Comments
 (0)