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

use module

This commit is contained in:
shimataro 2023-10-10 16:22:19 +09:00
parent 31d4b8b483
commit 83659583bf
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
6 changed files with 434 additions and 284 deletions

57
src/post.ts Normal file
View file

@ -0,0 +1,57 @@
import fs from "fs";
import path from "path";
import * as core from "@actions/core";
import * as common from "./common";
/**
* cleanup function
*/
export function post(): void {
const backupSuffix = common.getBackupSuffix();
if (backupSuffix === "") {
// remove ".ssh" directory if suffix is not set
removeSshDirectory();
} else {
// restore files from backup suffix
restore(backupSuffix);
}
}
/**
* remove ".ssh" directory
*/
function removeSshDirectory(): void {
const dirName = common.getSshDirectory();
fs.rmSync(dirName, {
recursive: true,
force: true,
});
console.log(`SSH key in ${dirName} has been removed successfully.`);
}
/**
* restore files
* @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}`;
if (!fs.existsSync(pathNameBak)) {
continue;
}
fs.rmSync(pathNameOrg);
fs.renameSync(pathNameBak, pathNameOrg);
restoredFileNames.push(fileName);
}
console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`);
}