2020-09-16 13:23:36 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2016-2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
import type { flags } from '@oclif/command';
|
|
|
|
import type { Output as ParserOutput } from '@oclif/parser';
|
|
|
|
import type { ApplicationType } from 'balena-sdk';
|
|
|
|
|
2020-09-16 13:23:36 +00:00
|
|
|
import Command from '../../command';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
2020-12-10 12:30:17 +00:00
|
|
|
import * as ca from '../../utils/common-args';
|
2020-09-16 13:23:36 +00:00
|
|
|
import { getBalenaSdk, stripIndent, getCliForm } from '../../utils/lazy';
|
2021-07-15 13:41:38 +00:00
|
|
|
import {
|
|
|
|
applicationIdInfo,
|
|
|
|
appToFleetCmdMsg,
|
|
|
|
warnify,
|
|
|
|
} from '../../utils/messages';
|
2020-09-16 13:23:36 +00:00
|
|
|
|
|
|
|
interface FlagsDef {
|
|
|
|
help: void;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ArgsDef {
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet: string;
|
2020-09-16 13:23:36 +00:00
|
|
|
newName?: string;
|
|
|
|
}
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
export class FleetRenameCmd extends Command {
|
2020-09-16 13:23:36 +00:00
|
|
|
public static description = stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
Rename a fleet.
|
2020-09-16 13:23:36 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Rename a fleet.
|
2020-09-16 13:23:36 +00:00
|
|
|
|
|
|
|
Note, if the \`newName\` parameter is omitted, it will be
|
|
|
|
prompted for interactively.
|
2020-12-10 12:30:17 +00:00
|
|
|
|
|
|
|
${applicationIdInfo.split('\n').join('\n\t\t')}
|
2020-12-09 15:55:00 +00:00
|
|
|
`;
|
|
|
|
|
2020-09-16 13:23:36 +00:00
|
|
|
public static examples = [
|
2021-07-15 13:41:38 +00:00
|
|
|
'$ balena fleet rename OldName',
|
|
|
|
'$ balena fleet rename OldName NewName',
|
|
|
|
'$ balena fleet rename myorg/oldname NewName',
|
2020-09-16 13:23:36 +00:00
|
|
|
];
|
|
|
|
|
2020-12-10 12:30:17 +00:00
|
|
|
public static args = [
|
2021-07-15 13:41:38 +00:00
|
|
|
ca.fleetRequired,
|
2020-09-16 13:23:36 +00:00
|
|
|
{
|
|
|
|
name: 'newName',
|
2021-07-15 13:41:38 +00:00
|
|
|
description: 'the new name for the fleet',
|
2020-09-16 13:23:36 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
public static usage = 'fleet rename <fleet> [newName]';
|
2020-09-16 13:23:36 +00:00
|
|
|
|
|
|
|
public static flags: flags.Input<FlagsDef> = {
|
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
public async run(parserOutput?: ParserOutput<FlagsDef, ArgsDef>) {
|
|
|
|
const { args: params } =
|
|
|
|
parserOutput || this.parse<FlagsDef, ArgsDef>(FleetRenameCmd);
|
2020-09-16 13:23:36 +00:00
|
|
|
|
2020-12-09 15:55:00 +00:00
|
|
|
const { validateApplicationName } = await import('../../utils/validation');
|
|
|
|
const { ExpectedError } = await import('../../errors');
|
|
|
|
|
2020-09-16 13:23:36 +00:00
|
|
|
const balena = getBalenaSdk();
|
|
|
|
|
2020-12-10 12:30:17 +00:00
|
|
|
// Disambiguate target application (if params.params is a number, it could either be an ID or a numerical name)
|
2020-12-09 15:55:00 +00:00
|
|
|
const { getApplication } = await import('../../utils/sdk');
|
2021-07-15 13:41:38 +00:00
|
|
|
const application = await getApplication(balena, params.fleet, {
|
2020-12-09 15:55:00 +00:00
|
|
|
$expand: {
|
|
|
|
application_type: {
|
|
|
|
$select: ['is_legacy'],
|
2020-09-16 13:23:36 +00:00
|
|
|
},
|
2020-12-09 15:55:00 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check app exists
|
|
|
|
if (!application) {
|
2021-07-15 13:41:38 +00:00
|
|
|
throw new ExpectedError(`Error: fleet ${params.fleet} not found.`);
|
2020-09-16 13:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check app supports renaming
|
2020-12-09 15:55:00 +00:00
|
|
|
const appType = (application.application_type as ApplicationType[])?.[0];
|
2020-09-16 13:23:36 +00:00
|
|
|
if (appType.is_legacy) {
|
|
|
|
throw new ExpectedError(
|
2021-07-15 13:41:38 +00:00
|
|
|
`Fleet ${params.fleet} is of 'legacy' type, and cannot be renamed.`,
|
2020-09-16 13:23:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:55:00 +00:00
|
|
|
// Ascertain new name
|
2020-09-16 13:23:36 +00:00
|
|
|
const newName =
|
|
|
|
params.newName ||
|
|
|
|
(await getCliForm().ask({
|
2021-07-15 13:41:38 +00:00
|
|
|
message: 'Please enter the new name for this fleet:',
|
2020-09-16 13:23:36 +00:00
|
|
|
type: 'input',
|
|
|
|
validate: validateApplicationName,
|
|
|
|
})) ||
|
|
|
|
'';
|
|
|
|
|
2020-12-09 15:55:00 +00:00
|
|
|
// Rename
|
2020-09-16 13:23:36 +00:00
|
|
|
try {
|
2020-12-09 15:55:00 +00:00
|
|
|
await balena.models.application.rename(application.id, newName);
|
2020-09-16 13:23:36 +00:00
|
|
|
} catch (e) {
|
2020-12-09 15:55:00 +00:00
|
|
|
// BalenaRequestError: Request error: "organization" and "app_name" must be unique.
|
2020-09-16 13:23:36 +00:00
|
|
|
if ((e.message || '').toLowerCase().includes('unique')) {
|
2021-10-04 03:52:24 +00:00
|
|
|
throw new ExpectedError(`Error: fleet ${newName} already exists.`);
|
2020-09-16 13:23:36 +00:00
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:30:17 +00:00
|
|
|
// Get application again, to be sure of results
|
|
|
|
const renamedApplication = await balena.models.application.get(
|
|
|
|
application.id,
|
|
|
|
);
|
|
|
|
|
2020-12-09 15:55:00 +00:00
|
|
|
// Output result
|
2021-07-15 13:41:38 +00:00
|
|
|
console.log(`Fleet renamed`);
|
2020-12-10 12:30:17 +00:00
|
|
|
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}`);
|
2020-09-16 13:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-15 13:41:38 +00:00
|
|
|
|
|
|
|
export default class AppRenameCmd extends FleetRenameCmd {
|
|
|
|
public static description = stripIndent`
|
|
|
|
DEPRECATED alias for the 'fleet rename' command
|
|
|
|
|
|
|
|
${appToFleetCmdMsg
|
|
|
|
.split('\n')
|
|
|
|
.map((l) => `\t\t${l}`)
|
|
|
|
.join('\n')}
|
|
|
|
|
|
|
|
For command usage, see 'balena help fleet rename'
|
|
|
|
`;
|
|
|
|
public static examples = [];
|
|
|
|
public static usage = 'app rename <fleet> [newName]';
|
|
|
|
public static args = FleetRenameCmd.args;
|
|
|
|
public static flags = FleetRenameCmd.flags;
|
|
|
|
public static authenticated = FleetRenameCmd.authenticated;
|
|
|
|
public static primary = FleetRenameCmd.primary;
|
|
|
|
|
|
|
|
public async run() {
|
|
|
|
// call this.parse() before deprecation message to parse '-h'
|
|
|
|
const parserOutput = this.parse<FlagsDef, ArgsDef>(AppRenameCmd);
|
|
|
|
if (process.stderr.isTTY) {
|
|
|
|
console.error(warnify(appToFleetCmdMsg));
|
|
|
|
}
|
|
|
|
await super.run(parserOutput);
|
|
|
|
}
|
|
|
|
}
|