1
0
Fork 0
mirror of https://github.com/shimataro/ssh-key-action.git synced 2025-06-19 22:52:10 +10:00

detect files to be restore automatically

This commit is contained in:
shimataro 2023-10-10 16:41:14 +09:00
parent 83659583bf
commit 27a89e2532
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
2 changed files with 22 additions and 22 deletions

View file

@ -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(", ")}`);
}

View file

@ -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(", ")}`);
}