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