From 76f6874f089c47d9ec34a270e722c4c6ec73ce7a Mon Sep 17 00:00:00 2001 From: "Stephen Weatherford (MSFT)" Date: Mon, 25 Jun 2018 15:27:46 -0700 Subject: [PATCH] Only use local port as default Mongo connection if running (#713) --- .vscode/extensions.json | 1 - README.md | 2 +- src/tree/AttachedAccountsTreeItem.ts | 17 ++++++++++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 9b500e3..5e0e900 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -2,7 +2,6 @@ "recommendations": [ "eg2.tslint", "ms-vscode.azure-account", - "DavidAnson.vscode-markdownlint", "mike-lischke.vscode-antlr4" ] } diff --git a/README.md b/README.md index ebb8f9b..f28c7f8 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ You will now find us by clicking in the new Azure view container instead of the ## Mongo Scrapbooks ### Run Mongo Commands with Rich Intellisense -- View your MongoDB database account by [signing in to Azure](#managing-azure-subscriptions) or attaching with a connection string +- View your MongoDB database account by [signing in to Azure](#managing-azure-subscriptions) or using "Attach Database Account" to connect via a connection string - Optionally configure the setting `mongo.shell.path` if your mongo executable is not already on your system's PATH (many of the common commands have built-in support and do not require the Mongo shell to be installed - see [Prerequisites](#prerequisites)) - Click on "New Mongo Scrapbook" in the tree title bar - Click on "Connect to a database" to indicate which database to run the commands against diff --git a/src/tree/AttachedAccountsTreeItem.ts b/src/tree/AttachedAccountsTreeItem.ts index 25a69cf..7d56217 100644 --- a/src/tree/AttachedAccountsTreeItem.ts +++ b/src/tree/AttachedAccountsTreeItem.ts @@ -25,6 +25,8 @@ interface IPersistedAccount { export const AttachedAccountSuffix: string = 'Attached'; export const MONGO_CONNECTION_EXPECTED: string = 'Connection string must start with "mongodb://" or "mongodb+srv://"'; +const localMongoConnectionString: string = 'mongodb://127.0.0.1:27017'; + export class AttachedAccountsTreeItem implements IAzureParentTreeItem { public static contextValue: string = 'cosmosDBAttachedAccounts'; public readonly contextValue: string = AttachedAccountsTreeItem.contextValue; @@ -94,6 +96,16 @@ export class AttachedAccountsTreeItem implements IAzureParentTreeItem { } } + private async canConnectToLocalMongoDB(): Promise { + try { + let db = await MongoClient.connect(localMongoConnectionString); + db.close(); + return true; + } catch (error) { + return false; + } + } + public async attachNewAccount(): Promise { const defaultExperiencePick = await vscode.window.showQuickPick(getExperienceQuickPicks(), { placeHolder: "Select a Database Account API...", ignoreFocusOut: true }); if (defaultExperiencePick) { @@ -102,7 +114,10 @@ export class AttachedAccountsTreeItem implements IAzureParentTreeItem { let defaultValue: string; let validateInput: (value: string) => string | undefined | null; if (defaultExperience.api === API.MongoDB) { - defaultValue = placeholder = 'mongodb://127.0.0.1:27017'; + placeholder = 'mongodb://host:port'; + if (await this.canConnectToLocalMongoDB()) { + defaultValue = placeholder = localMongoConnectionString; + } validateInput = AttachedAccountsTreeItem.validateMongoConnectionString; } else { placeholder = 'AccountEndpoint=...;AccountKey=...';