@onboardbase/cli
Version:
[](https://www.npmjs.com/package/@onboardbase/cli) [](https://www.npmjs.com/package/@onboardbase/cli) [ • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TFCommandService = void 0;
const fs_1 = require("fs");
const common_1 = require("../../common");
const onDeath = require("death");
const utils_1 = require("../../utils");
const encryptor_1 = require("./encryptor");
class TFCommandService extends common_1.BaseService {
async initialize({ args, flags }) {
var _a, _b;
onDeath(async () => {
await this._cleanup(flags);
});
const command = (_a = flags.command) !== null && _a !== void 0 ? _a : args.command;
if (!command)
console.error("Please specify a Terraform command to Execute");
const pathToFile = (_b = flags.file) !== null && _b !== void 0 ? _b : "./terraform.tfstate";
if ((0, utils_1.isExist)(pathToFile) || pathToFile === "-") {
let fileContent;
if (pathToFile === "-") {
const stdin = (0, fs_1.readFileSync)(0, "utf-8");
fileContent = stdin;
}
else
fileContent = (0, fs_1.readFileSync)(pathToFile, "utf-8");
if (!(0, utils_1.isJSON)(fileContent)) {
const decryptedContents = await (0, utils_1.decryptGenericSecret)(fileContent, flags["enc-key"]);
(0, fs_1.writeFileSync)(pathToFile === "-" ? "./terraform.tfstate" : pathToFile, JSON.parse(JSON.stringify(decryptedContents)));
}
}
else
return console.error(`Could not locate file: ${pathToFile}`);
/**
* check if user has an encrypted tfstate, if yes, decrypt it before running
* the command
*/
const daemon = (0, utils_1.executeCommand)(command.trim(), process.env, true);
daemon.on("exit", async (code) => {
try {
await this._cleanup(flags);
}
catch (error) {
process.exit(0);
}
});
}
async _cleanup(flags) {
if (flags.file !== "-") {
await (0, encryptor_1.encryptTfState)(flags);
process.exit();
}
else
(0, fs_1.unlinkSync)("./terraform.tfstate");
}
}
exports.TFCommandService = TFCommandService;