Skip to content

Commit

Permalink
Fix duplicate format option on slowlogs command (#1785)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxschmeling committed Apr 23, 2024
1 parent 7b8ddf6 commit 771caf7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
10 changes: 5 additions & 5 deletions __tests__/bin/vip-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe( 'getLogs', () => {
},
type: 'app',
limit: 500,
format: 'text',
format: 'table',
};
} );

Expand Down Expand Up @@ -81,7 +81,7 @@ describe( 'getLogs', () => {
type: 'app',
limit: 500,
follow: false,
format: 'text',
format: 'table',
};

expect( tracker.trackEvent ).toHaveBeenCalledTimes( 2 );
Expand Down Expand Up @@ -213,7 +213,7 @@ describe( 'getLogs', () => {
type: 'app',
limit: 500,
follow: false,
format: 'text',
format: 'table',
};

expect( tracker.trackEvent ).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -253,7 +253,7 @@ describe( 'getLogs', () => {
type: 'app',
limit: 500,
follow: false,
format: 'text',
format: 'table',
};

expect( tracker.trackEvent ).toHaveBeenCalledTimes( 2 );
Expand Down Expand Up @@ -296,7 +296,7 @@ describe( 'getLogs', () => {

expect( exit.withError ).toHaveBeenCalledTimes( 1 );
expect( exit.withError ).toHaveBeenCalledWith(
'Invalid format: jso. The supported formats are: csv, json, text.'
'Invalid format: jso. The supported formats are: csv, json, table.'
);

expect( logsLib.getRecentLogs ).not.toHaveBeenCalled();
Expand Down
10 changes: 5 additions & 5 deletions __tests__/bin/vip-slowlogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe( 'getSlowlogs', () => {
isK8sResident: true,
},
limit: 500,
format: 'text',
format: 'table',
};
} );

Expand Down Expand Up @@ -105,7 +105,7 @@ describe( 'getSlowlogs', () => {
env_id: 3,
limit: 500,
follow: false,
format: 'text',
format: 'table',
};

expect( tracker.trackEvent ).toHaveBeenCalledTimes( 2 );
Expand Down Expand Up @@ -270,7 +270,7 @@ describe( 'getSlowlogs', () => {
env_id: 3,
limit: 500,
follow: false,
format: 'text',
format: 'table',
};

expect( tracker.trackEvent ).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -309,7 +309,7 @@ describe( 'getSlowlogs', () => {
env_id: 3,
limit: 500,
follow: false,
format: 'text',
format: 'table',
};

expect( tracker.trackEvent ).toHaveBeenCalledTimes( 2 );
Expand All @@ -333,7 +333,7 @@ describe( 'getSlowlogs', () => {

expect( exit.withError ).toHaveBeenCalledTimes( 1 );
expect( exit.withError ).toHaveBeenCalledWith(
'Invalid format: jso. The supported formats are: csv, json, text.'
'Invalid format: jso. The supported formats are: csv, json, table.'
);

expect( slowlogsLib.getRecentSlowlogs ).not.toHaveBeenCalled();
Expand Down
6 changes: 3 additions & 3 deletions src/bin/vip-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { trackEvent } from '../lib/tracker';
const LIMIT_MIN = 1;
const LIMIT_MAX = 5000;
const ALLOWED_TYPES = [ 'app', 'batch' ];
const ALLOWED_FORMATS = [ 'csv', 'json', 'text' ];
const ALLOWED_FORMATS = [ 'csv', 'json', 'table' ];
const DEFAULT_POLLING_DELAY_IN_SECONDS = 30;
const MIN_POLLING_DELAY_IN_SECONDS = 5;
const MAX_POLLING_DELAY_IN_SECONDS = 300;
Expand Down Expand Up @@ -142,7 +142,7 @@ function printLogs( logs, format ) {
} );

let output = '';
if ( format && 'text' === format ) {
if ( format && 'table' === format ) {
const rows = [];
for ( const { timestamp, message } of logs ) {
rows.push( `${ timestamp } ${ message }` );
Expand Down Expand Up @@ -204,7 +204,7 @@ command( {
.option( 'type', 'The type of logs to be returned: "app" or "batch"', 'app' )
.option( 'limit', 'The maximum number of log lines', 500 )
.option( 'follow', 'Keep fetching new logs as they are generated' )
.option( 'format', 'Output the log lines in CSV or JSON format', 'text' )
.option( 'format', 'Output the log lines in CSV or JSON format', 'table' )
.examples( [
{
usage: 'vip @mysite.production logs',
Expand Down
8 changes: 4 additions & 4 deletions src/bin/vip-slowlogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import {
} from '../lib/app-slowlogs/types';
import command from '../lib/cli/command';
import * as exit from '../lib/cli/exit';
import { formatData } from '../lib/cli/format';
import { OutputFormat, formatData } from '../lib/cli/format';
import { trackEvent } from '../lib/tracker';

const LIMIT_MIN = 1;
const LIMIT_MAX = 500;
const ALLOWED_FORMATS = [ 'csv', 'json', 'text' ];
const ALLOWED_FORMATS: OutputFormat[] = [ 'csv', 'json', 'table' ];
const DEFAULT_POLLING_DELAY_IN_SECONDS = 30;
const MIN_POLLING_DELAY_IN_SECONDS = 5;
const MAX_POLLING_DELAY_IN_SECONDS = 300;
Expand Down Expand Up @@ -186,11 +186,11 @@ void command( {
appContext: true,
appQuery,
envContext: true,
format: true,
format: false,
module: 'slowlogs',
} )
.option( 'limit', 'The maximum number of log lines', 500 )
.option( 'format', 'Output the log lines in CSV or JSON format', 'text' )
.option( 'format', 'Output the log lines in CSV or JSON format', 'table' )
.examples( [
{
description: 'Get the most recent app slowlogs',
Expand Down
10 changes: 3 additions & 7 deletions src/lib/app-slowlogs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ export interface DefaultOptions {
};
}

export type SlowlogFormats = 'ids' | 'json' | 'csv';
export type SlowlogFormats = 'json' | 'csv' | 'table';

type Stringable =
| string
| {
toString: () => string;
};
type Stringable = string | { toString: () => string };

export interface GetSlowLogsOptions extends DefaultOptions {
limit: number;
format: 'ids' | 'json' | 'csv';
format: 'table' | 'json' | 'csv';
}

export interface GetRecentSlowlogsResponse {
Expand Down
12 changes: 5 additions & 7 deletions src/lib/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ export interface Tuple {
value: string;
}

type Stringable =
| string
| {
toString: () => string;
};
type Stringable = string | { toString: () => string };

export type OutputFormat = 'keyValue' | 'ids' | 'json' | 'csv' | 'table';

export function formatData( data: Tuple[], format: 'keyValue' ): string;
export function formatData( data: Record< string, Stringable >[], format: 'table' ): string;
export function formatData(
data: Record< string, unknown >[],
format: 'ids' | 'json' | 'csv'
): string;
export function formatData( data: Record< string, Stringable >[], format: OutputFormat ): string;
export function formatData(
data: Record< string, unknown >[] | Tuple[],
format: 'keyValue' | 'ids' | 'json' | 'csv' | 'table'
format: OutputFormat
): string {
if ( ! data.length ) {
return '';
Expand Down

0 comments on commit 771caf7

Please sign in to comment.