Skip to content

Commit

Permalink
Merge pull request #1615 from Automattic/GH-1561
Browse files Browse the repository at this point in the history
feat(dev-env): add quiet mode for "import sql"
  • Loading branch information
sjinks authored Jan 12, 2024
2 parents 5a6507d + 3e073ce commit ba09a43
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
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() {
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

0 comments on commit ba09a43

Please sign in to comment.