Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update thevip logs command to follow the VIP-CLI style guide #2089

Merged
merged 21 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/bin/vip-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ describe( 'getLogs', () => {

expect( exit.withError ).toHaveBeenCalledTimes( 1 );
expect( exit.withError ).toHaveBeenCalledWith(
`Invalid limit: ${ limit }. It should be a number between 1 and 5000.`
`Invalid limit: ${ limit }. Set the limit to an integer between 1 and 5000.`
);

expect( logsLib.getRecentLogs ).not.toHaveBeenCalled();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/bin/vip-slowlogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ describe( 'getSlowlogs', () => {

expect( exit.withError ).toHaveBeenCalledTimes( 1 );
expect( exit.withError ).toHaveBeenCalledWith(
`Invalid limit: ${ limit }. It should be a number between 1 and 5000.`
`Invalid limit: ${ limit }. Set the limit to an integer between 1 and 5000.`
);

expect( slowlogsLib.getRecentSlowlogs ).not.toHaveBeenCalled();
Expand Down
48 changes: 27 additions & 21 deletions src/bin/vip-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ const MAX_POLLING_DELAY_IN_SECONDS = 300;
* @param {string[]} arg
*/
export async function getLogs( arg, opt ) {
opt.type ??= 'app';
opt.limit ??= LIMIT_DEFAULT;
opt.format ??= 'table';

validateInputs( opt.type, opt.limit, opt.format );

const trackingParams = getBaseTrackingParams( opt );
Expand Down Expand Up @@ -188,10 +192,6 @@ function printLogs( logs, format ) {
* @param {string} format
*/
export function validateInputs( type, limit, format ) {
if ( limit === undefined ) {
limit = LIMIT_DEFAULT;
}

if ( ! ALLOWED_TYPES.includes( type ) ) {
exit.withError(
`Invalid type: ${ type }. The supported types are: ${ ALLOWED_TYPES.join( ', ' ) }.`
Expand All @@ -206,7 +206,7 @@ export function validateInputs( type, limit, format ) {

if ( ! Number.isInteger( limit ) || limit < LIMIT_MIN || limit > logsLib.LIMIT_MAX ) {
exit.withError(
`Invalid limit: ${ limit }. It should be a number between ${ LIMIT_MIN } and ${ logsLib.LIMIT_MAX }.`
`Invalid limit: ${ limit }. Set the limit to an integer between ${ LIMIT_MIN } and ${ logsLib.LIMIT_MAX }.`
);
}
}
Expand All @@ -232,31 +232,37 @@ command( {
envContext: true,
module: 'logs',
} )
.option( 'type', 'The type of logs to be returned: "app" or "batch"', 'app' )
.option(
'type',
'Specify the type of Runtime Logs to retrieve. Accepts "batch" (only valid for WordPress environments).',
'app'
)
// The default limit is set manually in the validateInputs function to address validation issues, avoiding incorrect replacement of the default value.
.option( 'limit', `The maximum number of log lines (defaults to ${ LIMIT_DEFAULT })` )
.option( 'follow', 'Keep fetching new logs as they are generated' )
.option( 'format', 'Output the log lines in CSV or JSON format', 'table' )
.option(
'limit',
`The maximum number of entries to return. Accepts an integer value between 1 and 5000 (defaults to ${ LIMIT_DEFAULT }).`
)
.option( 'follow', 'Output new entries as they are generated.' )
.option( 'format', 'Render output in a particular format. Accepts “csv”, and “json”.', 'table' )
.examples( [
{
usage: 'vip @mysite.production logs',
description: 'Get the most recent app logs',
},
{
usage: 'vip @mysite.production logs --type batch',
description: 'Get the most recent batch logs',
usage: 'vip @example-app.production logs',
description:
'Retrieve up to 500 of the most recent entries of application Runtime Logs from web containers.',
sjinks marked this conversation as resolved.
Show resolved Hide resolved
},
{
usage: 'vip @mysite.production logs --limit 100',
description: 'Get the most recent 100 log entries',
usage: 'vip @example-app.production logs --type=batch',
description:
'Retrieve up to 500 of the most recent entries generated by cron tasks or WP-CLI commands from batch containers.',
},
{
usage: 'vip @mysite.production logs --limit 100 --format csv',
description: 'Get the most recent 100 log entries formatted as comma-separated values (CSV)',
usage: 'vip @example-app.production logs --limit=100',
description: 'Retrieve up to 100 of the most recent entries of application logs.',
},
{
usage: 'vip @mysite.production logs --limit 100 --format json',
description: 'Get the most recent 100 log entries formatted as JSON',
usage: 'vip @example-app.production logs --type=batch --limit=800 --format=csv',
description:
'Retrieve up to 800 of the most recent entries of batch logs and output them in CSV format.',
},
] )
.argv( process.argv, getLogs );
11 changes: 8 additions & 3 deletions src/bin/vip-slowlogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const exampleUsage = 'vip @example-app.develop slowlogs';
const baseUsage = 'vip slowlogs';

export async function getSlowlogs( arg: string[], opt: GetSlowLogsOptions ): Promise< void > {
opt.format ??= 'table';
validateInputs( opt.limit, opt.format );

const trackingParams = getBaseTrackingParams( opt );
Expand Down Expand Up @@ -165,7 +166,7 @@ export function validateInputs( limit: number, format: SlowlogFormats ): void {

if ( ! Number.isInteger( limit ) || limit < LIMIT_MIN || limit > slowlogsLib.LIMIT_MAX ) {
exit.withError(
`Invalid limit: ${ limit }. It should be a number between ${ LIMIT_MIN } and ${ slowlogsLib.LIMIT_MAX }.`
`Invalid limit: ${ limit }. Set the limit to an integer between ${ LIMIT_MIN } and ${ slowlogsLib.LIMIT_MAX }.`
);
}
}
Expand Down Expand Up @@ -193,8 +194,12 @@ void command( {
module: 'slowlogs',
usage: baseUsage,
} )
.option( 'limit', 'The maximum number of log entries', 500 )
.option( 'format', 'Output the log entries in CSV or JSON format', 'table' )
.option(
'limit',
'Set the maximum number of log entries. Accepts an integer value between 1 and 500.',
500
)
.option( 'format', 'Render output in a particular format. Accepts “csv”, and “json”.', 'table' )
.examples( [
{
description:
Expand Down
2 changes: 1 addition & 1 deletion src/bin/vip.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const runCmd = async function () {
.command( 'dev-env', 'Create and manage VIP Local Development Environments.' )
.command( 'export', 'Export a copy of data associated with an environment.' )
.command( 'import', 'Import media or SQL database files to an environment.' )
.command( 'logs', 'Get logs from your VIP applications' )
.command( 'logs', 'Retrieve Runtime Logs from an environment.' )
.command(
'search-replace',
'Search for a string in a local SQL file and replace it with a new string.'
Expand Down
3 changes: 2 additions & 1 deletion src/lib/cli/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,8 @@ export default function ( opts ) {
if ( _opts.format ) {
args.option(
'format',
'Render output in a particular format. Accepts "table" (default), "csv", and "json".'
'Render output in a particular format. Accepts “csv”, and “json”.',
'table'
);
}

Expand Down