balena-cli/lib/actions-oclif/env/rm.ts
Scott Lowe be82bcfa63 convert commands key, keys, key add, key rm to oclif.
Also:
 - Display keys with `name` instead of `title`.
 - Check for empty key before calling API.

Change-type: patch
Signed-off-by: Scott Lowe <scott@balena.io>
2020-04-20 16:07:39 +02:00

109 lines
2.9 KiB
TypeScript

/**
* @license
* Copyright 2019 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.
*/
import { flags } from '@oclif/command';
import { stripIndent } from 'common-tags';
import Command from '../../command';
import * as ec from '../../utils/env-common';
import { getBalenaSdk } from '../../utils/lazy';
import { CommandHelp } from '../../utils/oclif-utils';
import { parseAsInteger } from '../../utils/validation';
type IArg<T> = import('@oclif/parser').args.IArg<T>;
interface FlagsDef {
config: boolean;
device: boolean;
service: boolean;
yes: boolean;
}
interface ArgsDef {
id: number;
}
export default class EnvRmCmd extends Command {
public static description = stripIndent`
Remove a config or env var from an application, device or service.
Remove a configuration or environment variable from an application, device
or service, as selected by command-line options.
${ec.rmRenameHelp.split('\n').join('\n\t\t')}
Interactive confirmation is normally asked before the variable is deleted.
The --yes option disables this behavior.
`;
public static examples = [
'$ balena env rm 123123',
'$ balena env rm 234234 --yes',
'$ balena env rm 345345 --config',
'$ balena env rm 456456 --service',
'$ balena env rm 567567 --device',
'$ balena env rm 678678 --device --config',
'$ balena env rm 789789 --device --service --yes',
];
public static args: Array<IArg<any>> = [
{
name: 'id',
required: true,
description: "variable's numeric database ID",
parse: input => parseAsInteger(input, 'id'),
},
];
// hardcoded 'env rm' to avoid oclif's 'env:rm' topic syntax
public static usage =
'env rm ' + new CommandHelp({ args: EnvRmCmd.args }).defaultUsage();
public static flags: flags.Input<FlagsDef> = {
config: ec.booleanConfig,
device: ec.booleanDevice,
service: ec.booleanService,
yes: flags.boolean({
char: 'y',
description:
'do not prompt for confirmation before deleting the variable',
default: false,
}),
};
public async run() {
const { args: params, flags: opt } = this.parse<FlagsDef, ArgsDef>(
EnvRmCmd,
);
const balena = getBalenaSdk();
const { confirm } = await import('../../utils/patterns');
await Command.checkLoggedIn();
await confirm(
opt.yes || false,
'Are you sure you want to delete the environment variable?',
undefined,
true,
);
await balena.pine.delete({
resource: ec.getVarResourceName(opt.config, opt.device, opt.service),
id: params.id,
});
}
}