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:
parent
31d4b8b483
commit
83659583bf
6 changed files with 434 additions and 284 deletions
57
src/post.ts
Normal file
57
src/post.ts
Normal 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(", ")}`);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue