heroku-kafka
Version:
heroku plugin to manage heroku kafka
137 lines (136 loc) • 4.45 kB
JavaScript
import { ux } from '@oclif/core';
function deprecated(cmd, newCmd, newTopic) {
return async function (context, heroku) {
const newName = `${newTopic || context.command.topic}:${newCmd}`;
ux.warn(`\nWARNING: ${context.command.topic}:${context.command.command} is deprecated; please use ${newName}\n`);
return await cmd(context, heroku);
};
}
function clusterConfig(attachment, config) {
if (!attachment) {
throw new Error('Could not find add-on');
}
const findVar = (suffix, required = true) => {
const configKey = attachment.config_vars.find(v => v.endsWith('_' + suffix));
if (!configKey) {
if (required) {
throw new Error(`Could not find ${suffix} for ${attachment.name} on ${attachment.app.name}`);
}
else {
return undefined;
}
}
const configVal = config[configKey];
if (!configVal && required) {
throw new Error(`Config value ${suffix} for ${attachment.name} on ${attachment.app.name} is empty`);
}
return configVal;
};
return {
url: findVar('URL'),
trustedCert: findVar('TRUSTED_CERT'),
clientCert: findVar('CLIENT_CERT'),
clientCertKey: findVar('CLIENT_CERT_KEY'),
prefix: findVar('PREFIX', false),
};
}
function isZookeeperAllowed(addon) {
return addon.plan.name.includes('private-');
}
function parseBool(boolStr) {
switch (boolStr) {
case 'yes':
case 'true':
case 'on':
case 'enable': {
return true;
}
case 'no':
case 'false':
case 'off':
case 'disable': {
return false;
}
}
}
function parseDuration(durationStr) {
if (/^\d+$/.test(durationStr)) {
return Number.parseInt(durationStr, 10);
}
const result = durationStr.match(/^(\d+) ?(ms|[smhd]|milliseconds?|seconds?|minutes?|hours?|days?)$/);
if (result) {
const magnitude = Number.parseInt(result[1]);
const unit = result[2];
let multiplier = 1;
switch (unit) {
case 'ms':
case 'millisecond':
case 'milliseconds': {
multiplier = 1;
break;
}
case 's':
case 'second':
case 'seconds': {
multiplier = 1000;
break;
}
case 'm':
case 'minute':
case 'minutes': {
multiplier = 1000 * 60;
break;
}
case 'h':
case 'hour':
case 'hours': {
multiplier = 1000 * 60 * 60;
break;
}
case 'd':
case 'day':
case 'days': {
multiplier = 1000 * 60 * 60 * 24;
break;
}
default: {
return null;
}
}
return magnitude * multiplier;
}
return null;
}
function formatIntervalFromMilliseconds(milliseconds) {
let remaining = milliseconds;
const multipliers = {
day: (24 * 60 * 60 * 1000),
hour: (60 * 60 * 1000),
minute: (60 * 1000),
second: 1000,
};
const intervals = Object.keys(multipliers)
.sort((intervalA, intervalB) => multipliers[intervalB] - multipliers[intervalA]);
const values = intervals.reduce((accum, interval) => {
accum[interval] = 0;
return accum;
}, {});
const smallest = multipliers[intervals[intervals.length - 1]];
while (remaining >= smallest) {
const nextInterval = intervals.find(interval => multipliers[interval] <= remaining);
const nextIntervalValue = multipliers[nextInterval];
let multiplier = 1;
while ((nextIntervalValue * (multiplier + 1)) <= remaining) {
multiplier += 1;
}
values[nextInterval] = multiplier;
remaining -= nextIntervalValue * multiplier;
}
values.millisecond = remaining;
intervals.push('millisecond');
return intervals.filter(interval => values[interval] > 0).map(interval => {
const val = values[interval];
return `${val} ${interval}${val > 1 ? 's' : ''}`;
}).join(' ');
}
export { clusterConfig, deprecated, formatIntervalFromMilliseconds, isZookeeperAllowed, parseBool, parseDuration, };