UNPKG

balena-cli

Version:

The official balena Command Line Interface

86 lines (83 loc) 3.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const ca = require("../../utils/common-args"); const lazy_1 = require("../../utils/lazy"); const messages_1 = require("../../utils/messages"); class FleetRenameCmd extends core_1.Command { async run() { const { args: params } = await this.parse(FleetRenameCmd); const { validateApplicationName } = await Promise.resolve().then(() => require('../../utils/validation')); const { ExpectedError } = await Promise.resolve().then(() => require('../../errors')); const balena = (0, lazy_1.getBalenaSdk)(); const { getApplication } = await Promise.resolve().then(() => require('../../utils/sdk')); const application = await getApplication(balena, params.fleet, { $select: ['id', 'app_name', 'slug'], $expand: { application_type: { $select: 'slug', }, }, }); if (!application) { throw new ExpectedError(`Error: fleet ${params.fleet} not found.`); } const appType = application.application_type[0]; if (appType.slug === 'legacy-v1' || appType.slug === 'legacy-v2') { throw new ExpectedError(`Fleet ${params.fleet} is of 'legacy' type, and cannot be renamed.`); } const newName = params.newName || (await (0, lazy_1.getCliForm)().ask({ message: 'Please enter the new name for this fleet:', type: 'input', validate: validateApplicationName, })) || ''; if (newName.includes('/')) { throw new ExpectedError(`New fleet name cannot include '/', please check that you are not specifying fleet slug.`); } try { await balena.models.application.rename(application.id, newName); } catch (e) { if ((e.message || '').toLowerCase().includes('unique')) { throw new ExpectedError(`Error: fleet ${newName} already exists.`); } if ((e.message || '').toLowerCase().includes('name may only contain')) { throw new ExpectedError(`Error: new fleet name may only include characters [a-zA-Z0-9_-].`); } throw e; } const renamedApplication = await getApplication(balena, application.id, { $select: ['app_name', 'slug'], }); console.log(`Fleet renamed`); console.log('From:'); console.log(`\tname: ${application.app_name}`); console.log(`\tslug: ${application.slug}`); console.log('To:'); console.log(`\tname: ${renamedApplication.app_name}`); console.log(`\tslug: ${renamedApplication.slug}`); } } FleetRenameCmd.description = (0, lazy_1.stripIndent) ` Rename a fleet. Rename a fleet. Note, if the \`newName\` parameter is omitted, it will be prompted for interactively. ${messages_1.applicationIdInfo.split('\n').join('\n\t\t')} `; FleetRenameCmd.examples = [ '$ balena fleet rename OldName', '$ balena fleet rename OldName NewName', '$ balena fleet rename myorg/oldname NewName', ]; FleetRenameCmd.args = { fleet: ca.fleetRequired, newName: core_1.Args.string({ description: 'the new name for the fleet', }), }; FleetRenameCmd.authenticated = true; exports.default = FleetRenameCmd; //# sourceMappingURL=rename.js.map