mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-20 06:07:55 +00:00
Simplify/refactor 'env add' and 'env rm' implementation
Change-type: patch Signed-off-by: Paulo Castro <paulo@balena.io>
This commit is contained in:
parent
2ff427fb90
commit
b3bef9e556
65
lib/actions-oclif/env/add.ts
vendored
65
lib/actions-oclif/env/add.ts
vendored
@ -83,46 +83,45 @@ export default class EnvAddCmd extends Command {
|
||||
const { args: params, flags: options } = this.parse<FlagsDef, ArgsDef>(
|
||||
EnvAddCmd,
|
||||
);
|
||||
const Bluebird = await import('bluebird');
|
||||
const cmd = this;
|
||||
const balena = (await import('balena-sdk')).fromSharedOptions();
|
||||
const { exitWithExpectedError } = await import('../../utils/patterns');
|
||||
|
||||
const cmd = this;
|
||||
if (params.value == null) {
|
||||
params.value = process.env[params.name];
|
||||
|
||||
await Bluebird.try(async function() {
|
||||
if (params.value == null) {
|
||||
params.value = process.env[params.name];
|
||||
|
||||
if (params.value == null) {
|
||||
throw new Error(
|
||||
`Environment value not found for variable: ${params.name}`,
|
||||
);
|
||||
} else if (!options.quiet) {
|
||||
cmd.warn(
|
||||
`Using ${params.name}=${params.value} from CLI process environment`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const reservedPrefixes = await getReservedPrefixes();
|
||||
const isConfigVar = _.some(reservedPrefixes, prefix =>
|
||||
_.startsWith(params.name, prefix),
|
||||
);
|
||||
|
||||
if (options.application) {
|
||||
return balena.models.application[
|
||||
isConfigVar ? 'configVar' : 'envVar'
|
||||
].set(options.application, params.name, params.value);
|
||||
} else if (options.device) {
|
||||
return balena.models.device[isConfigVar ? 'configVar' : 'envVar'].set(
|
||||
options.device,
|
||||
params.name,
|
||||
params.value,
|
||||
throw new Error(
|
||||
`Environment value not found for variable: ${params.name}`,
|
||||
);
|
||||
} else if (!options.quiet) {
|
||||
cmd.warn(
|
||||
`Using ${params.name}=${params.value} from CLI process environment`,
|
||||
);
|
||||
} else {
|
||||
exitWithExpectedError('You must specify an application or device');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const reservedPrefixes = await getReservedPrefixes();
|
||||
const isConfigVar = _.some(reservedPrefixes, prefix =>
|
||||
_.startsWith(params.name, prefix),
|
||||
);
|
||||
const varType = isConfigVar ? 'configVar' : 'envVar';
|
||||
|
||||
if (options.application) {
|
||||
await balena.models.application[varType].set(
|
||||
options.application,
|
||||
params.name,
|
||||
params.value,
|
||||
);
|
||||
} else if (options.device) {
|
||||
await balena.models.device[varType].set(
|
||||
options.device,
|
||||
params.name,
|
||||
params.value,
|
||||
);
|
||||
} else {
|
||||
exitWithExpectedError('You must specify an application or device');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
34
lib/actions-oclif/env/rm.ts
vendored
34
lib/actions-oclif/env/rm.ts
vendored
@ -54,7 +54,7 @@ export default class EnvRmCmd extends Command {
|
||||
{
|
||||
name: 'id',
|
||||
required: true,
|
||||
description: 'environment variable id',
|
||||
description: 'environment variable numeric database ID',
|
||||
},
|
||||
];
|
||||
|
||||
@ -89,23 +89,23 @@ export default class EnvRmCmd extends Command {
|
||||
);
|
||||
}
|
||||
|
||||
return patterns
|
||||
.confirm(
|
||||
try {
|
||||
await patterns.confirm(
|
||||
options.yes || false,
|
||||
'Are you sure you want to delete the environment variable?',
|
||||
)
|
||||
.then(function() {
|
||||
if (options.device) {
|
||||
return balena.pine.delete({
|
||||
resource: 'device_environment_variable',
|
||||
id: params.id,
|
||||
});
|
||||
} else {
|
||||
return balena.pine.delete({
|
||||
resource: 'application_environment_variable',
|
||||
id: params.id,
|
||||
});
|
||||
}
|
||||
});
|
||||
);
|
||||
} catch (err) {
|
||||
if (err.message === 'Aborted') {
|
||||
return patterns.exitWithExpectedError(err);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
await balena.pine.delete({
|
||||
resource: options.device
|
||||
? 'device_environment_variable'
|
||||
: 'application_environment_variable',
|
||||
id: params.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user