@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
132 lines (131 loc) • 5.52 kB
JavaScript
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 DownloadPlatform 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 Platform zip by specified version
* @param {string} version The version of AdInsure Platform
* @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-${version}.zip`, {
headers: {
Authorization: 'Bearer ' + (yield this.security.getToken()),
'Content-Type': 'application/octet-stream',
},
responseType: 'arraybuffer',
});
});
}
/**
* Get unique AdInsure Platform zip by specified version
* @param {string} version The version of AdInsure Platform
* @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';
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(DownloadPlatform);
let outputDir = flags.output;
// If the user didn't change the output (default is './'), try to get the default from config.
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-${flags.version}.zip`;
const filePath = path.resolve(basePath, filename);
ux.action.start(`Downloading AdInsure Platform 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);
}
}
}
});
}
}
DownloadPlatform.aliases = ['download'];
DownloadPlatform.description = 'download AdInsure Platform bundle containing zip of Client, Server and IdentityServer from azure storage';
DownloadPlatform.usage = 'download <flags>';
DownloadPlatform.examples = [
`$ ops download -v 6.0.0 -o ~/Downloads`,
];
DownloadPlatform.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 DownloadPlatform;