Skip to content

Commit

Permalink
detect files to be restore automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
shimataro committed Oct 10, 2023
1 parent 8365958 commit 27a89e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
21 changes: 11 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(", ")}`);
}
Expand Down
23 changes: 11 additions & 12 deletions src/post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import fs from "fs";
import path from "path";

import * as core from "@actions/core";

import * as common from "./common";

/**
Expand Down Expand Up @@ -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(", ")}`);
}

0 comments on commit 27a89e2

Please sign in to comment.