1
0
Fork 0
mirror of https://github.com/shimataro/ssh-key-action.git synced 2025-06-19 22:52:10 +10:00
This commit is contained in:
shimataro 2023-10-10 07:11:16 +09:00
parent 35831eecc5
commit 5cf4aac376
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
2 changed files with 40 additions and 18 deletions

View file

@ -2755,6 +2755,7 @@ catch (err) {
*/ */
function main() { function main() {
if (!isPost()) { if (!isPost()) {
backup();
setup(); setup();
setPost(); setPost();
} }
@ -2789,7 +2790,6 @@ function setup() {
}); });
const config = core.getInput("config"); const config = core.getInput("config");
const ifKeyExists = core.getInput("if_key_exists"); const ifKeyExists = core.getInput("if_key_exists");
backup(name);
// create ".ssh" directory // create ".ssh" directory
const sshDirName = createSshDirectory(); const sshDirName = createSshDirectory();
// files to be created // files to be created
@ -2846,10 +2846,17 @@ function cleanup() {
} }
} }
/** /**
* backup files * get file names to back up
* @param keyFileName filename of SSH private key * @returns file names to back up
*/ */
function backup(keyFileName) { function getFileNamesForBackup() {
const keyFileName = core.getInput("name");
return ["known_hosts", "config", keyFileName];
}
/**
* back up files
*/
function backup() {
const dirName = getSshDirectory(); const dirName = getSshDirectory();
if (!fs_1.default.existsSync(dirName)) { if (!fs_1.default.existsSync(dirName)) {
// do noting if .ssh does not exist // do noting if .ssh does not exist
@ -2857,7 +2864,8 @@ function backup(keyFileName) {
} }
const backupSuffix = `.bak-${Date.now()}`; const backupSuffix = `.bak-${Date.now()}`;
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix); core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
for (const fileName of ["known_hosts", "config", keyFileName]) { const backedUpFileNames = [];
for (const fileName of getFileNamesForBackup()) {
const pathNameOrg = path_1.default.join(dirName, fileName); const pathNameOrg = path_1.default.join(dirName, fileName);
const pathNameBak = `${pathNameOrg}${backupSuffix}`; const pathNameBak = `${pathNameOrg}${backupSuffix}`;
if (!fs_1.default.existsSync(pathNameOrg)) { if (!fs_1.default.existsSync(pathNameOrg)) {
@ -2866,17 +2874,18 @@ function backup(keyFileName) {
// move -> copy (in order to keep permissions when restore) // move -> copy (in order to keep permissions when restore)
fs_1.default.renameSync(pathNameOrg, pathNameBak); fs_1.default.renameSync(pathNameOrg, pathNameBak);
fs_1.default.copyFileSync(pathNameBak, pathNameOrg); fs_1.default.copyFileSync(pathNameBak, pathNameOrg);
backedUpFileNames.push(fileName);
} }
console.log(`backup suffix: "${backupSuffix}"`); console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(",")}`);
} }
/** /**
* restore files * restore files
* @param backupSuffix suffix of backup directory * @param backupSuffix suffix of backup directory
*/ */
function restore(backupSuffix) { function restore(backupSuffix) {
const keyFileName = core.getInput("name");
const dirName = getSshDirectory(); const dirName = getSshDirectory();
for (const fileName of ["known_hosts", "config", keyFileName]) { const restoredFileNames = [];
for (const fileName of getFileNamesForBackup()) {
const pathNameOrg = path_1.default.join(dirName, fileName); const pathNameOrg = path_1.default.join(dirName, fileName);
const pathNameBak = `${pathNameOrg}${backupSuffix}`; const pathNameBak = `${pathNameOrg}${backupSuffix}`;
if (!fs_1.default.existsSync(pathNameBak)) { if (!fs_1.default.existsSync(pathNameBak)) {
@ -2884,7 +2893,9 @@ function restore(backupSuffix) {
} }
fs_1.default.rmSync(pathNameOrg); fs_1.default.rmSync(pathNameOrg);
fs_1.default.renameSync(pathNameBak, pathNameOrg); fs_1.default.renameSync(pathNameBak, pathNameOrg);
restoredFileNames.push(fileName);
} }
console.log(`Following files are restored; ${restoredFileNames.join(",")}`);
} }
/** /**
* create ".ssh" directory * create ".ssh" directory

View file

@ -28,6 +28,7 @@ try {
*/ */
function main(): void { function main(): void {
if (!isPost()) { if (!isPost()) {
backup();
setup(); setup();
setPost(); setPost();
} else { } else {
@ -65,8 +66,6 @@ function setup(): void {
const config = core.getInput("config"); const config = core.getInput("config");
const ifKeyExists = core.getInput("if_key_exists"); const ifKeyExists = core.getInput("if_key_exists");
backup(name);
// create ".ssh" directory // create ".ssh" directory
const sshDirName = createSshDirectory(); const sshDirName = createSshDirectory();
@ -116,7 +115,6 @@ function setup(): void {
*/ */
function cleanup(): void { function cleanup(): void {
const backupSuffix = core.getState(STATE_BACKUP_SUFFIX); const backupSuffix = core.getState(STATE_BACKUP_SUFFIX);
if (backupSuffix === "") { if (backupSuffix === "") {
// remove ".ssh" directory if suffix is not set // remove ".ssh" directory if suffix is not set
const sshDirName = removeSshDirectory(); const sshDirName = removeSshDirectory();
@ -130,10 +128,18 @@ function cleanup(): void {
} }
/** /**
* backup files * get file names to back up
* @param keyFileName filename of SSH private key * @returns file names to back up
*/ */
function backup(keyFileName: string): void { function getFileNamesForBackup(): string[] {
const keyFileName = core.getInput("name");
return ["known_hosts", "config", keyFileName];
}
/**
* back up files
*/
function backup(): void {
const dirName = getSshDirectory(); const dirName = getSshDirectory();
if (!fs.existsSync(dirName)) { if (!fs.existsSync(dirName)) {
// do noting if .ssh does not exist // do noting if .ssh does not exist
@ -143,7 +149,8 @@ function backup(keyFileName: string): void {
const backupSuffix = `.bak-${Date.now()}`; const backupSuffix = `.bak-${Date.now()}`;
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix); core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
for (const fileName of ["known_hosts", "config", keyFileName]) { const backedUpFileNames: string[] = [];
for (const fileName of getFileNamesForBackup()) {
const pathNameOrg = path.join(dirName, fileName); const pathNameOrg = path.join(dirName, fileName);
const pathNameBak = `${pathNameOrg}${backupSuffix}`; const pathNameBak = `${pathNameOrg}${backupSuffix}`;
@ -154,9 +161,11 @@ function backup(keyFileName: string): void {
// move -> copy (in order to keep permissions when restore) // move -> copy (in order to keep permissions when restore)
fs.renameSync(pathNameOrg, pathNameBak); fs.renameSync(pathNameOrg, pathNameBak);
fs.copyFileSync(pathNameBak, pathNameOrg); fs.copyFileSync(pathNameBak, pathNameOrg);
backedUpFileNames.push(fileName);
} }
console.log(`backup suffix: "${backupSuffix}"`); console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(",")}`);
} }
/** /**
@ -164,10 +173,10 @@ function backup(keyFileName: string): void {
* @param backupSuffix suffix of backup directory * @param backupSuffix suffix of backup directory
*/ */
function restore(backupSuffix: string): void { function restore(backupSuffix: string): void {
const keyFileName = core.getInput("name");
const dirName = getSshDirectory(); const dirName = getSshDirectory();
for (const fileName of ["known_hosts", "config", keyFileName]) { const restoredFileNames: string[] = [];
for (const fileName of getFileNamesForBackup()) {
const pathNameOrg = path.join(dirName, fileName); const pathNameOrg = path.join(dirName, fileName);
const pathNameBak = `${pathNameOrg}${backupSuffix}`; const pathNameBak = `${pathNameOrg}${backupSuffix}`;
@ -177,7 +186,9 @@ function restore(backupSuffix: string): void {
fs.rmSync(pathNameOrg); fs.rmSync(pathNameOrg);
fs.renameSync(pathNameBak, pathNameOrg); fs.renameSync(pathNameBak, pathNameOrg);
restoredFileNames.push(fileName);
} }
console.log(`Following files are restored; ${restoredFileNames.join(",")}`);
} }
/** /**