mirror of
https://github.com/shimataro/ssh-key-action.git
synced 2025-06-19 22:52:10 +10:00
refactor
This commit is contained in:
parent
35831eecc5
commit
5cf4aac376
2 changed files with 40 additions and 18 deletions
27
lib/index.js
27
lib/index.js
|
@ -2755,6 +2755,7 @@ catch (err) {
|
|||
*/
|
||||
function main() {
|
||||
if (!isPost()) {
|
||||
backup();
|
||||
setup();
|
||||
setPost();
|
||||
}
|
||||
|
@ -2789,7 +2790,6 @@ function setup() {
|
|||
});
|
||||
const config = core.getInput("config");
|
||||
const ifKeyExists = core.getInput("if_key_exists");
|
||||
backup(name);
|
||||
// create ".ssh" directory
|
||||
const sshDirName = createSshDirectory();
|
||||
// files to be created
|
||||
|
@ -2846,10 +2846,17 @@ function cleanup() {
|
|||
}
|
||||
}
|
||||
/**
|
||||
* backup files
|
||||
* @param keyFileName filename of SSH private key
|
||||
* get file names to back up
|
||||
* @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();
|
||||
if (!fs_1.default.existsSync(dirName)) {
|
||||
// do noting if .ssh does not exist
|
||||
|
@ -2857,7 +2864,8 @@ function backup(keyFileName) {
|
|||
}
|
||||
const backupSuffix = `.bak-${Date.now()}`;
|
||||
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 pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
if (!fs_1.default.existsSync(pathNameOrg)) {
|
||||
|
@ -2866,17 +2874,18 @@ function backup(keyFileName) {
|
|||
// move -> copy (in order to keep permissions when restore)
|
||||
fs_1.default.renameSync(pathNameOrg, pathNameBak);
|
||||
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
|
||||
* @param backupSuffix suffix of backup directory
|
||||
*/
|
||||
function restore(backupSuffix) {
|
||||
const keyFileName = core.getInput("name");
|
||||
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 pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
if (!fs_1.default.existsSync(pathNameBak)) {
|
||||
|
@ -2884,7 +2893,9 @@ function restore(backupSuffix) {
|
|||
}
|
||||
fs_1.default.rmSync(pathNameOrg);
|
||||
fs_1.default.renameSync(pathNameBak, pathNameOrg);
|
||||
restoredFileNames.push(fileName);
|
||||
}
|
||||
console.log(`Following files are restored; ${restoredFileNames.join(",")}`);
|
||||
}
|
||||
/**
|
||||
* create ".ssh" directory
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue