Skip to content

Commit

Permalink
feat(config): add tasks support
Browse files Browse the repository at this point in the history
  • Loading branch information
davlgd committed Dec 2, 2024
1 parent 4a2b19d commit 20f9910
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
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

0 comments on commit 20f9910

Please sign in to comment.