From c2a64d0bcffdb36e71fff264d6960e55d219b608 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Sun, 25 Apr 2021 16:15:03 +1000 Subject: [PATCH] feat(twilio-run:logs): adds production flag Fixes #208. --- .../twilio-run/__tests__/config/logs.test.ts | 40 +++++++++++++++++++ packages/twilio-run/src/commands/logs.ts | 10 ++++- packages/twilio-run/src/config/logs.ts | 5 +++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 packages/twilio-run/__tests__/config/logs.test.ts diff --git a/packages/twilio-run/__tests__/config/logs.test.ts b/packages/twilio-run/__tests__/config/logs.test.ts new file mode 100644 index 00000000..2937cf5b --- /dev/null +++ b/packages/twilio-run/__tests__/config/logs.test.ts @@ -0,0 +1,40 @@ +import { getConfigFromFlags, LogsCliFlags } from '../../src/config/logs'; + +const baseFlags = { + $0: 'twilio-run', + _: ['logs'], + serviceSid: 'ZS11112222111122221111222211112222', +} as LogsCliFlags; + +describe('getConfigFromFlags', () => { + test('should return a config', async () => { + const flags = { ...baseFlags }; + const config = await getConfigFromFlags(flags); + expect(config).toBeDefined(); + expect(config.serviceSid).toBe(baseFlags.serviceSid); + }); + + test('should set environment to "dev" by default', async () => { + const flags = { ...baseFlags }; + const config = await getConfigFromFlags(flags); + expect(config.environment).toBe('dev'); + }); + + test('should set environment with a flag', async () => { + const flags = { ...baseFlags, environment: 'stage' }; + const config = await getConfigFromFlags(flags); + expect(config.environment).toBe('stage'); + }); + + test('should set environment to empty string with "production" flag', async () => { + const flags = { ...baseFlags, production: true }; + const config = await getConfigFromFlags(flags); + expect(config.environment).toBe(''); + }); + + test('production overrides setting environment directly', async () => { + const flags = { ...baseFlags, production: true, environment: 'stage' }; + const config = await getConfigFromFlags(flags); + expect(config.environment).toBe(''); + }); +}); diff --git a/packages/twilio-run/src/commands/logs.ts b/packages/twilio-run/src/commands/logs.ts index efff82dc..8eb5fa85 100644 --- a/packages/twilio-run/src/commands/logs.ts +++ b/packages/twilio-run/src/commands/logs.ts @@ -94,12 +94,18 @@ export const cliInfo: CliInfo = { 'tail', 'output-format', 'log-cache-size', + 'production', ]), environment: { ...ALL_FLAGS['environment'], describe: 'The environment to retrieve the logs for', default: 'dev', }, + production: { + ...ALL_FLAGS['production'], + describe: + 'Retrieve logs for the production environment. Overrides the "environment" flag', + }, }, }; @@ -110,8 +116,8 @@ function optionBuilder(yargs: Argv): Argv { 'Prints the last 50 logs for the current project in the dev environment' ) .example( - '$0 logs --environment=production', - 'Prints the last 50 logs for the current project in the production environment' + '$0 logs --environment=stage', + 'Prints the last 50 logs for the current project in the stage environment' ) .example( '$0 logs --tail', diff --git a/packages/twilio-run/src/config/logs.ts b/packages/twilio-run/src/config/logs.ts index 23524efb..fd3dc335 100644 --- a/packages/twilio-run/src/config/logs.ts +++ b/packages/twilio-run/src/config/logs.ts @@ -35,6 +35,7 @@ export type ConfigurableLogsCliFlags = Pick< | 'tail' | 'outputFormat' | 'logCacheSize' + | 'production' >; export type LogsCliFlags = Arguments; @@ -60,6 +61,10 @@ export async function getConfigFromFlags( cwd = flags.cwd || cwd; environment = flags.environment || environment; + if (flags.production) { + environment = ''; + } + const { localEnv: envFileVars, envPath } = await readLocalEnvFile(flags); const { username, password } = await getCredentialsFromFlags( flags,