Skip to content

Commit

Permalink
feat(addon-create): add support for JSON format (-F json)
Browse files Browse the repository at this point in the history
  • Loading branch information
hsablonniere committed Feb 16, 2024
1 parent 1f7842a commit 334cec5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
2 changes: 1 addition & 1 deletion bin/clever.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ function run () {
const addonCreateCommand = cliparse.command('create', {
description: 'Create an addon',
args: [args.addonProvider, args.addonName],
options: [opts.linkAddon, opts.confirmAddonCreation, opts.addonPlan, opts.addonRegion, opts.addonVersion, opts.addonOptions],
options: [opts.linkAddon, opts.confirmAddonCreation, opts.addonPlan, opts.addonRegion, opts.addonVersion, opts.addonOptions, opts.humanJsonOutputFormat],
}, addon('create'));
const addonDeleteCommand = cliparse.command('delete', {
description: 'Delete an addon',
Expand Down
67 changes: 46 additions & 21 deletions src/commands/addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,69 @@ async function list (params) {

async function create (params) {
const [providerName, name] = params.args;
const { link: linkedAppAlias, plan: planName, region, yes: skipConfirmation, org: orgaIdOrName } = params.options;
const {
link: linkedAppAlias,
plan: planName,
region,
yes: skipConfirmation,
org: orgaIdOrName,
format,
} = params.options;
const version = params.options['addon-version'];
const addonOptions = parseAddonOptions(params.options.option);

const ownerId = (orgaIdOrName != null)
? await Organisation.getId(orgaIdOrName)
: await User.getCurrentId();

const addonToCreate = {
ownerId,
name,
providerName,
planName,
region,
skipConfirmation,
version,
addonOptions,
};

if (linkedAppAlias != null) {
const linkedAppData = await AppConfig.getAppDetails({ alias: linkedAppAlias });
if (orgaIdOrName != null && linkedAppData.ownerId !== ownerId) {
if (orgaIdOrName != null && linkedAppData.ownerId !== ownerId && format === 'human') {
Logger.warn('The specified application does not belong to the specified organisation. Ignoring the `--org` option');
}
const newAddon = await Addon.create({
...addonToCreate,
ownerId: linkedAppData.ownerId,
name,
providerName,
planName,
region,
skipConfirmation,
version,
addonOptions,
});
await Addon.link(linkedAppData.ownerId, linkedAppData.appId, { addon_id: newAddon.id });
Logger.println(`Addon ${name} (id: ${newAddon.id}) successfully created and linked to the application`);
displayAddon(format, newAddon, `Add-on created and linked to application ${linkedAppAlias} successfully!`);
}
else {
const newAddon = await Addon.create({
ownerId,
name,
providerName,
planName,
region,
skipConfirmation,
version,
addonOptions,
});
Logger.println(`Addon ${name} (id: ${newAddon.id}) successfully created`);
const newAddon = await Addon.create(addonToCreate);
displayAddon(format, newAddon, 'Add-on created successfully!');
}
}

function displayAddon (format, addon, message) {
switch (format) {

case 'json': {
Logger.printJson({
id: addon.id,
name: addon.name,
realId: addon.realId,
});
break;
}

case 'human':
default:
Logger.println([
message,
`ID: ${addon.id}`,
`Real ID: ${addon.realId}`,
].join('\n'));
}
}

Expand Down

0 comments on commit 334cec5

Please sign in to comment.