From 9233d78c2fff932d094ff2ddeef25a2c7fc735bf Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 22 May 2018 14:49:20 -0400 Subject: [PATCH] update command --- src/commands/update.command.ts | 62 ++++++++++++++++++++++++++ src/models/response/messageResponse.ts | 1 + src/program.ts | 14 ++++-- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 src/commands/update.command.ts diff --git a/src/commands/update.command.ts b/src/commands/update.command.ts new file mode 100644 index 0000000..3d7a0ff --- /dev/null +++ b/src/commands/update.command.ts @@ -0,0 +1,62 @@ +import * as program from 'commander'; +import * as fetch from 'node-fetch'; + +import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service'; + +import { Response } from '../models/response'; +import { MessageResponse } from '../models/response/messageResponse'; + +export class UpdateCommand { + constructor(private platformUtilsService: PlatformUtilsService) { } + + async run(cmd: program.Command): Promise { + const currentVersion = this.platformUtilsService.getApplicationVersion(); + + const response = await fetch.default('https://api.github.com/repos/bitwarden/cli/releases/latest'); + if (response.status === 200) { + const responseJson = await response.json(); + const res = new MessageResponse(null, null); + + const tagName: string = responseJson.tag_name; + if (tagName === ('v' + currentVersion)) { + res.title = 'No update available.'; + res.noColor = true; + return Response.success(res); + } + + let downloadUrl: string = null; + if (responseJson.assets != null) { + for (const a of responseJson.assets) { + const download: string = a.browser_download_url; + if (download == null) { + continue; + } + + if (process.platform === 'win32' && download.indexOf('bw-windows.zip') > -1) { + downloadUrl = download; + break; + } else if (process.platform === 'darwin' && download.indexOf('bw-macos.zip') > -1) { + downloadUrl = download; + break; + } else if (process.platform === 'linux' && download.indexOf('bw-linux.zip') > -1) { + downloadUrl = download; + break; + } + } + } + + res.title = 'A new version is available: ' + tagName; + if (downloadUrl == null) { + downloadUrl = 'https://github.com/bitwarden/cli/releases'; + } else { + res.raw = downloadUrl; + } + res.message = 'You can download this update at: ' + downloadUrl + '\n' + + 'If you installed this CLI through a package manager ' + + 'you should probably update using its update command instead.'; + return Response.success(res); + } else { + return Response.error('Error contacting update API: ' + response.status); + } + } +} diff --git a/src/models/response/messageResponse.ts b/src/models/response/messageResponse.ts index 50e2805..448e3db 100644 --- a/src/models/response/messageResponse.ts +++ b/src/models/response/messageResponse.ts @@ -5,6 +5,7 @@ export class MessageResponse implements BaseResponse { title: string; message: string; raw: string; + noColor = false; constructor(title: string, message: string) { this.object = 'message'; diff --git a/src/program.ts b/src/program.ts index 5aee535..13ea37d 100644 --- a/src/program.ts +++ b/src/program.ts @@ -17,6 +17,7 @@ import { LoginCommand } from './commands/login.command'; import { LogoutCommand } from './commands/logout.command'; import { SyncCommand } from './commands/sync.command'; import { UnlockCommand } from './commands/unlock.command'; +import { UpdateCommand } from './commands/update.command'; import { Response } from './models/response'; import { ListResponse } from './models/response/listResponse'; @@ -470,15 +471,18 @@ export class Program { writeLn(''); writeLn(' Returns the URL to download the newest version of this CLI tool.'); writeLn(''); - writeLn(' Returns nothing if no update is available.'); + writeLn(' Use the `--raw` option to return only the download URL for the update.'); writeLn(''); writeLn(' Examples:'); writeLn(''); writeLn(' bw update'); + writeLn(' bw update --raw'); writeLn('', true); }) .action(async (object, id, cmd) => { - // TODO + const command = new UpdateCommand(this.main.platformUtilsService); + const response = await command.run(cmd); + this.processResponse(response); }); program @@ -544,7 +548,11 @@ export class Program { let out: string = ''; if (message.title != null) { - out = chalk.greenBright(message.title); + if (message.noColor) { + out = message.title; + } else { + out = chalk.greenBright(message.title); + } } if (message.message != null) { if (message.title != null) {