@hotglue/cli
Version:
hotglue CLI tools
188 lines (160 loc) • 5.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handler = exports.desc = exports.command = exports.builder = void 0;
var _debug = _interopRequireDefault(require("../../helpers/debug.js"));
var _ora = _interopRequireDefault(require("ora"));
var _cliTable = _interopRequireDefault(require("cli-table"));
var _descriptions = _interopRequireDefault(require("../../helpers/descriptions.js"));
var _print = require("../../helpers/print.js");
var _api = require("../../helpers/api.js");
var _utils = require("../../helpers/utils.js");
var _fs = require("fs");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const debug = (0, _debug.default)('commands:tenants:update-config');
const command = 'update-config';
exports.command = command;
const desc = 'Update the config of a specific connector and flow for all tenants';
exports.desc = desc;
const builder = async yargs => {
debug('builder', command);
return yargs.option('flow', _descriptions.default.options['flow'].config).demandOption('flow', _descriptions.default.options['flow'].demandText).option('connector', _descriptions.default.options['connector'].config).demandOption('connector', _descriptions.default.options['connector'].demandText).option('configFilePath', _descriptions.default.options['configFilePath'].config).demandOption('configFilePath', _descriptions.default.options['configFilePath'].demandText);
};
exports.builder = builder;
const processTenant = async ({
debug,
baseUri,
apikey,
env,
flow,
tenant,
connectorId,
config,
isV2Flow
}) => {
const lookupKey = isV2Flow ? 'id' : 'tap'; // Get all linked connectors/sources of the tuple (env, flow, tenant)
const linkedConnectors = isV2Flow ? await (0, _api.getLinkedConnectors)({
debug,
baseUri,
env,
flow,
tenant,
apikey,
config: true
}) : await (0, _api.getLinkedSources)({
debug,
baseUri,
env,
flow,
tenant,
apikey,
config: true
});
const connector = linkedConnectors.find(c => c[lookupKey] === connectorId);
if (!connector) return null;
const patchFunction = isV2Flow ? _api.patchLinkedConnectorConfig : _api.patchLinkedSourceConfig;
try {
await patchFunction({
debug,
baseUri,
env,
flow,
tenant,
apikey,
connectorId,
config
});
} catch (e) {
console.log((0, _print.themed)(`Error patching: ${tenant}. ${e}`, 'secondary'));
}
return tenant;
};
const handler = async argv => {
debug('handler', command, argv);
const {
hg,
json,
apikey,
env,
flow,
connector,
configFilePath
} = argv;
let message;
let spinner = (0, _ora.default)();
try {
if (!configFilePath) {
throw new Error('Config file path not provided');
}
if (!configFilePath.endsWith('.json')) {
throw new Error('Config file must have .json extension');
}
if (!(0, _fs.existsSync)(configFilePath)) {
throw new Error('Config file not found');
}
const config = JSON.parse((0, _fs.readFileSync)(configFilePath, {
encoding: 'utf-8'
}));
message = (0, _print.themed)(`Retrieving tenants for environment ${(0, _print.themed)(env, 'info')}`);
!json && spinner.start((0, _print.themed)(`In progress: ${message}...`, 'secondary'));
const [allTenants, supportedFlow] = await Promise.all([(0, _api.getTenants)({
debug,
baseUri: hg.clientApiBaseUri,
apikey,
env
}), (0, _api.getSupportedFlow)({
debug,
baseUri: hg.clientApiBaseUri,
apikey,
env,
flow
})]);
const isV2Flow = (supportedFlow === null || supportedFlow === void 0 ? void 0 : supportedFlow.version) === 2;
!json && spinner.succeed((0, _print.themed)(`Finished: ${message}.`, 'secondary'));
const tenantsUpdated = [];
message = (0, _print.themed)(`Updating config for connector ${(0, _print.themed)(connector, 'info')} and flow ${(0, _print.themed)(flow, 'info')}`);
!json && spinner.start((0, _print.themed)(`In progress: ${message}...`, 'secondary')); // Break the tenants array in to an array of arrays so we can make
// parallel calls and make it faster
for (const tenants of (0, _utils.buildChunks)(allTenants)) {
const maybeTenantsUpdated = await Promise.all(tenants.map(tenant => processTenant({
debug,
baseUri: hg.clientApiBaseUri,
apikey,
env,
flow,
tenant,
connectorId: connector,
config,
isV2Flow
})));
tenantsUpdated.push(...maybeTenantsUpdated.filter(Boolean));
}
!json && spinner.succeed((0, _print.themed)(`Finished: ${message}.`, 'secondary'));
if (json) {
(0, _print.printJSON)(tenantsUpdated);
} else {
// generate results table
const table = new _cliTable.default({
head: ['Tenant ID']
});
tenantsUpdated.forEach(t => table.push([t])); // print results
console.log(table.toString());
}
} catch (err) {
if (json) {
(0, _print.printJSON)({
status: 'error',
error: err
});
} else {
spinner.fail((0, _print.themed)(`Error: ${message}.`, 'secondary'));
(0, _print.pr)((0, _print.themed)(`Message: ${(0, _print.themed)(err.message)}`, 'secondary'));
debug(err);
if (err && err.response && err.response.data) {
debug('response', err.response.data);
}
}
}
};
exports.handler = handler;