mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-17 02:51:36 -05:00
Add static logger class for azdata extension (#11939)
* Add static logger class for arc extension * Fix compile errors * Fix test
This commit is contained in:
@@ -10,6 +10,7 @@ import * as loc from './localizedConstants';
|
|||||||
import { executeCommand, executeSudoCommand, ExitCodeError } from './common/childProcess';
|
import { executeCommand, executeSudoCommand, ExitCodeError } from './common/childProcess';
|
||||||
import { searchForCmd } from './common/utils';
|
import { searchForCmd } from './common/utils';
|
||||||
import * as azdataExt from 'azdata-ext';
|
import * as azdataExt from 'azdata-ext';
|
||||||
|
import Logger from './common/logger';
|
||||||
|
|
||||||
export const azdataHostname = 'https://aka.ms';
|
export const azdataHostname = 'https://aka.ms';
|
||||||
export const azdataUri = 'azdata-msi';
|
export const azdataUri = 'azdata-msi';
|
||||||
@@ -26,7 +27,7 @@ export interface IAzdataTool extends azdataExt.IAzdataApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class AzdataTool implements IAzdataTool {
|
class AzdataTool implements IAzdataTool {
|
||||||
constructor(public path: string, public toolVersion: string, private _outputChannel: vscode.OutputChannel) { }
|
constructor(public path: string, public toolVersion: string) { }
|
||||||
|
|
||||||
public arc = {
|
public arc = {
|
||||||
dc: {
|
dc: {
|
||||||
@@ -97,7 +98,7 @@ class AzdataTool implements IAzdataTool {
|
|||||||
|
|
||||||
public async executeCommand<R>(args: string[], additionalEnvVars?: { [key: string]: string }): Promise<azdataExt.AzdataOutput<R>> {
|
public async executeCommand<R>(args: string[], additionalEnvVars?: { [key: string]: string }): Promise<azdataExt.AzdataOutput<R>> {
|
||||||
try {
|
try {
|
||||||
const output = JSON.parse((await executeCommand(`"${this.path}"`, args.concat(['--output', 'json']), this._outputChannel, additionalEnvVars)).stdout);
|
const output = JSON.parse((await executeCommand(`"${this.path}"`, args.concat(['--output', 'json']), additionalEnvVars)).stdout);
|
||||||
return {
|
return {
|
||||||
logs: <string[]>output.log,
|
logs: <string[]>output.log,
|
||||||
stdout: <string[]>output.stdout,
|
stdout: <string[]>output.stdout,
|
||||||
@@ -119,45 +120,43 @@ class AzdataTool implements IAzdataTool {
|
|||||||
/**
|
/**
|
||||||
* Finds the existing installation of azdata, or throws an error if it couldn't find it
|
* Finds the existing installation of azdata, or throws an error if it couldn't find it
|
||||||
* or encountered an unexpected error.
|
* or encountered an unexpected error.
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
*/
|
*/
|
||||||
export async function findAzdata(outputChannel: vscode.OutputChannel): Promise<IAzdataTool> {
|
export async function findAzdata(): Promise<IAzdataTool> {
|
||||||
outputChannel.appendLine(loc.searchingForAzdata);
|
Logger.log(loc.searchingForAzdata);
|
||||||
try {
|
try {
|
||||||
let azdata: IAzdataTool | undefined = undefined;
|
let azdata: IAzdataTool | undefined = undefined;
|
||||||
switch (process.platform) {
|
switch (process.platform) {
|
||||||
case 'win32':
|
case 'win32':
|
||||||
azdata = await findAzdataWin32(outputChannel);
|
azdata = await findAzdataWin32();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
azdata = await findSpecificAzdata('azdata', outputChannel);
|
azdata = await findSpecificAzdata('azdata');
|
||||||
}
|
}
|
||||||
outputChannel.appendLine(loc.foundExistingAzdata(azdata.path, azdata.toolVersion));
|
Logger.log(loc.foundExistingAzdata(azdata.path, azdata.toolVersion));
|
||||||
return azdata;
|
return azdata;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
outputChannel.appendLine(loc.couldNotFindAzdata(err));
|
Logger.log(loc.couldNotFindAzdata(err));
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the appropriate installer and/or runs the command to install azdata
|
* Downloads the appropriate installer and/or runs the command to install azdata
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
*/
|
*/
|
||||||
export async function downloadAndInstallAzdata(outputChannel: vscode.OutputChannel): Promise<void> {
|
export async function downloadAndInstallAzdata(): Promise<void> {
|
||||||
const statusDisposable = vscode.window.setStatusBarMessage(loc.installingAzdata);
|
const statusDisposable = vscode.window.setStatusBarMessage(loc.installingAzdata);
|
||||||
outputChannel.show();
|
Logger.show();
|
||||||
outputChannel.appendLine(loc.installingAzdata);
|
Logger.log(loc.installingAzdata);
|
||||||
try {
|
try {
|
||||||
switch (process.platform) {
|
switch (process.platform) {
|
||||||
case 'win32':
|
case 'win32':
|
||||||
await downloadAndInstallAzdataWin32(outputChannel);
|
await downloadAndInstallAzdataWin32();
|
||||||
break;
|
break;
|
||||||
case 'darwin':
|
case 'darwin':
|
||||||
await installAzdataDarwin(outputChannel);
|
await installAzdataDarwin();
|
||||||
break;
|
break;
|
||||||
case 'linux':
|
case 'linux':
|
||||||
await installAzdataLinux(outputChannel);
|
await installAzdataLinux();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error(loc.platformUnsupported(process.platform));
|
throw new Error(loc.platformUnsupported(process.platform));
|
||||||
@@ -169,58 +168,55 @@ export async function downloadAndInstallAzdata(outputChannel: vscode.OutputChann
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads the Windows installer and runs it
|
* Downloads the Windows installer and runs it
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
*/
|
*/
|
||||||
async function downloadAndInstallAzdataWin32(outputChannel: vscode.OutputChannel): Promise<void> {
|
async function downloadAndInstallAzdataWin32(): Promise<void> {
|
||||||
const downloadFolder = os.tmpdir();
|
const downloadFolder = os.tmpdir();
|
||||||
const downloadedFile = await HttpClient.download(`${azdataHostname}/${azdataUri}`, downloadFolder, outputChannel);
|
const downloadedFile = await HttpClient.download(`${azdataHostname}/${azdataUri}`, downloadFolder);
|
||||||
await executeCommand('msiexec', ['/qn', '/i', downloadedFile], outputChannel);
|
await executeCommand('msiexec', ['/qn', '/i', downloadedFile]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs commands to install azdata on MacOS
|
* Runs commands to install azdata on MacOS
|
||||||
*/
|
*/
|
||||||
async function installAzdataDarwin(outputChannel: vscode.OutputChannel): Promise<void> {
|
async function installAzdataDarwin(): Promise<void> {
|
||||||
await executeCommand('brew', ['tap', 'microsoft/azdata-cli-release'], outputChannel);
|
await executeCommand('brew', ['tap', 'microsoft/azdata-cli-release']);
|
||||||
await executeCommand('brew', ['update'], outputChannel);
|
await executeCommand('brew', ['update']);
|
||||||
await executeCommand('brew', ['install', 'azdata-cli'], outputChannel);
|
await executeCommand('brew', ['install', 'azdata-cli']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs commands to install azdata on Linux
|
* Runs commands to install azdata on Linux
|
||||||
*/
|
*/
|
||||||
async function installAzdataLinux(outputChannel: vscode.OutputChannel): Promise<void> {
|
async function installAzdataLinux(): Promise<void> {
|
||||||
// https://docs.microsoft.com/en-us/sql/big-data-cluster/deploy-install-azdata-linux-package
|
// https://docs.microsoft.com/en-us/sql/big-data-cluster/deploy-install-azdata-linux-package
|
||||||
// Get packages needed for install process
|
// Get packages needed for install process
|
||||||
await executeSudoCommand('apt-get update', outputChannel);
|
await executeSudoCommand('apt-get update');
|
||||||
await executeSudoCommand('apt-get install gnupg ca-certificates curl wget software-properties-common apt-transport-https lsb-release -y', outputChannel);
|
await executeSudoCommand('apt-get install gnupg ca-certificates curl wget software-properties-common apt-transport-https lsb-release -y');
|
||||||
// Download and install the signing key
|
// Download and install the signing key
|
||||||
await executeSudoCommand('curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null', outputChannel);
|
await executeSudoCommand('curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null');
|
||||||
// Add the azdata repository information
|
// Add the azdata repository information
|
||||||
const release = (await executeCommand('lsb_release', ['-rs'], outputChannel)).stdout.trim();
|
const release = (await executeCommand('lsb_release', ['-rs'])).stdout.trim();
|
||||||
await executeSudoCommand(`add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/${release}/mssql-server-2019.list)"`, outputChannel);
|
await executeSudoCommand(`add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/${release}/mssql-server-2019.list)"`);
|
||||||
// Update repository information and install azdata
|
// Update repository information and install azdata
|
||||||
await executeSudoCommand('apt-get update', outputChannel);
|
await executeSudoCommand('apt-get update');
|
||||||
await executeSudoCommand('apt-get install -y azdata-cli', outputChannel);
|
await executeSudoCommand('apt-get install -y azdata-cli');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds azdata specifically on Windows
|
* Finds azdata specifically on Windows
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
*/
|
*/
|
||||||
async function findAzdataWin32(outputChannel: vscode.OutputChannel): Promise<IAzdataTool> {
|
async function findAzdataWin32(): Promise<IAzdataTool> {
|
||||||
const promise = searchForCmd('azdata.cmd');
|
const promise = searchForCmd('azdata.cmd');
|
||||||
return findSpecificAzdata(await promise, outputChannel);
|
return findSpecificAzdata(await promise);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the version using a known azdata path
|
* Gets the version using a known azdata path
|
||||||
* @param path The path to the azdata executable
|
* @param path The path to the azdata executable
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
*/
|
*/
|
||||||
async function findSpecificAzdata(path: string, outputChannel: vscode.OutputChannel): Promise<IAzdataTool> {
|
async function findSpecificAzdata(path: string): Promise<IAzdataTool> {
|
||||||
const versionOutput = await executeCommand(`"${path}"`, ['--version'], outputChannel);
|
const versionOutput = await executeCommand(`"${path}"`, ['--version']);
|
||||||
return new AzdataTool(path, parseVersion(versionOutput.stdout), outputChannel);
|
return new AzdataTool(path, parseVersion(versionOutput.stdout));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import * as vscode from 'vscode';
|
|||||||
import * as cp from 'child_process';
|
import * as cp from 'child_process';
|
||||||
import * as sudo from 'sudo-prompt';
|
import * as sudo from 'sudo-prompt';
|
||||||
import * as loc from '../localizedConstants';
|
import * as loc from '../localizedConstants';
|
||||||
|
import Logger from './logger';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper error for when an unexpected exit code was received
|
* Wrapper error for when an unexpected exit code was received
|
||||||
@@ -46,11 +47,10 @@ export type ProcessOutput = { stdout: string, stderr: string };
|
|||||||
* @param command The command to execute
|
* @param command The command to execute
|
||||||
* @param args Optional args to pass, every arg and arg value must be a separate item in the array
|
* @param args Optional args to pass, every arg and arg value must be a separate item in the array
|
||||||
* @param additionalEnvVars Additional environment variables to add to the process environment
|
* @param additionalEnvVars Additional environment variables to add to the process environment
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
*/
|
*/
|
||||||
export async function executeCommand(command: string, args: string[], outputChannel: vscode.OutputChannel, additionalEnvVars?: { [key: string]: string },): Promise<ProcessOutput> {
|
export async function executeCommand(command: string, args: string[], additionalEnvVars?: { [key: string]: string },): Promise<ProcessOutput> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
outputChannel.appendLine(loc.executingCommand(command, args));
|
Logger.log(loc.executingCommand(command, args));
|
||||||
const stdoutBuffers: Buffer[] = [];
|
const stdoutBuffers: Buffer[] = [];
|
||||||
const stderrBuffers: Buffer[] = [];
|
const stderrBuffers: Buffer[] = [];
|
||||||
const env = Object.assign({}, process.env, additionalEnvVars);
|
const env = Object.assign({}, process.env, additionalEnvVars);
|
||||||
@@ -62,14 +62,14 @@ export async function executeCommand(command: string, args: string[], outputChan
|
|||||||
const stdout = Buffer.concat(stdoutBuffers).toString('utf8').trim();
|
const stdout = Buffer.concat(stdoutBuffers).toString('utf8').trim();
|
||||||
const stderr = Buffer.concat(stderrBuffers).toString('utf8').trim();
|
const stderr = Buffer.concat(stderrBuffers).toString('utf8').trim();
|
||||||
if (stdout) {
|
if (stdout) {
|
||||||
outputChannel.appendLine(loc.stdoutOutput(stdout));
|
Logger.log(loc.stdoutOutput(stdout));
|
||||||
}
|
}
|
||||||
if (stderr) {
|
if (stderr) {
|
||||||
outputChannel.appendLine(loc.stdoutOutput(stderr));
|
Logger.log(loc.stdoutOutput(stderr));
|
||||||
}
|
}
|
||||||
if (code) {
|
if (code) {
|
||||||
const err = new ExitCodeError(code, stderr);
|
const err = new ExitCodeError(code, stderr);
|
||||||
outputChannel.appendLine(err.message);
|
Logger.log(err.message);
|
||||||
reject(err);
|
reject(err);
|
||||||
} else {
|
} else {
|
||||||
resolve({ stdout: stdout, stderr: stderr });
|
resolve({ stdout: stdout, stderr: stderr });
|
||||||
@@ -83,22 +83,21 @@ export async function executeCommand(command: string, args: string[], outputChan
|
|||||||
* this function. The exact prompt is platform-dependent.
|
* this function. The exact prompt is platform-dependent.
|
||||||
* @param command The command to execute
|
* @param command The command to execute
|
||||||
* @param args The additional args
|
* @param args The additional args
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
*/
|
*/
|
||||||
export async function executeSudoCommand(command: string, outputChannel: vscode.OutputChannel): Promise<ProcessOutput> {
|
export async function executeSudoCommand(command: string): Promise<ProcessOutput> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
outputChannel.appendLine(loc.executingCommand(`sudo ${command}`, []));
|
Logger.log(loc.executingCommand(`sudo ${command}`, []));
|
||||||
sudo.exec(command, { name: vscode.env.appName }, (error, stdout, stderr) => {
|
sudo.exec(command, { name: vscode.env.appName }, (error, stdout, stderr) => {
|
||||||
stdout = stdout?.toString() ?? '';
|
stdout = stdout?.toString() ?? '';
|
||||||
stderr = stderr?.toString() ?? '';
|
stderr = stderr?.toString() ?? '';
|
||||||
if (stdout) {
|
if (stdout) {
|
||||||
outputChannel.appendLine(loc.stdoutOutput(stdout));
|
Logger.log(loc.stdoutOutput(stdout));
|
||||||
}
|
}
|
||||||
if (stderr) {
|
if (stderr) {
|
||||||
outputChannel.appendLine(loc.stdoutOutput(stderr));
|
Logger.log(loc.stdoutOutput(stderr));
|
||||||
}
|
}
|
||||||
if (error) {
|
if (error) {
|
||||||
outputChannel.appendLine(loc.unexpectedCommandError(error.message));
|
Logger.log(loc.unexpectedCommandError(error.message));
|
||||||
reject(error);
|
reject(error);
|
||||||
} else {
|
} else {
|
||||||
resolve({ stdout: stdout, stderr: stderr });
|
resolve({ stdout: stdout, stderr: stderr });
|
||||||
|
|||||||
@@ -3,11 +3,11 @@
|
|||||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
import * as vscode from 'vscode';
|
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as request from 'request';
|
import * as request from 'request';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as loc from '../localizedConstants';
|
import * as loc from '../localizedConstants';
|
||||||
|
import Logger from './logger';
|
||||||
|
|
||||||
const DownloadTimeout = 20000;
|
const DownloadTimeout = 20000;
|
||||||
|
|
||||||
@@ -17,34 +17,33 @@ export namespace HttpClient {
|
|||||||
* Downloads a file from the given URL, resolving to the full path of the downloaded file when complete
|
* Downloads a file from the given URL, resolving to the full path of the downloaded file when complete
|
||||||
* @param downloadUrl The URL to download the file from
|
* @param downloadUrl The URL to download the file from
|
||||||
* @param targetFolder The folder to download the file to
|
* @param targetFolder The folder to download the file to
|
||||||
* @param outputChannel Channel used to display diagnostic information
|
|
||||||
* @returns Full path to the downloaded file
|
* @returns Full path to the downloaded file
|
||||||
*/
|
*/
|
||||||
export function download(downloadUrl: string, targetFolder: string, outputChannel: vscode.OutputChannel): Promise<string> {
|
export function download(downloadUrl: string, targetFolder: string): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let totalMegaBytes: number | undefined = undefined;
|
let totalMegaBytes: number | undefined = undefined;
|
||||||
let receivedBytes = 0;
|
let receivedBytes = 0;
|
||||||
let printThreshold = 0.1;
|
let printThreshold = 0.1;
|
||||||
let downloadRequest = request.get(downloadUrl, { timeout: DownloadTimeout })
|
let downloadRequest = request.get(downloadUrl, { timeout: DownloadTimeout })
|
||||||
.on('error', downloadError => {
|
.on('error', downloadError => {
|
||||||
outputChannel.appendLine(loc.downloadError);
|
Logger.log(loc.downloadError);
|
||||||
outputChannel.appendLine(downloadError?.message ?? downloadError);
|
Logger.log(downloadError?.message ?? downloadError);
|
||||||
reject(downloadError);
|
reject(downloadError);
|
||||||
})
|
})
|
||||||
.on('response', (response) => {
|
.on('response', (response) => {
|
||||||
if (response.statusCode !== 200) {
|
if (response.statusCode !== 200) {
|
||||||
outputChannel.appendLine(loc.downloadError);
|
Logger.log(loc.downloadError);
|
||||||
outputChannel.appendLine(response.statusMessage);
|
Logger.log(response.statusMessage);
|
||||||
return reject(response.statusMessage);
|
return reject(response.statusMessage);
|
||||||
}
|
}
|
||||||
const filename = path.basename(response.request.path);
|
const filename = path.basename(response.request.path);
|
||||||
const targetPath = path.join(targetFolder, filename);
|
const targetPath = path.join(targetFolder, filename);
|
||||||
outputChannel.appendLine(loc.downloadingTo(filename, targetPath));
|
Logger.log(loc.downloadingTo(filename, targetPath));
|
||||||
// Wait to create the WriteStream until here so we can use the actual
|
// Wait to create the WriteStream until here so we can use the actual
|
||||||
// filename based off of the URI.
|
// filename based off of the URI.
|
||||||
downloadRequest.pipe(fs.createWriteStream(targetPath))
|
downloadRequest.pipe(fs.createWriteStream(targetPath))
|
||||||
.on('close', async () => {
|
.on('close', async () => {
|
||||||
outputChannel.appendLine(loc.downloadFinished);
|
Logger.log(loc.downloadFinished);
|
||||||
resolve(targetPath);
|
resolve(targetPath);
|
||||||
})
|
})
|
||||||
.on('error', (downloadError) => {
|
.on('error', (downloadError) => {
|
||||||
@@ -54,7 +53,7 @@ export namespace HttpClient {
|
|||||||
let contentLength = response.headers['content-length'];
|
let contentLength = response.headers['content-length'];
|
||||||
let totalBytes = parseInt(contentLength || '0');
|
let totalBytes = parseInt(contentLength || '0');
|
||||||
totalMegaBytes = totalBytes / (1024 * 1024);
|
totalMegaBytes = totalBytes / (1024 * 1024);
|
||||||
outputChannel.appendLine(loc.downloadingProgressMb('0', totalMegaBytes.toFixed(2)));
|
Logger.log(loc.downloadingProgressMb('0', totalMegaBytes.toFixed(2)));
|
||||||
})
|
})
|
||||||
.on('data', (data) => {
|
.on('data', (data) => {
|
||||||
receivedBytes += data.length;
|
receivedBytes += data.length;
|
||||||
@@ -62,7 +61,7 @@ export namespace HttpClient {
|
|||||||
let receivedMegaBytes = receivedBytes / (1024 * 1024);
|
let receivedMegaBytes = receivedBytes / (1024 * 1024);
|
||||||
let percentage = receivedMegaBytes / totalMegaBytes;
|
let percentage = receivedMegaBytes / totalMegaBytes;
|
||||||
if (percentage >= printThreshold) {
|
if (percentage >= printThreshold) {
|
||||||
outputChannel.appendLine(loc.downloadingProgressMb(receivedMegaBytes.toFixed(2), totalMegaBytes.toFixed(2)));
|
Logger.log(loc.downloadingProgressMb(receivedMegaBytes.toFixed(2), totalMegaBytes.toFixed(2)));
|
||||||
printThreshold += 0.1;
|
printThreshold += 0.1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,8 +73,8 @@ export namespace HttpClient {
|
|||||||
* Gets the filename for the specified URL - following redirects as needed
|
* Gets the filename for the specified URL - following redirects as needed
|
||||||
* @param url The URL to get the filename of
|
* @param url The URL to get the filename of
|
||||||
*/
|
*/
|
||||||
export async function getFilename(url: string, outputChannel: vscode.OutputChannel): Promise<string> {
|
export async function getFilename(url: string): Promise<string> {
|
||||||
outputChannel.appendLine(loc.gettingFilenameOfUrl(url));
|
Logger.log(loc.gettingFilenameOfUrl(url));
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let httpRequest = request.get(url, { timeout: DownloadTimeout })
|
let httpRequest = request.get(url, { timeout: DownloadTimeout })
|
||||||
.on('error', downloadError => {
|
.on('error', downloadError => {
|
||||||
@@ -88,7 +87,7 @@ export namespace HttpClient {
|
|||||||
// We don't want to actually download the file so abort the request now
|
// We don't want to actually download the file so abort the request now
|
||||||
httpRequest.abort();
|
httpRequest.abort();
|
||||||
const filename = path.basename(response.request.path);
|
const filename = path.basename(response.request.path);
|
||||||
outputChannel.appendLine(loc.gotFilenameOfUrl(response.request.path, filename));
|
Logger.log(loc.gotFilenameOfUrl(response.request.path, filename));
|
||||||
resolve(filename);
|
resolve(filename);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
24
extensions/azdata/src/common/logger.ts
Normal file
24
extensions/azdata/src/common/logger.ts
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*---------------------------------------------------------------------------------------------
|
||||||
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import * as vscode from 'vscode';
|
||||||
|
|
||||||
|
export class Log {
|
||||||
|
private _output: vscode.OutputChannel;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this._output = vscode.window.createOutputChannel('azdata');
|
||||||
|
}
|
||||||
|
|
||||||
|
log(msg: string): void {
|
||||||
|
this._output.appendLine(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
show(): void {
|
||||||
|
this._output.show(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const Logger = new Log();
|
||||||
|
export default Logger;
|
||||||
@@ -4,15 +4,13 @@
|
|||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
import * as azdataExt from 'azdata-ext';
|
import * as azdataExt from 'azdata-ext';
|
||||||
import * as vscode from 'vscode';
|
|
||||||
import { findAzdata, IAzdataTool } from './azdata';
|
import { findAzdata, IAzdataTool } from './azdata';
|
||||||
import * as loc from './localizedConstants';
|
import * as loc from './localizedConstants';
|
||||||
|
|
||||||
let localAzdata: IAzdataTool | undefined = undefined;
|
let localAzdata: IAzdataTool | undefined = undefined;
|
||||||
|
|
||||||
export async function activate(): Promise<azdataExt.IExtension> {
|
export async function activate(): Promise<azdataExt.IExtension> {
|
||||||
const outputChannel = vscode.window.createOutputChannel('azdata');
|
localAzdata = await checkForAzdata();
|
||||||
localAzdata = await checkForAzdata(outputChannel);
|
|
||||||
return {
|
return {
|
||||||
azdata: {
|
azdata: {
|
||||||
arc: {
|
arc: {
|
||||||
@@ -85,24 +83,24 @@ function throwIfNoAzdata(): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function checkForAzdata(outputChannel: vscode.OutputChannel): Promise<IAzdataTool | undefined> {
|
async function checkForAzdata(): Promise<IAzdataTool | undefined> {
|
||||||
try {
|
try {
|
||||||
return await findAzdata(outputChannel);
|
return await findAzdata();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Don't block on this since we want the extension to finish activating without needing user input.
|
// Don't block on this since we want the extension to finish activating without needing user input.
|
||||||
// Calls will be made to handle azdata not being installed
|
// Calls will be made to handle azdata not being installed
|
||||||
promptToInstallAzdata(outputChannel).catch(e => console.log(`Unexpected error prompting to install azdata ${e}`));
|
promptToInstallAzdata().catch(e => console.log(`Unexpected error prompting to install azdata ${e}`));
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function promptToInstallAzdata(_outputChannel: vscode.OutputChannel): Promise<void> {
|
async function promptToInstallAzdata(): Promise<void> {
|
||||||
//TODO: Figure out better way to display/prompt
|
//TODO: Figure out better way to display/prompt
|
||||||
/*
|
/*
|
||||||
const response = await vscode.window.showErrorMessage(loc.couldNotFindAzdataWithPrompt, loc.install, loc.cancel);
|
const response = await vscode.window.showErrorMessage(loc.couldNotFindAzdataWithPrompt, loc.install, loc.cancel);
|
||||||
if (response === loc.install) {
|
if (response === loc.install) {
|
||||||
try {
|
try {
|
||||||
await downloadAndInstallAzdata(outputChannel);
|
await downloadAndInstallAzdata();
|
||||||
vscode.window.showInformationMessage(loc.azdataInstalled);
|
vscode.window.showInformationMessage(loc.azdataInstalled);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Windows: 1602 is User Cancelling installation - not unexpected so don't display
|
// Windows: 1602 is User Cancelling installation - not unexpected so don't display
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
import * as vscode from 'vscode';
|
|
||||||
import * as TypeMoq from 'typemoq';
|
|
||||||
import * as azdata from '../azdata';
|
import * as azdata from '../azdata';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
import * as childProcess from '../common/childProcess';
|
import * as childProcess from '../common/childProcess';
|
||||||
@@ -14,11 +12,6 @@ import * as nock from 'nock';
|
|||||||
|
|
||||||
describe('azdata', function () {
|
describe('azdata', function () {
|
||||||
|
|
||||||
let outputChannelMock: TypeMoq.IMock<vscode.OutputChannel>;
|
|
||||||
beforeEach(function (): void {
|
|
||||||
outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
|
|
||||||
|
|
||||||
});
|
|
||||||
afterEach(function (): void {
|
afterEach(function (): void {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
nock.cleanAll();
|
nock.cleanAll();
|
||||||
@@ -33,7 +26,7 @@ describe('azdata', function () {
|
|||||||
}
|
}
|
||||||
// Mock call to --version to simulate azdata being installed
|
// Mock call to --version to simulate azdata being installed
|
||||||
sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve({ stdout: 'v1.0.0', stderr: '' }));
|
sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve({ stdout: 'v1.0.0', stderr: '' }));
|
||||||
await should(azdata.findAzdata(outputChannelMock.object)).not.be.rejected();
|
await should(azdata.findAzdata()).not.be.rejected();
|
||||||
});
|
});
|
||||||
it('unsuccessful', async function (): Promise<void> {
|
it('unsuccessful', async function (): Promise<void> {
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
@@ -43,7 +36,7 @@ describe('azdata', function () {
|
|||||||
// Mock call to executeCommand to simulate azdata --version returning error
|
// Mock call to executeCommand to simulate azdata --version returning error
|
||||||
sinon.stub(childProcess, 'executeCommand').returns(Promise.reject({ stdout: '', stderr: 'command not found: azdata' }));
|
sinon.stub(childProcess, 'executeCommand').returns(Promise.reject({ stdout: '', stderr: 'command not found: azdata' }));
|
||||||
}
|
}
|
||||||
await should(azdata.findAzdata(outputChannelMock.object)).be.rejected();
|
await should(azdata.findAzdata()).be.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -57,7 +50,7 @@ describe('azdata', function () {
|
|||||||
nock(azdata.azdataHostname)
|
nock(azdata.azdataHostname)
|
||||||
.get(`/${azdata.azdataUri}`)
|
.get(`/${azdata.azdataUri}`)
|
||||||
.replyWithFile(200, __filename);
|
.replyWithFile(200, __filename);
|
||||||
const downloadPromise = azdata.downloadAndInstallAzdata(outputChannelMock.object);
|
const downloadPromise = azdata.downloadAndInstallAzdata();
|
||||||
await downloadPromise;
|
await downloadPromise;
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -65,7 +58,7 @@ describe('azdata', function () {
|
|||||||
nock('https://aka.ms')
|
nock('https://aka.ms')
|
||||||
.get('/azdata-msi')
|
.get('/azdata-msi')
|
||||||
.reply(404);
|
.reply(404);
|
||||||
const downloadPromise = azdata.downloadAndInstallAzdata(outputChannelMock.object);
|
const downloadPromise = azdata.downloadAndInstallAzdata();
|
||||||
await should(downloadPromise).be.rejected();
|
await should(downloadPromise).be.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -3,16 +3,14 @@
|
|||||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
import * as vscode from 'vscode';
|
|
||||||
import * as should from 'should';
|
import * as should from 'should';
|
||||||
import * as TypeMoq from 'typemoq';
|
|
||||||
import * as sudo from 'sudo-prompt';
|
import * as sudo from 'sudo-prompt';
|
||||||
import * as sinon from 'sinon';
|
import * as sinon from 'sinon';
|
||||||
|
import Logger from '../../common/logger';
|
||||||
|
|
||||||
import { executeCommand, executeSudoCommand } from '../../common/childProcess';
|
import { executeCommand, executeSudoCommand } from '../../common/childProcess';
|
||||||
|
|
||||||
describe('ChildProcess', function (): void {
|
describe('ChildProcess', function (): void {
|
||||||
const outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
|
|
||||||
|
|
||||||
afterEach(function(): void {
|
afterEach(function(): void {
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
@@ -21,19 +19,20 @@ describe('ChildProcess', function (): void {
|
|||||||
describe('executeCommand', function(): void {
|
describe('executeCommand', function(): void {
|
||||||
[[], ['test']].forEach(args => {
|
[[], ['test']].forEach(args => {
|
||||||
it(`Output channel is used with ${JSON.stringify(args)} args`, async function (): Promise<void> {
|
it(`Output channel is used with ${JSON.stringify(args)} args`, async function (): Promise<void> {
|
||||||
await executeCommand('echo', args, outputChannelMock.object);
|
const logStub = sinon.stub(Logger, 'log');
|
||||||
outputChannelMock.verify(x => x.appendLine(TypeMoq.It.isAny()), TypeMoq.Times.atLeastOnce());
|
await executeCommand('echo', args);
|
||||||
|
should(logStub.called).be.true('Log should have been called');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Gets expected output', async function (): Promise<void> {
|
it('Gets expected output', async function (): Promise<void> {
|
||||||
const echoOutput = 'test';
|
const echoOutput = 'test';
|
||||||
const output = await executeCommand('echo', [echoOutput], outputChannelMock.object);
|
const output = await executeCommand('echo', [echoOutput]);
|
||||||
should(output.stdout).equal(echoOutput);
|
should(output.stdout).equal(echoOutput);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Invalid command errors', async function (): Promise<void> {
|
it('Invalid command errors', async function (): Promise<void> {
|
||||||
await should(executeCommand('invalid_command', [], outputChannelMock.object)).be.rejected();
|
await should(executeCommand('invalid_command', [])).be.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -43,7 +42,7 @@ describe('ChildProcess', function (): void {
|
|||||||
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
||||||
callback!(undefined, stdout);
|
callback!(undefined, stdout);
|
||||||
});
|
});
|
||||||
const result = await executeSudoCommand('echo', outputChannelMock.object);
|
const result = await executeSudoCommand('echo');
|
||||||
should(result.stdout).equal(stdout);
|
should(result.stdout).equal(stdout);
|
||||||
should(result.stderr).equal('');
|
should(result.stderr).equal('');
|
||||||
});
|
});
|
||||||
@@ -53,7 +52,7 @@ describe('ChildProcess', function (): void {
|
|||||||
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
||||||
callback!(undefined, undefined, stderr);
|
callback!(undefined, undefined, stderr);
|
||||||
});
|
});
|
||||||
const result = await executeSudoCommand('echo', outputChannelMock.object);
|
const result = await executeSudoCommand('echo');
|
||||||
should(result.stdout).equal('');
|
should(result.stdout).equal('');
|
||||||
should(result.stderr).equal(stderr);
|
should(result.stderr).equal(stderr);
|
||||||
});
|
});
|
||||||
@@ -62,7 +61,7 @@ describe('ChildProcess', function (): void {
|
|||||||
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
||||||
callback!(new Error('error'));
|
callback!(new Error('error'));
|
||||||
});
|
});
|
||||||
await should(executeSudoCommand('invalid_command', outputChannelMock.object)).be.rejected();
|
await should(executeSudoCommand('invalid_command')).be.rejected();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,7 @@
|
|||||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
|
|
||||||
import * as vscode from 'vscode';
|
|
||||||
import * as should from 'should';
|
import * as should from 'should';
|
||||||
import * as TypeMoq from 'typemoq';
|
|
||||||
import { HttpClient } from '../../common/httpClient';
|
import { HttpClient } from '../../common/httpClient';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
@@ -16,12 +14,6 @@ import { Deferred } from '../../common/promise';
|
|||||||
|
|
||||||
describe('HttpClient', function (): void {
|
describe('HttpClient', function (): void {
|
||||||
|
|
||||||
let outputChannelMock: TypeMoq.IMock<vscode.OutputChannel>;
|
|
||||||
|
|
||||||
before(function (): void {
|
|
||||||
outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
|
|
||||||
});
|
|
||||||
|
|
||||||
afterEach(function (): void {
|
afterEach(function (): void {
|
||||||
nock.cleanAll();
|
nock.cleanAll();
|
||||||
nock.enableNetConnect();
|
nock.enableNetConnect();
|
||||||
@@ -33,7 +25,7 @@ describe('HttpClient', function (): void {
|
|||||||
.get('/README.md')
|
.get('/README.md')
|
||||||
.replyWithFile(200, __filename);
|
.replyWithFile(200, __filename);
|
||||||
const downloadFolder = os.tmpdir();
|
const downloadFolder = os.tmpdir();
|
||||||
const downloadPath = await HttpClient.download('https://127.0.0.1/README.md', downloadFolder, outputChannelMock.object);
|
const downloadPath = await HttpClient.download('https://127.0.0.1/README.md', downloadFolder);
|
||||||
// Verify file was downloaded correctly
|
// Verify file was downloaded correctly
|
||||||
await fs.promises.stat(downloadPath);
|
await fs.promises.stat(downloadPath);
|
||||||
});
|
});
|
||||||
@@ -43,7 +35,7 @@ describe('HttpClient', function (): void {
|
|||||||
nock('https://127.0.0.1')
|
nock('https://127.0.0.1')
|
||||||
.get('/')
|
.get('/')
|
||||||
.replyWithError('Unexpected Error');
|
.replyWithError('Unexpected Error');
|
||||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
|
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder);
|
||||||
|
|
||||||
await should(downloadPromise).be.rejected();
|
await should(downloadPromise).be.rejected();
|
||||||
});
|
});
|
||||||
@@ -53,7 +45,7 @@ describe('HttpClient', function (): void {
|
|||||||
nock('https://127.0.0.1')
|
nock('https://127.0.0.1')
|
||||||
.get('/')
|
.get('/')
|
||||||
.reply(404, '');
|
.reply(404, '');
|
||||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
|
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder);
|
||||||
|
|
||||||
await should(downloadPromise).be.rejected();
|
await should(downloadPromise).be.rejected();
|
||||||
});
|
});
|
||||||
@@ -69,7 +61,7 @@ describe('HttpClient', function (): void {
|
|||||||
nock('https://127.0.0.1')
|
nock('https://127.0.0.1')
|
||||||
.get('/')
|
.get('/')
|
||||||
.reply(200, '');
|
.reply(200, '');
|
||||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
|
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder);
|
||||||
// Wait for the stream to be created before throwing the error or HttpClient will miss the event
|
// Wait for the stream to be created before throwing the error or HttpClient will miss the event
|
||||||
await deferredPromise;
|
await deferredPromise;
|
||||||
try {
|
try {
|
||||||
@@ -87,7 +79,7 @@ describe('HttpClient', function (): void {
|
|||||||
nock('https://127.0.0.1')
|
nock('https://127.0.0.1')
|
||||||
.get(`/${filename}`)
|
.get(`/${filename}`)
|
||||||
.reply(200);
|
.reply(200);
|
||||||
const receivedFilename = await HttpClient.getFilename(`https://127.0.0.1/${filename}`, outputChannelMock.object);
|
const receivedFilename = await HttpClient.getFilename(`https://127.0.0.1/${filename}`);
|
||||||
|
|
||||||
should(receivedFilename).equal(filename);
|
should(receivedFilename).equal(filename);
|
||||||
});
|
});
|
||||||
@@ -96,7 +88,7 @@ describe('HttpClient', function (): void {
|
|||||||
nock('https://127.0.0.1')
|
nock('https://127.0.0.1')
|
||||||
.get('/')
|
.get('/')
|
||||||
.replyWithError('Unexpected Error');
|
.replyWithError('Unexpected Error');
|
||||||
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1', outputChannelMock.object);
|
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1');
|
||||||
|
|
||||||
await should(getFilenamePromise).be.rejected();
|
await should(getFilenamePromise).be.rejected();
|
||||||
});
|
});
|
||||||
@@ -105,7 +97,7 @@ describe('HttpClient', function (): void {
|
|||||||
nock('https://127.0.0.1')
|
nock('https://127.0.0.1')
|
||||||
.get('/')
|
.get('/')
|
||||||
.reply(404, '');
|
.reply(404, '');
|
||||||
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1', outputChannelMock.object);
|
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1');
|
||||||
|
|
||||||
await should(getFilenamePromise).be.rejected();
|
await should(getFilenamePromise).be.rejected();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user