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

feat(dev-env): add quiet mode for "import sql" #1615

Merged
merged 6 commits into from
Jan 12, 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__/commands/dev-env-sync-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe( 'commands/DevEnvSyncSQLCommand', () => {
const cmd = new DevEnvSyncSQLCommand( app, env, 'test-slug' );
await cmd.runImport();

expect( mockImport ).toHaveBeenCalledWith( true );
expect( mockImport ).toHaveBeenCalled();
} );
} );

Expand Down
4 changes: 3 additions & 1 deletion src/bin/vip-dev-env-import-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ command( {
.option( 'slug', 'Custom name of the dev environment' )
.option( [ 'r', 'search-replace' ], 'Perform Search and Replace on the specified SQL file' )
.option( 'in-place', 'Search and Replace explicitly on the given input file' )
.option( 'skip-validate', 'Do not perform file validation.' )
.option( 'skip-validate', 'Do not perform file validation' )
.option( [ 'k', 'skip-reindex' ], 'Do not reindex data in Elasticsearch after import' )
.option( 'quiet', 'Suppress prompts and informational messages' )
.examples( examples )
.argv( process.argv, async ( unmatchedArgs, opt ) => {
const [ fileName ] = unmatchedArgs;
Expand Down
52 changes: 34 additions & 18 deletions src/commands/dev-env-import-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import fs from 'fs';

import * as exit from '../lib/cli/exit';
import { getFileMeta, unzipFile } from '../lib/client-file-uploader';
import { promptForBoolean, validateDependencies } from '../lib/dev-environment/dev-environment-cli';
import {
processBooleanOption,
validateDependencies,
} from '../lib/dev-environment/dev-environment-cli';
import {
getEnvironmentPath,
resolveImportPath,
Expand All @@ -25,9 +28,9 @@ export class DevEnvImportSQLCommand {
this.slug = slug;
}

async run( silent = false ) {
async run() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is gonna be an issue:

const importOptions = {
inPlace: true,
skipValidate: true,
};
const importCommand = new DevEnvImportSQLCommand( this.sqlFile, importOptions, this.slug );
await importCommand.run( true );

Also, I'd prefer keeping the silent parameter because it can be useful in re-purposing a command class in multiple places. I think all the command-classes' run functions have this, for this purpose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated dev-env-sync-sql.js to pass quiet: true in the constructor's parameters.

This is because, with VIP CLI Express, we won't be able to pass arbitrary arguments to run(). The correct approach will be to instantiate the command with the necessary parameters.

const lando = await bootstrapLando();
await validateDependencies( lando, this.slug, silent );
await validateDependencies( lando, this.slug, this.options.quiet );

validateImportFileExtension( this.fileName );

Expand All @@ -38,9 +41,16 @@ export class DevEnvImportSQLCommand {
const sqlFile = `${ tmpDir }/sql-import.sql`;

try {
console.log( `Extracting the compressed file ${ this.fileName }...` );
if ( ! this.options.quiet ) {
console.log( `Extracting the compressed file ${ this.fileName }...` );
}

await unzipFile( this.fileName, sqlFile );
console.log( `${ chalk.green( '✓' ) } Extracted to ${ sqlFile }` );

if ( ! this.options.quiet ) {
console.log( `${ chalk.green( '✓' ) } Extracted to ${ sqlFile }` );
}

this.fileName = sqlFile;
} catch ( err ) {
exit.withError( `Error extracting the SQL file: ${ err.message }` );
Expand Down Expand Up @@ -69,7 +79,9 @@ export class DevEnvImportSQLCommand {
}

const fd = await fs.promises.open( resolvedPath, 'r' );
const importArg = [ 'db', '--disable-auto-rehash' ];
const importArg = [ 'db', '--disable-auto-rehash' ].concat(
this.options.quiet ? '--silent' : []
);
const origIsTTY = process.stdin.isTTY;

try {
Expand All @@ -83,7 +95,7 @@ export class DevEnvImportSQLCommand {
process.stdin.isTTY = false;
await exec( lando, this.slug, importArg, { stdio: [ fd, 'pipe', 'pipe' ] } );

if ( ! silent ) {
if ( ! this.options.quiet ) {
console.log( `${ chalk.green.bold( 'Success:' ) } Database imported.` );
}
} finally {
Expand All @@ -94,16 +106,15 @@ export class DevEnvImportSQLCommand {
fs.unlinkSync( resolvedPath );
}

const cacheArg = [ 'wp', 'cache', 'flush' ];
const cacheArg = [ 'wp', 'cache', 'flush' ].concat( this.options.quiet ? '--quiet' : [] );
await exec( lando, this.slug, cacheArg );

try {
await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] );
const doIndex = await promptForBoolean(
'Do you want to index data in Elasticsearch (used by Enterprise Search)?',
true
);
if ( doIndex ) {
if (
undefined === this.options.skipReindex ||
! processBooleanOption( this.options.skipReindex )
) {
try {
await exec( lando, this.slug, [ 'wp', 'cli', 'has-command', 'vip-search' ] );
await exec( lando, this.slug, [
'wp',
'vip-search',
Expand All @@ -112,12 +123,17 @@ export class DevEnvImportSQLCommand {
'--network-wide',
'--skip-confirm',
] );
} catch {
// Exception means they don't have vip-search enabled.
}
} catch ( err ) {
// Exception means they don't have vip-search enabled.
}

const addUserArg = [ 'wp', 'dev-env-add-admin', '--username=vipgo', '--password=password' ];
const addUserArg = [
'wp',
'dev-env-add-admin',
'--username=vipgo',
'--password=password',
].concat( this.options.quiet ? '--quiet' : [] );
await exec( lando, this.slug, addUserArg );
}
}
3 changes: 2 additions & 1 deletion src/commands/dev-env-sync-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ export class DevEnvSyncSQLCommand {
const importOptions = {
inPlace: true,
skipValidate: true,
quiet: true,
};
const importCommand = new DevEnvImportSQLCommand( this.sqlFile, importOptions, this.slug );
await importCommand.run( true );
await importCommand.run();
}

/**
Expand Down
Loading