UNPKG

@adinsure-ops/ops-cli

Version:

Operations CLI for working with AdInsure

131 lines (130 loc) 5.37 kB
import { __awaiter } from "tslib"; import { Flags, ux } from '@oclif/core'; import axios from 'axios'; import chalk from 'chalk'; import fs from 'fs-extra'; import os from 'os'; import path from 'path'; import CommandBase from '../../command.base.js'; import OpsConfig from '../../lib/config.js'; class DownloadClient extends CommandBase { constructor() { super(...arguments); this.url_azure = 'https://ops.adinsure.com/api/storage/binaries/'; this.url_gitlabru = 'https://gitlabru.adacta-fintech.ru/api/v4/projects/2/packages/generic'; } /** * Get unique AdInsure Client zip by specified version * @param {string} version The version of AdInsure Client * @returns {Promise<AxiosResponse<any, any>>} */ // eslint-disable-next-line @typescript-eslint/no-explicit-any downloadFileFromAzure(version) { return __awaiter(this, void 0, void 0, function* () { return axios.get(`${this.url_azure}/AdInsure-client-${version}.zip`, { headers: { Authorization: 'Bearer ' + (yield this.security.getToken()), 'Content-Type': 'application/octet-stream', }, responseType: 'arraybuffer', }); }); } /** * Get unique AdInsure Client zip by specified version * @param {string} version The version of AdInsure Client * @returns {Promise<AxiosResponse<any, any>>} */ //eslint-disable-next-line @typescript-eslint/no-explicit-any downloadFileFromGitLabRu(version) { return __awaiter(this, void 0, void 0, function* () { const packageName = 'AdInsure-client'; const fileName = `${packageName}-${version}.zip`; return axios.get(`${this.url_gitlabru}/${packageName}/${version}/${fileName}`, { headers: { 'PRIVATE-TOKEN': this.security.getTokenRu(), }, responseType: 'arraybuffer', }); }); } run() { const _super = Object.create(null, { run: { get: () => super.run } }); return __awaiter(this, void 0, void 0, function* () { _super.run.call(this); const { flags } = yield this.parse(DownloadClient); let outputDir = flags.output; if (!outputDir) { // If the user didn't provide the output flag, try to get the default from config. const config = new OpsConfig(this.security.getConfigPath()); const defaultFolder = config.get('download_folder'); outputDir = defaultFolder || './'; // no flag -> DEFAULT output is "./" } // Expand tilde if present const basePath = outputDir.charAt(0) === '~' ? os.homedir() + outputDir.split('~')[1] : outputDir; const filename = `AdInsure-client-${flags.version}.zip`; const filePath = path.resolve(basePath, filename); ux.action.start(`Downloading AdInsure Client version ${flags.version} to ${filePath}`, undefined, { stdout: true }); try { var file; if (!flags.source) { const config = new OpsConfig(this.security.getConfigPath()); flags.source = config.get('endpoint') || 'azure'; // no flag -> DEFAULT source is azure } if (flags.source === 'azure') { file = yield this.downloadFileFromAzure(flags.version); } else { file = yield this.downloadFileFromGitLabRu(flags.version); } const writeStream = fs.createWriteStream(filePath); writeStream.write(file.data, 'binary'); writeStream.on('finish', () => { ux.action.stop(`${chalk.green('OK')}`); }); writeStream.end(); // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error) { switch (error.statusCode) { case 404: { this.error('Does not exist.', { exit: 1 }); } case 400: { this.error('Try refreshing your login credentials with "ops login" or "ops login:ru".', { exit: 1 }); } default: { this.error(error.message); } } } }); } } DownloadClient.aliases = ['download:client']; DownloadClient.description = 'download AdInsure Client zip from Azure storage'; DownloadClient.usage = 'download:client <flags>'; DownloadClient.examples = [ `$ ops download:client -v 3.2.0 -o ~/Downloads`, ]; DownloadClient.flags = { source: Flags.string({ char: 's', description: '[default: azure] artifact source, configurable with config:set endpoint <option>', options: ['azure', 'gitlabru'], }), version: Flags.string({ char: 'v', description: 'version we want to download', required: true, }), output: Flags.string({ char: 'o', description: 'output directory', }), }; export default DownloadClient;