UNPKG

@onboardbase/cli

Version:

[![Version](https://img.shields.io/npm/v/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![Downloads/week](https://img.shields.io/npm/dw/@onboardbase/cli.svg)](https://www.npmjs.com/package/@onboardbase/cli) [![License](https://img

105 lines (104 loc) 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TFBaseService = void 0; const fs = require("fs"); const axios_1 = require("axios"); const path = require("path"); const chalk = require("chalk"); const utils_1 = require("../../utils"); const common_1 = require("../../common"); const access_manager_1 = require("../access-manager"); const types_1 = require("./types"); const Store = require("@onboardbase/store"); const path_1 = require("path"); const { Blob } = require("buffer"); class TFBaseService extends common_1.BaseService { constructor(configManager) { super(configManager); this.accessManager = new access_manager_1.AccessManager(configManager); } async configure(input) { var _a; const { flags, operation } = input; const storeKey = flags["store-key"]; const lockerKey = flags["locker-key"]; const fileId = (_a = flags["file-id"]) !== null && _a !== void 0 ? _a : "TF-STATE"; if (!flags.file && !storeKey) { throw new Error("Please specify a file or store key"); } // @ts-ignore - this was intentional :smirk: this.store = new Store(storeKey.trim()); switch (operation) { case types_1.TfOperation.UPLOAD: await this._uploadStateFileToStore({ lockerKey, storeKey, fileId, flags, }); break; case types_1.TfOperation.DOWNLOAD: await this._downloadStateFileFromStore({ lockerKey, storeKey, fileId, flags, }); break; } } async _uploadStateFileToStore(input) { var _a; const { lockerKey, fileId, flags } = input; const pathToFile = (_a = flags.file) !== null && _a !== void 0 ? _a : "./terraform.tfstate"; if ((0, utils_1.isExist)(pathToFile) || pathToFile === "-") { // const uploadResponse = await axios.put(uploadURL, fileContent); const fileObject = this._getFileObject(pathToFile); await this.store.setFile({ lockerKey, key: fileId, file: fileObject, fileName: (0, path_1.basename)(pathToFile), }); } console.log(chalk.greenBright("State file uploaded successfully to store")); } _getFileObject(pathToFile) { let buffer; if (pathToFile === "-") { const stdin = fs.readFileSync(0); buffer = stdin; } else buffer = fs.readFileSync(pathToFile); return new Blob([buffer]); } async _downloadStateFileFromStore(input) { var _a; const { lockerKey, fileId, flags } = input; const getFileResponse = await this.store.getFile({ lockerKey, key: fileId, }); if (!getFileResponse) { throw new Error("Failed to fetch state file from store"); } const pathToFile = (_a = flags.file) !== null && _a !== void 0 ? _a : "./terraform.tfstate"; await this.downloadAndSaveFile(getFileResponse.downloadUrl, pathToFile); } async downloadAndSaveFile(downloadUrl, savePath) { try { const response = await axios_1.default.get(downloadUrl, { responseType: "text" }); const dir = path.dirname(savePath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } fs.writeFileSync(savePath, JSON.stringify(response.data)); console.log(chalk.greenBright(`File downloaded and saved to ${savePath}`)); } catch (error) { console.error(chalk.redBright("Failed to download file")); } } } exports.TFBaseService = TFBaseService;