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
5cf4aac376
commit
a1efc7a069
2 changed files with 71 additions and 56 deletions
60
lib/index.js
60
lib/index.js
|
@ -2755,7 +2755,6 @@ catch (err) {
|
|||
*/
|
||||
function main() {
|
||||
if (!isPost()) {
|
||||
backup();
|
||||
setup();
|
||||
setPost();
|
||||
}
|
||||
|
@ -2780,6 +2779,7 @@ function setPost() {
|
|||
* setup function
|
||||
*/
|
||||
function setup() {
|
||||
const backupSuffix = generateBackupSuffix();
|
||||
// parameters
|
||||
const key = core.getInput("key", {
|
||||
required: true,
|
||||
|
@ -2824,11 +2824,18 @@ function setup() {
|
|||
});
|
||||
}
|
||||
// create files
|
||||
const backedUpFileNames = [];
|
||||
for (const file of files) {
|
||||
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);
|
||||
}
|
||||
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
|
||||
|
@ -2846,37 +2853,37 @@ function cleanup() {
|
|||
}
|
||||
}
|
||||
/**
|
||||
* get file names to back up
|
||||
* @returns file names to back up
|
||||
* generate backup suffix name
|
||||
* @returns backup suffix
|
||||
*/
|
||||
function getFileNamesForBackup() {
|
||||
const keyFileName = core.getInput("name");
|
||||
return ["known_hosts", "config", keyFileName];
|
||||
}
|
||||
/**
|
||||
* back up files
|
||||
*/
|
||||
function backup() {
|
||||
function generateBackupSuffix() {
|
||||
const dirName = getSshDirectory();
|
||||
if (!fs_1.default.existsSync(dirName)) {
|
||||
// do noting if .ssh does not exist
|
||||
return;
|
||||
// do nothing if .ssh does not exist
|
||||
return "";
|
||||
}
|
||||
const backupSuffix = `.bak-${Date.now()}`;
|
||||
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
|
||||
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)) {
|
||||
continue;
|
||||
}
|
||||
// move -> copy (in order to keep permissions when restore)
|
||||
fs_1.default.renameSync(pathNameOrg, pathNameBak);
|
||||
fs_1.default.copyFileSync(pathNameBak, pathNameOrg);
|
||||
backedUpFileNames.push(fileName);
|
||||
return backupSuffix;
|
||||
}
|
||||
/**
|
||||
* back up file
|
||||
* @param fileName file to back up
|
||||
* @param backupSuffix suffix
|
||||
* @returns is file backed up?
|
||||
*/
|
||||
function backup(fileName, backupSuffix) {
|
||||
if (backupSuffix === "") {
|
||||
return false;
|
||||
}
|
||||
console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(",")}`);
|
||||
if (!fs_1.default.existsSync(fileName)) {
|
||||
return false;
|
||||
}
|
||||
// move -> copy (in order to keep permissions when restore)
|
||||
const fileNameBak = `${fileName}${backupSuffix}`;
|
||||
fs_1.default.renameSync(fileName, fileNameBak);
|
||||
fs_1.default.copyFileSync(fileNameBak, fileName);
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* restore files
|
||||
|
@ -2884,8 +2891,9 @@ function backup() {
|
|||
*/
|
||||
function restore(backupSuffix) {
|
||||
const dirName = getSshDirectory();
|
||||
const keyFileName = core.getInput("name");
|
||||
const restoredFileNames = [];
|
||||
for (const fileName of getFileNamesForBackup()) {
|
||||
for (const fileName of ["known_hosts", "config", keyFileName]) {
|
||||
const pathNameOrg = path_1.default.join(dirName, fileName);
|
||||
const pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
if (!fs_1.default.existsSync(pathNameBak)) {
|
||||
|
|
67
src/main.ts
67
src/main.ts
|
@ -28,7 +28,6 @@ try {
|
|||
*/
|
||||
function main(): void {
|
||||
if (!isPost()) {
|
||||
backup();
|
||||
setup();
|
||||
setPost();
|
||||
} else {
|
||||
|
@ -55,6 +54,8 @@ function setPost(): void {
|
|||
* setup function
|
||||
*/
|
||||
function setup(): void {
|
||||
const backupSuffix = generateBackupSuffix();
|
||||
|
||||
// parameters
|
||||
const key = core.getInput("key", {
|
||||
required: true,
|
||||
|
@ -102,12 +103,20 @@ function setup(): void {
|
|||
}
|
||||
|
||||
// create files
|
||||
const backedUpFileNames: string[] = [];
|
||||
for (const file of files) {
|
||||
const fileName = path.join(sshDirName, file.name);
|
||||
if (backup(fileName, backupSuffix)) {
|
||||
backedUpFileNames.push(file.name);
|
||||
}
|
||||
|
||||
fs.writeFileSync(fileName, file.contents, file.options);
|
||||
}
|
||||
|
||||
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
|
||||
* @returns file names to back up
|
||||
* generate backup suffix name
|
||||
* @returns backup suffix
|
||||
*/
|
||||
function getFileNamesForBackup(): string[] {
|
||||
const keyFileName = core.getInput("name");
|
||||
return ["known_hosts", "config", keyFileName];
|
||||
}
|
||||
|
||||
/**
|
||||
* back up files
|
||||
*/
|
||||
function backup(): void {
|
||||
function generateBackupSuffix(): string {
|
||||
const dirName = getSshDirectory();
|
||||
if (!fs.existsSync(dirName)) {
|
||||
// do noting if .ssh does not exist
|
||||
return;
|
||||
// do nothing if .ssh does not exist
|
||||
return "";
|
||||
}
|
||||
|
||||
const backupSuffix = `.bak-${Date.now()}`;
|
||||
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
|
||||
return backupSuffix;
|
||||
}
|
||||
|
||||
const backedUpFileNames: string[] = [];
|
||||
for (const fileName of getFileNamesForBackup()) {
|
||||
const pathNameOrg = path.join(dirName, fileName);
|
||||
const pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
|
||||
if (!fs.existsSync(pathNameOrg)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// move -> copy (in order to keep permissions when restore)
|
||||
fs.renameSync(pathNameOrg, pathNameBak);
|
||||
fs.copyFileSync(pathNameBak, pathNameOrg);
|
||||
|
||||
backedUpFileNames.push(fileName);
|
||||
/**
|
||||
* back up file
|
||||
* @param fileName file to back up
|
||||
* @param backupSuffix suffix
|
||||
* @returns is file backed up?
|
||||
*/
|
||||
function backup(fileName: string, backupSuffix: string): boolean {
|
||||
if (backupSuffix === "") {
|
||||
return false;
|
||||
}
|
||||
if (!fs.existsSync(fileName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(",")}`);
|
||||
// move -> copy (in order to keep permissions when restore)
|
||||
const fileNameBak = `${fileName}${backupSuffix}`;
|
||||
fs.renameSync(fileName, fileNameBak);
|
||||
fs.copyFileSync(fileNameBak, fileName);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -174,9 +180,10 @@ function backup(): void {
|
|||
*/
|
||||
function restore(backupSuffix: string): void {
|
||||
const dirName = getSshDirectory();
|
||||
const keyFileName = core.getInput("name");
|
||||
|
||||
const restoredFileNames: string[] = [];
|
||||
for (const fileName of getFileNamesForBackup()) {
|
||||
for (const fileName of ["known_hosts", "config", keyFileName]) {
|
||||
const pathNameOrg = path.join(dirName, fileName);
|
||||
const pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue