From 2341f074c4df32d8f96dded1d1fd9c149964604d Mon Sep 17 00:00:00 2001 From: Prajwal Pai Date: Tue, 5 Mar 2024 15:06:24 +0530 Subject: [PATCH] Format files --- src/handlers/handleCLIDownloader.ts | 37 +- src/pages/Tools/MDBExport/MDBExport.ts | 407 +++++++------- src/tools/MDBToCBExport.ts | 101 ++-- src/webViews/tools/dataMdbMigrate.webview.ts | 540 ++++++++++--------- src/webViews/tools/savedFile.ts | 432 --------------- 5 files changed, 577 insertions(+), 940 deletions(-) delete mode 100644 src/webViews/tools/savedFile.ts diff --git a/src/handlers/handleCLIDownloader.ts b/src/handlers/handleCLIDownloader.ts index 2a9757b9..3684aa11 100644 --- a/src/handlers/handleCLIDownloader.ts +++ b/src/handlers/handleCLIDownloader.ts @@ -53,9 +53,7 @@ class DependenciesDownloader { path.join(pathPrefix, "cbexport" + suffix) ); } else if (toolKey === this.TOOL_MDB_MIGRATE) { - map.set( - CBToolsType.CB_MIGRATE,"cbmigrate" + suffix - ); + map.set(CBToolsType.CB_MIGRATE, "cbmigrate" + suffix); } else if (toolKey === this.ALL_TOOLS) { map.set( CBToolsType.CBC_PILLOW_FIGHT, @@ -69,7 +67,7 @@ class DependenciesDownloader { throw new Error("Not implemented yet"); } - return map; + return map; } private getToolSpec(url: string, toolKey: string, os: string): ToolSpec { @@ -270,7 +268,11 @@ class DependenciesDownloader { } public handleCLIDownloader = () => { - const extensionPath = path.join(__filename, "..", "cb-vscode-extension"); + const extensionPath = path.join( + __filename, + "..", + "cb-vscode-extension" + ); createFolder(extensionPath); const toolsPath = path.join(extensionPath, "tools"); createFolder(toolsPath); @@ -289,7 +291,11 @@ class DependenciesDownloader { } if ( shellStatus === ToolStatus.NOT_AVAILABLE && - !this.isInstalled(toolsPath, toolShellDownloadsMap, CBToolsType.SHELL) + !this.isInstalled( + toolsPath, + toolShellDownloadsMap, + CBToolsType.SHELL + ) ) { // Avoiding 2 threads to install the same thing at the same time logger.info("Downloading CB Shell."); @@ -304,7 +310,10 @@ class DependenciesDownloader { if (cbMigrate === undefined) { return; } - const cbMigratePath = path.join(toolsPath, cbMigrate.getInstallationPath()); + const cbMigratePath = path.join( + toolsPath, + cbMigrate.getInstallationPath() + ); const cbMigrateTool = CBTools.getTool(CBToolsType.CB_MIGRATE); const cbMigrateStatus = cbMigrateTool.status; const cbMigrateDownloadsMap = downloads.get(this.TOOL_SHELL); @@ -313,7 +322,11 @@ class DependenciesDownloader { } if ( cbMigrateStatus === ToolStatus.NOT_AVAILABLE && - !this.isInstalled(toolsPath, cbMigrateDownloadsMap, CBToolsType.CB_MIGRATE) + !this.isInstalled( + toolsPath, + cbMigrateDownloadsMap, + CBToolsType.CB_MIGRATE + ) ) { // Avoiding 2 threads to install the same thing at the same time logger.info("Downloading CB Migrate.."); @@ -339,8 +352,12 @@ class DependenciesDownloader { } if ( CBTools.getTool(CBToolsType.CB_IMPORT).status === - ToolStatus.NOT_AVAILABLE && - !this.isInstalled(toolsPath, toolImpExportMap, CBToolsType.CB_EXPORT) + ToolStatus.NOT_AVAILABLE && + !this.isInstalled( + toolsPath, + toolImpExportMap, + CBToolsType.CB_EXPORT + ) ) { logger.info( "Downloading CB Import/Export. The feature will be automatically enabled when the download is complete." diff --git a/src/pages/Tools/MDBExport/MDBExport.ts b/src/pages/Tools/MDBExport/MDBExport.ts index d0774c04..ed03b7bf 100644 --- a/src/pages/Tools/MDBExport/MDBExport.ts +++ b/src/pages/Tools/MDBExport/MDBExport.ts @@ -1,7 +1,7 @@ import { MDBToCB } from "../../../tools/MDBToCBExport"; import { - CBTools, - Type as CBToolsType, + CBTools, + Type as CBToolsType, } from "../../../util/DependencyDownloaderUtils/CBTool"; import * as vscode from "vscode"; import { getActiveConnection } from "../../../util/connections"; @@ -12,214 +12,247 @@ import * as path from "path"; import { getLoader } from "../../../webViews/loader.webview"; import { dataMdbMigrateWebview } from "../../../webViews/tools/dataMdbMigrate.webview"; import { IConnection } from "../../../types/IConnection"; -import { MongoClient } from 'mongodb'; +import { MongoClient } from "mongodb"; export const getScopes = async (bucketId: string, connection: IConnection) => { - const scopes = await connection.cluster - ?.bucket(bucketId) - .collections() - .getAllScopes(); - return scopes; + const scopes = await connection.cluster + ?.bucket(bucketId) + .collections() + .getAllScopes(); + return scopes; }; - const validateFormData = (formData: any): string => { - const errors = []; - if (!formData.connectionString.trim()) { - errors.push("Please specify a valid Mongodb Connection URI"); - } - - if (!formData.database) { - errors.push("Please specify the source database to migrate from"); - } - - if (!formData.collections || formData.collections.length === 0) { - errors.push("Please specify the source collections field"); - } + const errors = []; + if (!formData.connectionString.trim()) { + errors.push("Please specify a valid Mongodb Connection URI"); + } - if (!formData.bucket) { - errors.push("Please select a Couchbase bucket"); - } + if (!formData.database) { + errors.push("Please specify the source database to migrate from"); + } - if (!formData.cbScope || formData.cbScope.length === 0) { - errors.push("Please select a Couchbase scope field"); - } + if (!formData.collections || formData.collections.length === 0) { + errors.push("Please specify the source collections field"); + } + if (!formData.bucket) { + errors.push("Please select a Couchbase bucket"); + } + if (!formData.cbScope || formData.cbScope.length === 0) { + errors.push("Please select a Couchbase scope field"); + } - if (errors.length > 0) { - return errors.join("
"); - } + if (errors.length > 0) { + return errors.join("
"); + } - return ""; + return ""; }; export interface IDataMigrateWebviewState { - webviewPanel: vscode.WebviewPanel; + webviewPanel: vscode.WebviewPanel; } export const mdbExport = async (context: vscode.ExtensionContext) => { - const connection = getActiveConnection(); - if (!connection) { - return; - } - if (!CBTools.getTool(CBToolsType.CB_MIGRATE).isAvailable()) { - vscode.window.showErrorMessage( - "CB Migrate is still loading, Please try again later" - ); - return; - } - - const dataMigratetWebviewDetails = Memory.state.get( - Constants.DATA_MIGRATE_MDB_WEBVIEW - ); - if (dataMigratetWebviewDetails) { - // data migrate webview already exists, Closing existing and creating new - try { - dataMigratetWebviewDetails.webviewPanel.dispose(); - } catch (e) { - logger.error("Error while disposing data migrate webview: " + e); + const connection = getActiveConnection(); + if (!connection) { + return; } - Memory.state.update(Constants.DATA_MIGRATE_MDB_WEBVIEW, null); - } - - const currentPanel = vscode.window.createWebviewPanel( - "dataMigrate", - "Data Migrate from MDB to Couchbase", - vscode.ViewColumn.One, - { - enableScripts: true, - enableForms: true, + if (!CBTools.getTool(CBToolsType.CB_MIGRATE).isAvailable()) { + vscode.window.showErrorMessage( + "CB Migrate is still loading, Please try again later" + ); + return; } - ); - Memory.state.update(Constants.DATA_MIGRATE_MDB_WEBVIEW, { - webviewPanel: currentPanel, - }); - currentPanel.iconPath = { - dark: vscode.Uri.file( - path.join(__filename, "..", "..", "images", "dark", "export_dark.svg") - ), - light: vscode.Uri.file( - path.join(__filename, "..", "..", "images", "light", "export_light.svg") - ), - }; - currentPanel.webview.html = getLoader("Data Export"); - - // Get all buckets - const buckets = await connection.cluster?.buckets().getAllBuckets(); - if (buckets === undefined) { - vscode.window.showErrorMessage("Buckets not found"); - return; - } - - const bucketNameArr: string[] = buckets.map(b => b.name); - - async function connectToMongoDB(connectionString: string): Promise { - try { - const client: MongoClient = await MongoClient.connect(connectionString); - logger.info("Connected successfully to MongoDB"); - const adminDb = client.db().admin(); - logger.info("fetching list of databases"); - const databasesList = await adminDb.listDatabases(); - const databases = databasesList.databases.map(db => db.name); - await client.close(); - - return databases; - } catch (error) { - let error_message = "Unable to Connect to MongoDB, Please check Connection URI and try again"; - console.error("Error while connecting to MongoDB:", error); - currentPanel.webview.postMessage({ - command: "vscode-couchbase.tools.mdbExport.formValidationError", - error: error_message, - }); - throw new Error("Failed to connect to MongoDB: " + error); + + const dataMigratetWebviewDetails = + Memory.state.get( + Constants.DATA_MIGRATE_MDB_WEBVIEW + ); + if (dataMigratetWebviewDetails) { + // data migrate webview already exists, Closing existing and creating new + try { + dataMigratetWebviewDetails.webviewPanel.dispose(); + } catch (e) { + logger.error("Error while disposing data migrate webview: " + e); + } + Memory.state.update(Constants.DATA_MIGRATE_MDB_WEBVIEW, null); } - } - async function getCollectionsList(connectionString: string, databaseName: string): Promise { - try { - const client: MongoClient = await MongoClient.connect(connectionString); - // Connect to the specific database - const db = client.db(databaseName); - - // List collections in the specific database - const collections = await db.listCollections().toArray(); - const collectionNames: string[] = collections.map(collection => collection.name); - await client.close(); - - return collectionNames; - } catch (error) { - console.error("Error while fetching collections:", error); - throw error; + const currentPanel = vscode.window.createWebviewPanel( + "dataMigrate", + "Data Migrate from MDB to Couchbase", + vscode.ViewColumn.One, + { + enableScripts: true, + enableForms: true, + } + ); + Memory.state.update(Constants.DATA_MIGRATE_MDB_WEBVIEW, { + webviewPanel: currentPanel, + }); + currentPanel.iconPath = { + dark: vscode.Uri.file( + path.join( + __filename, + "..", + "..", + "images", + "dark", + "export_dark.svg" + ) + ), + light: vscode.Uri.file( + path.join( + __filename, + "..", + "..", + "images", + "light", + "export_light.svg" + ) + ), + }; + currentPanel.webview.html = getLoader("Data Export"); + + // Get all buckets + const buckets = await connection.cluster?.buckets().getAllBuckets(); + if (buckets === undefined) { + vscode.window.showErrorMessage("Buckets not found"); + return; } - } - - try { - currentPanel.webview.html = await dataMdbMigrateWebview(bucketNameArr); - currentPanel.webview.onDidReceiveMessage(async (message) => { - switch (message.command) { - // ADD cases here :) - case "vscode-couchbase.tools.mdbExport.runExport": - const formData = message.data; - console.log(formData); - const validationError = validateFormData(formData); - if (validationError === "") { - // There is no error - MDBToCB.export( - formData.connectionString, - formData.database, - formData.collections, - formData.indexes, - formData.bucket, - formData.cbScope, - context + + const bucketNameArr: string[] = buckets.map((b) => b.name); + + async function connectToMongoDB( + connectionString: string + ): Promise { + try { + const client: MongoClient = await MongoClient.connect( + connectionString ); - } else { + logger.info("Connected successfully to MongoDB"); + const adminDb = client.db().admin(); + logger.info("fetching list of databases"); + const databasesList = await adminDb.listDatabases(); + const databases = databasesList.databases.map((db) => db.name); + await client.close(); + + return databases; + } catch (error) { + let error_message = + "Unable to Connect to MongoDB, Please check Connection URI and try again"; + console.error("Error while connecting to MongoDB:", error); currentPanel.webview.postMessage({ - command: "vscode-couchbase.tools.mdbExport.formValidationError", - error: validationError, + command: "vscode-couchbase.tools.mdbExport.formValidationError", + error: error_message, }); - } - break; - case "vscode-couchbase.tools.mdbExport.getCollections": - const collection = await getCollectionsList(message.connectionString, message.databases); - currentPanel.webview.postMessage({ - command: "vscode-couchbase.tools.mdbExport.collectionInfo", - collection: collection - }); - - break; - - case "vscode-couchbase.tools.mdbExport.getCbScopes": - const scopes = await getScopes(message.bucketId, connection); - if (scopes === undefined) { - vscode.window.showErrorMessage("Scopes are undefined"); - break; - } - currentPanel.webview.postMessage({ - command: "vscode-couchbase.tools.mdbExport.scopesInfo", - scopes: scopes, - }); - break; - - case "vscode-couchbase.tools.mdbExport.getDatabases": - const databases = await connectToMongoDB(message.connectionString); - currentPanel.webview.postMessage({ - command: "vscode-couchbase.tools.mdbExport.databaseInfo", - databases: databases, - }); - - } - }); - } catch (err) { - logger.error(`Failed to open data migrate webview`); - logger.debug(err); - vscode.window.showErrorMessage( - "Failed to open data migrate webview: " + err - ); - } + throw new Error("Failed to connect to MongoDB: " + error); + } + } + + async function getCollectionsList( + connectionString: string, + databaseName: string + ): Promise { + try { + const client: MongoClient = await MongoClient.connect( + connectionString + ); + // Connect to the specific database + const db = client.db(databaseName); + + // List collections in the specific database + const collections = await db.listCollections().toArray(); + const collectionNames: string[] = collections.map( + (collection) => collection.name + ); + await client.close(); + + return collectionNames; + } catch (error) { + console.error("Error while fetching collections:", error); + throw error; + } + } + + try { + currentPanel.webview.html = await dataMdbMigrateWebview(bucketNameArr); + currentPanel.webview.onDidReceiveMessage(async (message) => { + switch (message.command) { + // ADD cases here :) + case "vscode-couchbase.tools.mdbExport.runExport": + const formData = message.data; + const validationError = validateFormData(formData); + if (validationError === "") { + // There is no error + MDBToCB.export( + formData.connectionString, + formData.database, + formData.collections, + formData.indexes, + formData.bucket, + formData.cbScope, + context + ); + } else { + currentPanel.webview.postMessage({ + command: + "vscode-couchbase.tools.mdbExport.formValidationError", + error: validationError, + }); + } + break; + case "vscode-couchbase.tools.mdbExport.getCollections": + const collection = await getCollectionsList( + message.connectionString, + message.databases + ); + currentPanel.webview.postMessage({ + command: + "vscode-couchbase.tools.mdbExport.collectionInfo", + collection: collection, + }); + + break; + + case "vscode-couchbase.tools.mdbExport.getCbScopes": + const scopes = await getScopes( + message.bucketId, + connection + ); + if (scopes === undefined) { + vscode.window.showErrorMessage("Scopes are undefined"); + break; + } + currentPanel.webview.postMessage({ + command: "vscode-couchbase.tools.mdbExport.scopesInfo", + scopes: scopes, + }); + break; - currentPanel.onDidDispose(() => { - Memory.state.update(Constants.DATA_MIGRATE_MDB_WEBVIEW, null); - }); -}; \ No newline at end of file + case "vscode-couchbase.tools.mdbExport.getDatabases": + const databases = await connectToMongoDB( + message.connectionString + ); + currentPanel.webview.postMessage({ + command: + "vscode-couchbase.tools.mdbExport.databaseInfo", + databases: databases, + }); + } + }); + } catch (err) { + logger.error(`Failed to open data migrate webview`); + logger.debug(err); + vscode.window.showErrorMessage( + "Failed to open data migrate webview: " + err + ); + } + + currentPanel.onDidDispose(() => { + Memory.state.update(Constants.DATA_MIGRATE_MDB_WEBVIEW, null); + }); +}; diff --git a/src/tools/MDBToCBExport.ts b/src/tools/MDBToCBExport.ts index 6968437c..f15b3dfd 100644 --- a/src/tools/MDBToCBExport.ts +++ b/src/tools/MDBToCBExport.ts @@ -2,7 +2,7 @@ import { CBTools, Type } from "../util/DependencyDownloaderUtils/CBTool"; import { getActiveConnection, getConnectionId } from "../util/connections"; import { Constants } from "../util/constants"; import * as keytar from "keytar"; -import * as vscode from 'vscode'; +import * as vscode from "vscode"; export class MDBToCB { static async export( @@ -14,65 +14,74 @@ export class MDBToCB { cbScope: string, context: vscode.ExtensionContext ): Promise { - const connection = getActiveConnection(); if (!connection) { return; } - - const password = await keytar.getPassword(Constants.extensionID, getConnectionId(connection)); + const password = await keytar.getPassword( + Constants.extensionID, + getConnectionId(connection) + ); if (!password) { return undefined; } try { for (const collection of collections) { - // Build Command - const cmd: string[] = []; - cmd.push(CBTools.getTool(Type.CB_MIGRATE).path); - cmd.push("mongo ") - cmd.push("--mongodb-uri"); - cmd.push(mDBConnectionString); - cmd.push("--mongodb-database"); - cmd.push(databases) - cmd.push("--mongodb-collection"); - cmd.push(collection); - cmd.push("--cb-cluster"); - cmd.push(connection.url); - if (indexes){ - cmd.push("--copy-indexes"); - } - cmd.push("--cb-username"); - cmd.push(connection.username); - cmd.push("--cb-password"); - cmd.push(password); - cmd.push("--cb-bucket"); - cmd.push(cbBucket); - cmd.push("--cb-scope"); - cmd.push(cbScope); - // cmd.push("--cb-collection"); - // cmd.push(colCB); - cmd.push("--cb-generate-key"); - cmd.push("%_id%"); + // Build Command + const cmd: string[] = []; + cmd.push(CBTools.getTool(Type.CB_MIGRATE).path); + cmd.push("mongo "); + cmd.push("--mongodb-uri"); + cmd.push(mDBConnectionString); + cmd.push("--mongodb-database"); + cmd.push(databases); + cmd.push("--mongodb-collection"); + cmd.push(collection); + cmd.push("--cb-cluster"); + cmd.push(connection.url); + if (indexes) { + cmd.push("--copy-indexes"); + } + cmd.push("--cb-username"); + cmd.push(connection.username); + cmd.push("--cb-password"); + cmd.push(password); + cmd.push("--cb-bucket"); + cmd.push(cbBucket); + cmd.push("--cb-scope"); + cmd.push(cbScope); + // cmd.push("--cb-collection"); + // cmd.push(colCB); + cmd.push("--cb-generate-key"); + cmd.push("%_id%"); - cmd.push("; \n"); - cmd.push("export CB_PASSWORD=''"); // To make sure that password is truly unset + cmd.push("; \n"); + cmd.push("export CB_PASSWORD=''"); // To make sure that password is truly unset - // Run Command - const terminal: vscode.Terminal = vscode.window.createTerminal("MDBToCb"); - // sending password to vscode environment variables. Note: Password is still accessible via terminal, till its removed - context.environmentVariableCollection.replace('CB_PASSWORD', password); - let text = cmd.join(" "); - terminal.sendText(text); - terminal.show(); - // removing password from vscode environment variables after 5 seconds - await new Promise((resolve) => setTimeout(resolve, 5000)); - context.environmentVariableCollection.replace('CB_PASSWORD', ''); + // Run Command + const terminal: vscode.Terminal = + vscode.window.createTerminal("MDBToCb"); + // sending password to vscode environment variables. Note: Password is still accessible via terminal, till its removed + context.environmentVariableCollection.replace( + "CB_PASSWORD", + password + ); + let text = cmd.join(" "); + terminal.sendText(text); + terminal.show(); + // removing password from vscode environment variables after 5 seconds + await new Promise((resolve) => setTimeout(resolve, 5000)); + context.environmentVariableCollection.replace( + "CB_PASSWORD", + "" + ); } } catch (error) { - console.error("An error occurred while trying to migrate the databases from mongodb to couchbase"); + console.error( + "An error occurred while trying to migrate the databases from mongodb to couchbase" + ); console.error(error); } } - -} \ No newline at end of file +} diff --git a/src/webViews/tools/dataMdbMigrate.webview.ts b/src/webViews/tools/dataMdbMigrate.webview.ts index 85a17186..74adfb67 100644 --- a/src/webViews/tools/dataMdbMigrate.webview.ts +++ b/src/webViews/tools/dataMdbMigrate.webview.ts @@ -5,7 +5,7 @@ export const dataMdbMigrateWebview = async (buckets: string[]): Promise - Data Export + Data Migrate - - - -

Data Migration

-
- - - - - -
-
-
- - - -
- - - -
- - - -
- - -
-
- - - - - - - - `; -}; \ No newline at end of file