Changes to discover and perform azdata update (#11906)

* WIP

* first version with working tests

* fixes needed after merge from main

* Linux untest changes and merge from other changes from mac

* after testing getTextContent

* rename 2 methods

* linux discovery

* tested code on linux

* using release.json for update discovery on linux

* comment added

* dead code removed

* coomments

* revert unrelated change

* revert testing changes

* PR feedback

* remove SendOutputChannelToConsole

* cleanup

* pr feedback

* PR Feedback

* pr feedback

* pr feedback

* merge from main

* merge from main

* cleanup and pr feedback

* pr feedback

* pr feedback.

* pr feedback

Co-authored-by: chgagnon <chgagnon@microsoft.com>
This commit is contained in:
Arvind Ranasaria
2020-08-27 13:25:54 -07:00
committed by GitHub
parent b715e6ed82
commit 00c7600b05
10 changed files with 521 additions and 148 deletions

View File

@@ -17,13 +17,34 @@ export namespace HttpClient {
* 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 targetFolder The folder to download the file to
* @returns Full path to the downloaded file
* @returns a promise to a full path to the downloaded file
*/
export function download(downloadUrl: string, targetFolder: string): Promise<string> {
export function downloadFile(downloadUrl: string, targetFolder: string): Promise<string> {
return download(downloadUrl, targetFolder);
}
/**
* Downloads the text contents of the document at the given URL, resolving to a string containing the text when complete
* @param url The URL of the document whose contents need to be fetched
* @returns a promise to a string that has the contents of document at the provided url
*/
export async function getTextContent(url: string): Promise<string> {
Logger.log(loc.gettingTextContentsOfUrl(url));
return await download(url);
}
/**
* Gets a file/fileContents at the given URL.
* @param downloadUrl The URL to download the file from
* @param targetFolder The folder to download the file to. If not defined then return value is the contents of the downloaded file.
* @returns Full path to the downloaded file or the contents of the file at the given downloadUrl
*/
function download(downloadUrl: string, targetFolder?: string): Promise<string> {
return new Promise((resolve, reject) => {
let totalMegaBytes: number | undefined = undefined;
let receivedBytes = 0;
let printThreshold = 0.1;
let strings: string[] = [];
let downloadRequest = request.get(downloadUrl, { timeout: DownloadTimeout })
.on('error', downloadError => {
Logger.log(loc.downloadError);
@@ -34,28 +55,34 @@ export namespace HttpClient {
if (response.statusCode !== 200) {
Logger.log(loc.downloadError);
Logger.log(response.statusMessage);
Logger.log(`response code: ${response.statusCode}`);
return reject(response.statusMessage);
}
const filename = path.basename(response.request.path);
const targetPath = path.join(targetFolder, filename);
Logger.log(loc.downloadingTo(filename, targetPath));
// Wait to create the WriteStream until here so we can use the actual
// filename based off of the URI.
downloadRequest.pipe(fs.createWriteStream(targetPath))
.on('close', async () => {
Logger.log(loc.downloadFinished);
resolve(targetPath);
})
.on('error', (downloadError) => {
reject(downloadError);
downloadRequest.abort();
});
if (targetFolder !== undefined) {
const filename = path.basename(response.request.path);
const targetPath = path.join(targetFolder, filename);
Logger.log(loc.downloadingTo(filename, targetPath));
// Wait to create the WriteStream until here so we can use the actual
// filename based off of the URI.
downloadRequest.pipe(fs.createWriteStream(targetPath))
.on('close', async () => {
Logger.log(loc.downloadFinished);
resolve(targetPath);
})
.on('error', (downloadError) => {
reject(downloadError);
downloadRequest.abort();
});
}
let contentLength = response.headers['content-length'];
let totalBytes = parseInt(contentLength || '0');
totalMegaBytes = totalBytes / (1024 * 1024);
Logger.log(loc.downloadingProgressMb('0', totalMegaBytes.toFixed(2)));
})
.on('data', (data) => {
if (targetFolder === undefined) {
strings.push(data.toString('utf-8'));
}
receivedBytes += data.length;
if (totalMegaBytes) {
let receivedMegaBytes = receivedBytes / (1024 * 1024);
@@ -65,30 +92,13 @@ export namespace HttpClient {
printThreshold += 0.1;
}
}
});
});
}
/**
* Gets the filename for the specified URL - following redirects as needed
* @param url The URL to get the filename of
*/
export async function getFilename(url: string): Promise<string> {
Logger.log(loc.gettingFilenameOfUrl(url));
return new Promise((resolve, reject) => {
let httpRequest = request.get(url, { timeout: DownloadTimeout })
.on('error', downloadError => {
reject(downloadError);
})
.on('response', (response) => {
if (response.statusCode !== 200) {
return reject(response.statusMessage);
.on('close', async () => {
if (targetFolder === undefined) {
Logger.log(loc.downloadFinished);
resolve(strings.join(''));
}
// We don't want to actually download the file so abort the request now
httpRequest.abort();
const filename = path.basename(response.request.path);
Logger.log(loc.gotFilenameOfUrl(response.request.path, filename));
resolve(filename);
});
});
}

View File

@@ -13,3 +13,11 @@ export function searchForCmd(exe: string): Promise<string> {
// Note : This is separated out to allow for easy test stubbing
return new Promise<string>((resolve, reject) => which(exe, (err, path) => err ? reject(err) : resolve(path)));
}
/**
* Gets the message to display for a given error object that may be a variety of types.
* @param error The error object
*/
export function getErrorMessage(error: any): string {
return error.message ?? error;
}