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:26:01 +09:00
parent 5cf4aac376
commit a1efc7a069
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
2 changed files with 71 additions and 56 deletions

View file

@ -2755,7 +2755,6 @@ catch (err) {
*/ */
function main() { function main() {
if (!isPost()) { if (!isPost()) {
backup();
setup(); setup();
setPost(); setPost();
} }
@ -2780,6 +2779,7 @@ function setPost() {
* setup function * setup function
*/ */
function setup() { function setup() {
const backupSuffix = generateBackupSuffix();
// parameters // parameters
const key = core.getInput("key", { const key = core.getInput("key", {
required: true, required: true,
@ -2824,11 +2824,18 @@ function setup() {
}); });
} }
// create files // create files
const backedUpFileNames = [];
for (const file of files) { for (const file of files) {
const fileName = path_1.default.join(sshDirName, file.name); const fileName = path_1.default.join(sshDirName, file.name);
if (backup(fileName, backupSuffix)) {
backedUpFileNames.push(file.name);
}
fs_1.default.writeFileSync(fileName, file.contents, file.options); fs_1.default.writeFileSync(fileName, file.contents, file.options);
} }
console.log(`SSH key has been stored to ${sshDirName} successfully.`); console.log(`SSH key has been stored to ${sshDirName} successfully.`);
if (backedUpFileNames.length > 0) {
console.log(`Following files are backed up in suffix "${backupSuffix}; ${backedUpFileNames.join(", ")}.`);
}
} }
/** /**
* cleanup function * cleanup function
@ -2846,37 +2853,37 @@ function cleanup() {
} }
} }
/** /**
* get file names to back up * generate backup suffix name
* @returns file names to back up * @returns backup suffix
*/ */
function getFileNamesForBackup() { function generateBackupSuffix() {
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 nothing if .ssh does not exist
return; return "";
} }
const backupSuffix = `.bak-${Date.now()}`; const backupSuffix = `.bak-${Date.now()}`;
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix); core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
const backedUpFileNames = []; return backupSuffix;
for (const fileName of getFileNamesForBackup()) { }
const pathNameOrg = path_1.default.join(dirName, fileName); /**
const pathNameBak = `${pathNameOrg}${backupSuffix}`; * back up file
if (!fs_1.default.existsSync(pathNameOrg)) { * @param fileName file to back up
continue; * @param backupSuffix suffix
* @returns is file backed up?
*/
function backup(fileName, backupSuffix) {
if (backupSuffix === "") {
return false;
}
if (!fs_1.default.existsSync(fileName)) {
return false;
} }
// move -> copy (in order to keep permissions when restore) // move -> copy (in order to keep permissions when restore)
fs_1.default.renameSync(pathNameOrg, pathNameBak); const fileNameBak = `${fileName}${backupSuffix}`;
fs_1.default.copyFileSync(pathNameBak, pathNameOrg); fs_1.default.renameSync(fileName, fileNameBak);
backedUpFileNames.push(fileName); fs_1.default.copyFileSync(fileNameBak, fileName);
} return true;
console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(",")}`);
} }
/** /**
* restore files * restore files
@ -2884,8 +2891,9 @@ function backup() {
*/ */
function restore(backupSuffix) { function restore(backupSuffix) {
const dirName = getSshDirectory(); const dirName = getSshDirectory();
const keyFileName = core.getInput("name");
const restoredFileNames = []; const restoredFileNames = [];
for (const fileName of getFileNamesForBackup()) { for (const fileName of ["known_hosts", "config", keyFileName]) {
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)) {

View file

@ -28,7 +28,6 @@ try {
*/ */
function main(): void { function main(): void {
if (!isPost()) { if (!isPost()) {
backup();
setup(); setup();
setPost(); setPost();
} else { } else {
@ -55,6 +54,8 @@ function setPost(): void {
* setup function * setup function
*/ */
function setup(): void { function setup(): void {
const backupSuffix = generateBackupSuffix();
// parameters // parameters
const key = core.getInput("key", { const key = core.getInput("key", {
required: true, required: true,
@ -102,12 +103,20 @@ function setup(): void {
} }
// create files // create files
const backedUpFileNames: string[] = [];
for (const file of files) { for (const file of files) {
const fileName = path.join(sshDirName, file.name); const fileName = path.join(sshDirName, file.name);
if (backup(fileName, backupSuffix)) {
backedUpFileNames.push(file.name);
}
fs.writeFileSync(fileName, file.contents, file.options); fs.writeFileSync(fileName, file.contents, file.options);
} }
console.log(`SSH key has been stored to ${sshDirName} successfully.`); console.log(`SSH key has been stored to ${sshDirName} successfully.`);
if (backedUpFileNames.length > 0) {
console.log(`Following files are backed up in suffix "${backupSuffix}; ${backedUpFileNames.join(", ")}.`);
}
} }
/** /**
@ -128,44 +137,41 @@ function cleanup(): void {
} }
/** /**
* get file names to back up * generate backup suffix name
* @returns file names to back up * @returns backup suffix
*/ */
function getFileNamesForBackup(): string[] { function generateBackupSuffix(): 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 nothing if .ssh does not exist
return; return "";
} }
const backupSuffix = `.bak-${Date.now()}`; const backupSuffix = `.bak-${Date.now()}`;
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix); core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
return backupSuffix;
}
const backedUpFileNames: string[] = []; /**
for (const fileName of getFileNamesForBackup()) { * back up file
const pathNameOrg = path.join(dirName, fileName); * @param fileName file to back up
const pathNameBak = `${pathNameOrg}${backupSuffix}`; * @param backupSuffix suffix
* @returns is file backed up?
if (!fs.existsSync(pathNameOrg)) { */
continue; function backup(fileName: string, backupSuffix: string): boolean {
if (backupSuffix === "") {
return false;
}
if (!fs.existsSync(fileName)) {
return false;
} }
// move -> copy (in order to keep permissions when restore) // move -> copy (in order to keep permissions when restore)
fs.renameSync(pathNameOrg, pathNameBak); const fileNameBak = `${fileName}${backupSuffix}`;
fs.copyFileSync(pathNameBak, pathNameOrg); fs.renameSync(fileName, fileNameBak);
fs.copyFileSync(fileNameBak, fileName);
backedUpFileNames.push(fileName); return true;
}
console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(",")}`);
} }
/** /**
@ -174,9 +180,10 @@ function backup(): void {
*/ */
function restore(backupSuffix: string): void { function restore(backupSuffix: string): void {
const dirName = getSshDirectory(); const dirName = getSshDirectory();
const keyFileName = core.getInput("name");
const restoredFileNames: string[] = []; const restoredFileNames: string[] = [];
for (const fileName of getFileNamesForBackup()) { for (const fileName of ["known_hosts", "config", keyFileName]) {
const pathNameOrg = path.join(dirName, fileName); const pathNameOrg = path.join(dirName, fileName);
const pathNameBak = `${pathNameOrg}${backupSuffix}`; const pathNameBak = `${pathNameOrg}${backupSuffix}`;