Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): add tasks support #853

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,21 @@ export async function set (params) {
const { ownerId, appId } = await Application.resolveId(appIdOrName, alias);
const config = ApplicationConfiguration.getById(configurationName);

let result = null;
if (config != null) {
const app = await application.update({ id: ownerId, appId }, { [config.name]: ApplicationConfiguration.parse(config, configurationValue) }).then(sendToApi);

ApplicationConfiguration.printById(app, configurationName);
if (configurationName === 'task') {
const upperValue = configurationValue.toUpperCase();
if (upperValue === 'TASK' || upperValue === 'REGULAR') {
result = await application.update({ id: ownerId, appId }, { instanceLifetime: upperValue }).then(sendToApi);
}
else {
throw new Error('Invalid value for task configuration: must be TASK or REGULAR');
}
}
else {
result = await application.update({ id: ownerId, appId }, { [config.name]: ApplicationConfiguration.parse(config, configurationValue) }).then(sendToApi);
}
ApplicationConfiguration.printById(result, configurationName);
}
}

Expand All @@ -41,6 +52,15 @@ export async function update (params) {
throw new Error('No configuration to update');
}

const { task } = options;
if (task != null) {
const result = await application.update({ id: ownerId, appId }, { instanceLifetime: task }).then(sendToApi);

// We delete the task key to avoid conflicts
delete options.task;
ApplicationConfiguration.printById(result, 'task');
}

const app = await application.update({ id: ownerId, appId }, options).then(sendToApi);

for (const configName of Object.keys(options)) {
Expand Down
31 changes: 28 additions & 3 deletions src/models/application_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const CONFIG_KEYS = [
{ id: 'sticky-sessions', name: 'stickySessions', displayName: 'Sticky sessions', kind: 'bool' },
{ id: 'cancel-on-push', name: 'cancelOnPush', displayName: 'Cancel current deployment on push', kind: 'bool' },
{ id: 'force-https', name: 'forceHttps', displayName: 'Force redirection of HTTP to HTTPS', kind: 'force-https' },
{ id: 'task', name: 'task', displayName: 'Deploy an application as a Clever Task', kind: 'task' },
];

export function listAvailableIds () {
Expand All @@ -36,6 +37,9 @@ function display (config, value) {
case 'force-https': {
return value.toLowerCase();
}
case 'task': {
return (value === 'TASK') ? 'enabled' : 'disabled';
}
default: {
return String(value);
}
Expand Down Expand Up @@ -69,7 +73,8 @@ function getConfigOptions (config) {
switch (config.kind) {
case 'bool':
case 'inverted-bool':
case 'force-https': {
case 'force-https':
case 'task': {
return [
cliparse.flag(`enable-${config.id}`, { description: `Enable ${config.id}` }),
cliparse.flag(`disable-${config.id}`, { description: `Disable ${config.id}` }),
Expand Down Expand Up @@ -126,6 +131,18 @@ function parseConfigOption (config, options) {
}
return null;
}
case 'task': {
const enable = options[`enable-${config.id}`];
const disable = options[`disable-${config.id}`];
if (enable && disable) {
Logger.warn(`${config.id} is both enabled and disabled, ignoring`);
}
else if (enable || disable) {
const value = (enable) ? 'TASK' : 'REGULAR';
return [config.name, value];
}
return null;
}
default: {
if (options[config.id] !== null) {
return [config.name, options[config.id]];
Expand All @@ -136,15 +153,23 @@ function parseConfigOption (config, options) {
}

function printConfig (app, config) {
if (app[config.name] != null) {
if (config.name === 'task') {
Logger.println(`${config.displayName}: ${colors.bold(display(config, app.instance.lifetime))}`);
}
else if (app[config.name] != null) {
Logger.println(`${config.displayName}: ${colors.bold(display(config, app[config.name]))}`);
}
}

export function printById (app, id) {
const config = getById(id);
if (config != null) {
printConfig(app, config);
if (config.name === 'task') {
Logger.println(`${config.displayName}: ${colors.bold(display(config, app.instance.lifetime))}`);
}
else {
printConfig(app, config);
}
}
}

Expand Down