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

fix; files that didn't backed up is not removed in "post" phase

This commit is contained in:
shimataro 2023-10-11 10:54:08 +09:00
parent ab731d2fcd
commit df107d88c4
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
7 changed files with 97 additions and 60 deletions

42
dist/main.js vendored

File diff suppressed because one or more lines are too long

6
dist/main.js.map vendored

File diff suppressed because one or more lines are too long

44
dist/post.js vendored

File diff suppressed because one or more lines are too long

6
dist/post.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -6,6 +6,7 @@ import * as core from "@actions/core";
/** state name of backup suffix */
const STATE_BACKUP_SUFFIX = "backup-suffix";
const STATE_CREATED_FILES = "created-files";
/**
* create backup suffix name
@ -31,6 +32,24 @@ export function getBackupSuffix(): string {
return core.getState(STATE_BACKUP_SUFFIX);
}
/**
* save created file names
* @param fileNames array of file names
*/
export function saveCreatedFileNames(fileNames: string[]): void {
const json = JSON.stringify(fileNames);
core.saveState(STATE_CREATED_FILES, json);
}
/**
* save created file names
* @returns saved array of file names
*/
export function loadCreatedFileNames(): string[] {
const json = core.getState(STATE_CREATED_FILES);
return JSON.parse(json) as string[];
}
/**
* get SSH directory
* @returns SSH directory name

View file

@ -89,6 +89,7 @@ export function main(): void {
}
// create files
const createdFileNames: string[] = [];
const backedUpFileNames: string[] = [];
for (const file of files) {
const fileName = path.join(sshDirName, file.name);
@ -97,7 +98,9 @@ export function main(): void {
}
fs.writeFileSync(fileName, file.contents, file.options);
createdFileNames.push(file.name);
}
common.saveCreatedFileNames(createdFileNames);
console.log(`SSH key has been stored to ${sshDirName} successfully.`);
if (backedUpFileNames.length > 0) {

View file

@ -17,35 +17,51 @@ try {
* cleanup function
*/
export function post(): void {
const sshDirName = common.getSshDirectory();
const backupSuffix = common.getBackupSuffix();
if (backupSuffix === "") {
// remove ".ssh" directory if suffix is not set
removeSshDirectory();
removeDirectory(sshDirName);
console.log(`SSH key in ${sshDirName} has been removed successfully.`);
} else {
// restore files from backup suffix
restore(backupSuffix);
// remove created files and restore from backup
removeCreatedFiles(sshDirName);
const restoredFileNames = restoreFiles(sshDirName, backupSuffix);
console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`);
}
}
/**
* remove ".ssh" directory
* remove directory
* @param dirName directory name to remove
*/
function removeSshDirectory(): void {
const dirName = common.getSshDirectory();
function removeDirectory(dirName: string): void {
fs.rmSync(dirName, {
recursive: true,
force: true,
});
}
console.log(`SSH key in ${dirName} has been removed successfully.`);
/**
* remove created files in main phase
* @param dirName directory name
*/
function removeCreatedFiles(dirName: string): void {
const createdFileNames = common.loadCreatedFileNames();
for (const fileName of createdFileNames) {
const pathName = path.join(dirName, fileName);
fs.rmSync(pathName);
}
}
/**
* restore files from backups
* @param dirName directory name
* @param backupSuffix suffix of backup directory
* @returns restored file names
*/
function restore(backupSuffix: string): void {
const dirName = common.getSshDirectory();
function restoreFiles(dirName: string, backupSuffix: string): string[] {
const restoredFileNames: string[] = [];
const entries = fs.readdirSync(dirName)
.filter((entry) => {
@ -58,9 +74,8 @@ function restore(backupSuffix: string): void {
const pathNameOrg = path.join(dirName, entryOrg);
const pathNameBak = path.join(dirName, entry);
fs.rmSync(pathNameOrg);
fs.renameSync(pathNameBak, pathNameOrg);
restoredFileNames.push(entryOrg);
}
console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`);
return restoredFileNames;
}