1
0
Fork 0
mirror of https://github.com/shimataro/ssh-key-action.git synced 2025-06-19 22:52:10 +10:00

back up key file

This commit is contained in:
shimataro 2023-10-10 07:55:24 +09:00
parent a1efc7a069
commit d2f01f267a
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
2 changed files with 21 additions and 10 deletions

View file

@ -2801,6 +2801,7 @@ function setup() {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
}, },
mustNotExist: false,
}, },
]; ];
if (shouldCreateKeyFile(path_1.default.join(sshDirName, name), ifKeyExists)) { if (shouldCreateKeyFile(path_1.default.join(sshDirName, name), ifKeyExists)) {
@ -2811,6 +2812,7 @@ function setup() {
mode: 0o400, mode: 0o400,
flag: "wx", flag: "wx",
}, },
mustNotExist: true,
}); });
} }
if (config !== "") { if (config !== "") {
@ -2821,13 +2823,14 @@ function setup() {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
}, },
mustNotExist: false,
}); });
} }
// create files // create files
const backedUpFileNames = []; 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)) { if (backup(fileName, backupSuffix, file.mustNotExist)) {
backedUpFileNames.push(file.name); backedUpFileNames.push(file.name);
} }
fs_1.default.writeFileSync(fileName, file.contents, file.options); fs_1.default.writeFileSync(fileName, file.contents, file.options);
@ -2870,9 +2873,10 @@ function generateBackupSuffix() {
* back up file * back up file
* @param fileName file to back up * @param fileName file to back up
* @param backupSuffix suffix * @param backupSuffix suffix
* @param removeOrig remove original file
* @returns is file backed up? * @returns is file backed up?
*/ */
function backup(fileName, backupSuffix) { function backup(fileName, backupSuffix, removeOrig) {
if (backupSuffix === "") { if (backupSuffix === "") {
return false; return false;
} }
@ -2882,7 +2886,9 @@ function backup(fileName, backupSuffix) {
// move -> copy (in order to keep permissions when restore) // move -> copy (in order to keep permissions when restore)
const fileNameBak = `${fileName}${backupSuffix}`; const fileNameBak = `${fileName}${backupSuffix}`;
fs_1.default.renameSync(fileName, fileNameBak); fs_1.default.renameSync(fileName, fileNameBak);
fs_1.default.copyFileSync(fileNameBak, fileName); if (!removeOrig) {
fs_1.default.copyFileSync(fileNameBak, fileName);
}
return true; return true;
} }
/** /**
@ -2998,8 +3004,7 @@ function shouldCreateKeyFile(keyFilePath, ifKeyExists) {
} }
switch (ifKeyExists) { switch (ifKeyExists) {
case "replace": case "replace":
// remove file and should create if replace // should create if replace (existing file will be backed up when creating)
fs_1.default.unlinkSync(keyFilePath);
return true; return true;
case "ignore": case "ignore":
// should NOT create if ignore // should NOT create if ignore

View file

@ -7,6 +7,7 @@ interface FileInfo {
name: string; name: string;
contents: string; contents: string;
options: fs.WriteFileOptions; options: fs.WriteFileOptions;
mustNotExist: boolean; // file must not exist when creating
} }
const STATE_BACKUP_SUFFIX = "backup-suffix"; const STATE_BACKUP_SUFFIX = "backup-suffix";
@ -79,6 +80,7 @@ function setup(): void {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
}, },
mustNotExist: false,
}, },
]; ];
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) { if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
@ -89,6 +91,7 @@ function setup(): void {
mode: 0o400, mode: 0o400,
flag: "wx", flag: "wx",
}, },
mustNotExist: true,
}); });
} }
if (config !== "") { if (config !== "") {
@ -99,6 +102,7 @@ function setup(): void {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
}, },
mustNotExist: false,
}); });
} }
@ -106,7 +110,7 @@ function setup(): void {
const backedUpFileNames: string[] = []; 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)) { if (backup(fileName, backupSuffix, file.mustNotExist)) {
backedUpFileNames.push(file.name); backedUpFileNames.push(file.name);
} }
@ -156,9 +160,10 @@ function generateBackupSuffix(): string {
* back up file * back up file
* @param fileName file to back up * @param fileName file to back up
* @param backupSuffix suffix * @param backupSuffix suffix
* @param removeOrig remove original file
* @returns is file backed up? * @returns is file backed up?
*/ */
function backup(fileName: string, backupSuffix: string): boolean { function backup(fileName: string, backupSuffix: string, removeOrig: boolean): boolean {
if (backupSuffix === "") { if (backupSuffix === "") {
return false; return false;
} }
@ -169,7 +174,9 @@ function backup(fileName: string, backupSuffix: string): boolean {
// move -> copy (in order to keep permissions when restore) // move -> copy (in order to keep permissions when restore)
const fileNameBak = `${fileName}${backupSuffix}`; const fileNameBak = `${fileName}${backupSuffix}`;
fs.renameSync(fileName, fileNameBak); fs.renameSync(fileName, fileNameBak);
fs.copyFileSync(fileNameBak, fileName); if (!removeOrig) {
fs.copyFileSync(fileNameBak, fileName);
}
return true; return true;
} }
@ -303,8 +310,7 @@ function shouldCreateKeyFile(keyFilePath: string, ifKeyExists: string): boolean
switch (ifKeyExists) { switch (ifKeyExists) {
case "replace": case "replace":
// remove file and should create if replace // should create if replace (existing file will be backed up when creating)
fs.unlinkSync(keyFilePath);
return true; return true;
case "ignore": case "ignore":