@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
91 lines (90 loc) • 3.62 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 DownloadKeycloak extends CommandBase {
constructor() {
super(...arguments);
this.url = 'https://ops.adinsure.com/api/storage/extensions/';
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
downloadFile(version) {
return __awaiter(this, void 0, void 0, function* () {
return axios.get(`${this.url}/keycloak-scim2-outbound-provisioning-${version}.zip`, {
headers: {
Authorization: 'Bearer ' + (yield this.security.getToken()),
'Content-Type': 'application/octet-stream',
},
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(DownloadKeycloak);
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 = `Keycloak-scim-${flags.version}.zip`;
const filePath = path.resolve(basePath, filename);
ux.action.start(`Downloading Keycloack version ${flags.version} to ${filePath}`, undefined, { stdout: true });
try {
const file = yield this.downloadFile(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".', { exit: 1 });
}
default: {
this.error(error.message);
}
}
}
});
}
}
DownloadKeycloak.description = 'download Keycloak-scim from Azure storage';
DownloadKeycloak.usage = 'download:keycloak-scim <flags>';
DownloadKeycloak.examples = [
`$ ops download:keycloak-scim -v 3.2.0 -o ~/Downloads`,
];
DownloadKeycloak.flags = {
version: Flags.string({
char: 'v',
description: 'version we want to download',
required: true,
}),
output: Flags.string({
char: 'o',
description: 'output directory',
}),
};
export default DownloadKeycloak;