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
|
||||
|
|
31
src/main.ts
31
src/main.ts
|
@ -28,6 +28,7 @@ try {
|
|||
*/
|
||||
function main(): void {
|
||||
if (!isPost()) {
|
||||
backup();
|
||||
setup();
|
||||
setPost();
|
||||
} else {
|
||||
|
@ -65,8 +66,6 @@ function setup(): void {
|
|||
const config = core.getInput("config");
|
||||
const ifKeyExists = core.getInput("if_key_exists");
|
||||
|
||||
backup(name);
|
||||
|
||||
// create ".ssh" directory
|
||||
const sshDirName = createSshDirectory();
|
||||
|
||||
|
@ -116,7 +115,6 @@ function setup(): void {
|
|||
*/
|
||||
function cleanup(): void {
|
||||
const backupSuffix = core.getState(STATE_BACKUP_SUFFIX);
|
||||
|
||||
if (backupSuffix === "") {
|
||||
// remove ".ssh" directory if suffix is not set
|
||||
const sshDirName = removeSshDirectory();
|
||||
|
@ -130,10 +128,18 @@ function cleanup(): void {
|
|||
}
|
||||
|
||||
/**
|
||||
* backup files
|
||||
* @param keyFileName filename of SSH private key
|
||||
* get file names to back up
|
||||
* @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();
|
||||
if (!fs.existsSync(dirName)) {
|
||||
// do noting if .ssh does not exist
|
||||
|
@ -143,7 +149,8 @@ function backup(keyFileName: string): void {
|
|||
const backupSuffix = `.bak-${Date.now()}`;
|
||||
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 pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
|
||||
|
@ -154,9 +161,11 @@ function backup(keyFileName: string): void {
|
|||
// move -> copy (in order to keep permissions when restore)
|
||||
fs.renameSync(pathNameOrg, pathNameBak);
|
||||
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
|
||||
*/
|
||||
function restore(backupSuffix: string): void {
|
||||
const keyFileName = core.getInput("name");
|
||||
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 pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
|
||||
|
@ -177,7 +186,9 @@ function restore(backupSuffix: string): void {
|
|||
|
||||
fs.rmSync(pathNameOrg);
|
||||
fs.renameSync(pathNameBak, pathNameOrg);
|
||||
restoredFileNames.push(fileName);
|
||||
}
|
||||
console.log(`Following files are restored; ${restoredFileNames.join(",")}`);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue