@liara/cli
Version:
The command line interface for Liara
62 lines (61 loc) • 2.16 kB
JavaScript
import inquirer from 'inquirer';
import Command from '../../base.js';
import { Flags, Args } from '@oclif/core';
import { createDebugLogger } from '../../utils/output.js';
class EnvUnset extends Command {
async run() {
const { flags, argv } = await this.parse(EnvUnset);
await this.setGotConfig(flags);
const debug = createDebugLogger(flags.debug);
if (!argv.length) {
EnvUnset.run(['-h']);
this.exit(0);
}
if (argv.join(' ').includes('=')) {
return this.error(`You can't use '=' in the key. Please check your input.`);
}
const app = flags.app || (await this.promptProject());
const appliedEnvs = await this.fetchEnvs(app);
const variables = appliedEnvs.filter((v) => !argv.includes(v.key));
try {
if (flags.force || (await this.confirm())) {
await this.got.post(`v1/projects/update-envs`, {
json: { project: app, variables },
});
this.log(`Configuration variable removed and restarting ${app}`);
}
}
catch (error) {
debug(error.message);
}
}
async fetchEnvs(app) {
const { project } = await this.got(`v1/projects/${app}`).json();
const envs = project.envs.map((env) => {
const key = env.key;
const value = env.value;
return { key, value };
});
return envs;
}
async confirm() {
const { confirm } = (await inquirer.prompt({
name: 'confirm',
type: 'confirm',
message: `Your app will be restarted due to these configuration changes. Confirm: `,
default: false,
}));
return confirm;
}
}
EnvUnset.description = 'remove environment variables from an app';
EnvUnset.strict = false;
EnvUnset.args = {
env: Args.string({ description: 'key' }),
};
EnvUnset.flags = {
...Command.flags,
app: Flags.string({ char: 'a', description: 'app id' }),
force: Flags.boolean({ char: 'f', description: 'force update' }),
};
export default EnvUnset;