From 27a89e2532ce0ad9dc494a73182693ce45f6488a Mon Sep 17 00:00:00 2001 From: shimataro Date: Tue, 10 Oct 2023 16:41:14 +0900 Subject: [PATCH] detect files to be restore automatically --- lib/index.js | 21 +++++++++++---------- src/post.ts | 23 +++++++++++------------ 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/index.js b/lib/index.js index 2e9e4fd..18e184c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3104,7 +3104,6 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.post = void 0; const fs_1 = __importDefault(__nccwpck_require__(147)); const path_1 = __importDefault(__nccwpck_require__(17)); -const core = __importStar(__nccwpck_require__(186)); const common = __importStar(__nccwpck_require__(108)); /** * cleanup function @@ -3133,22 +3132,24 @@ function removeSshDirectory() { console.log(`SSH key in ${dirName} has been removed successfully.`); } /** - * restore files + * restore files from backups * @param backupSuffix suffix of backup directory */ function restore(backupSuffix) { const dirName = common.getSshDirectory(); - const keyFileName = core.getInput("name"); const restoredFileNames = []; - for (const fileName of ["known_hosts", "config", keyFileName]) { - const pathNameOrg = path_1.default.join(dirName, fileName); - const pathNameBak = `${pathNameOrg}${backupSuffix}`; - if (!fs_1.default.existsSync(pathNameBak)) { - continue; - } + const entries = fs_1.default.readdirSync(dirName) + .filter((entry) => { + // skip if not a backed-up file + return entry.endsWith(backupSuffix); + }); + for (const entry of entries) { + const entryOrg = entry.substring(0, entry.length - backupSuffix.length); + const pathNameOrg = path_1.default.join(dirName, entryOrg); + const pathNameBak = path_1.default.join(dirName, entry); fs_1.default.rmSync(pathNameOrg); fs_1.default.renameSync(pathNameBak, pathNameOrg); - restoredFileNames.push(fileName); + restoredFileNames.push(entryOrg); } console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`); } diff --git a/src/post.ts b/src/post.ts index d622479..9c875fa 100644 --- a/src/post.ts +++ b/src/post.ts @@ -1,8 +1,6 @@ import fs from "fs"; import path from "path"; -import * as core from "@actions/core"; - import * as common from "./common"; /** @@ -33,25 +31,26 @@ function removeSshDirectory(): void { } /** - * restore files + * restore files from backups * @param backupSuffix suffix of backup directory */ function restore(backupSuffix: string): void { const dirName = common.getSshDirectory(); - const keyFileName = core.getInput("name"); - const restoredFileNames: string[] = []; - for (const fileName of ["known_hosts", "config", keyFileName]) { - const pathNameOrg = path.join(dirName, fileName); - const pathNameBak = `${pathNameOrg}${backupSuffix}`; + const entries = fs.readdirSync(dirName) + .filter((entry) => { + // skip if not a backed-up file + return entry.endsWith(backupSuffix); + }); - if (!fs.existsSync(pathNameBak)) { - continue; - } + for (const entry of entries) { + const entryOrg = entry.substring(0, entry.length - backupSuffix.length); + const pathNameOrg = path.join(dirName, entryOrg); + const pathNameBak = path.join(dirName, entry); fs.rmSync(pathNameOrg); fs.renameSync(pathNameBak, pathNameOrg); - restoredFileNames.push(fileName); + restoredFileNames.push(entryOrg); } console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`); }