Merge pull request #1825 from balena-io/1824-app-exists

`app create`: "You already have an application with that name!"
This commit is contained in:
Paulo Castro 2020-05-14 09:15:18 -04:00 committed by GitHub
commit aff370e9c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 15 deletions

View File

@ -307,6 +307,10 @@ Examples:
### Options
#### -v, --verbose
add extra columns in the tabular output (SLUG)
## app <name>
Display detailed information about a single balena application.

View File

@ -78,22 +78,25 @@ export default class AppCreateCmd extends Command {
const balena = getBalenaSdk();
const patterns = await import('../../utils/patterns');
// First make sure they don't already have an app with this name
if (await balena.models.application.has(params.name)) {
throw new ExpectedError(
'You already have an application with that name!',
);
}
// Create application
const deviceType = options.type || (await patterns.selectDeviceType());
const application = await balena.models.application.create({
name: params.name,
deviceType,
});
let application: import('balena-sdk').Application;
try {
application = await balena.models.application.create({
name: params.name,
deviceType,
});
} catch (err) {
// BalenaRequestError: Request error: Unique key constraint violated
if ((err.message || '').toLowerCase().includes('unique')) {
throw new ExpectedError(
`Error: application "${params.name}" already exists`,
);
}
throw err;
}
console.info(
`Application created: ${application.app_name} (${application.device_type}, id ${application.id})`,
`Application created: ${application.slug} (${application.device_type}, id ${application.id})`,
);
}
}

View File

@ -29,6 +29,7 @@ interface ExtendedApplication extends Application {
interface FlagsDef {
help: void;
verbose?: boolean;
}
export default class AppsCmd extends Command {
@ -46,13 +47,17 @@ export default class AppsCmd extends Command {
public static flags: flags.Input<FlagsDef> = {
help: cf.help,
verbose: flags.boolean({
char: 'v',
description: 'add extra columns in the tabular output (SLUG)',
}),
};
public static authenticated = true;
public static primary = true;
public async run() {
this.parse<FlagsDef, {}>(AppsCmd);
const { flags: options } = this.parse<FlagsDef, {}>(AppsCmd);
const _ = await import('lodash');
const balena = getBalenaSdk();
@ -60,7 +65,7 @@ export default class AppsCmd extends Command {
// Get applications
const applications: ExtendedApplication[] = await balena.models.application.getAll(
{
$select: ['id', 'app_name', 'device_type'],
$select: ['id', 'app_name', 'slug', 'device_type'],
$expand: { owns__device: { $select: 'is_online' } },
},
);
@ -78,6 +83,7 @@ export default class AppsCmd extends Command {
getVisuals().table.horizontal(applications, [
'id',
'app_name',
options.verbose ? 'slug' : '',
'device_type',
'online_devices',
'device_count',

View File

@ -132,6 +132,7 @@ const messages: {
};
const EXPECTED_ERROR_REGEXES = [
/^BalenaAmbiguousApplication:/, // balena-sdk
/^BalenaApplicationNotFound:/, // balena-sdk
/^BalenaDeviceNotFound:/, // balena-sdk
/^Missing \w+$/, // Capitano, oclif parser: RequiredArgsError, RequiredFlagError