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

fix: do not remove .ssh dir on cleanup. use .backup files instead

This commit is contained in:
Daniel 2023-09-07 23:29:19 +02:00
parent f5671c022c
commit 13556936c6

View file

@ -67,16 +67,31 @@ function setup(): void {
const sshDirName = createSshDirectory(); const sshDirName = createSshDirectory();
// files to be created // files to be created
const files: FileInfo[] = [ const files: FileInfo[] = [];
{
name: "known_hosts", const knownHostsPath = path.join(sshDirName, "known_hosts")
contents: insertLf(buildKnownHostsArray(knownHosts).join("\n"), true, true), if (fs.existsSync(knownHostsPath)) {
if (fs.existsSync(knownHostsPath + ".backup")) {
fs.unlinkSync(knownHostsPath + ".backup");
}
files.push({
name: "known_hosts.backup",
contents: fs.readFileSync(knownHostsPath, "utf8"),
options: { options: {
mode: 0o644, mode: 0o644,
flag: "a", flag: "w"
}, },
})
}
files.push({
name: "known_hosts",
contents: insertLf(buildKnownHostsArray(knownHosts).join("\n"), true, true),
options: {
mode: 0o644,
flag: "a",
}, },
]; });
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) { if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
files.push({ files.push({
name: name, name: name,
@ -88,6 +103,19 @@ function setup(): void {
}); });
} }
if (config !== "") { if (config !== "") {
const configPath = path.join(sshDirName, "config")
if (fs.existsSync(configPath + ".backup")) {
fs.unlinkSync(configPath + ".backup");
}
files.push({
name: "config.backup",
contents: fs.readFileSync(configPath, "utf8"),
options: {
mode: 0o644,
flag: "w"
},
})
files.push({ files.push({
name: "config", name: "config",
contents: insertLf(config, true, true), contents: insertLf(config, true, true),
@ -111,10 +139,15 @@ function setup(): void {
* cleanup function * cleanup function
*/ */
function cleanup(): void { function cleanup(): void {
// remove ".ssh" directory restoreFileFromBackup("known_hosts")
const sshDirName = removeSshDirectory(); restoreFileFromBackup("config")
console.log(`SSH key in ${sshDirName} has been removed successfully.`); const sshDirName = getSshDirectory()
const sshKeyName = core.getInput("name");
const sshKeyFilePath = path.join(sshDirName, sshKeyName);
if (fs.existsSync(sshKeyFilePath)) {
fs.unlinkSync(sshKeyFilePath);
}
} }
/** /**
@ -131,16 +164,20 @@ function createSshDirectory(): string {
} }
/** /**
* remove ".ssh" directory * restore file from ".backup" file
* @returns removed directory name * @returns void
*/ */
function removeSshDirectory(): string { function restoreFileFromBackup(fileName: string): void {
const dirName = getSshDirectory(); const sshDirName = getSshDirectory();
fs.rmSync(dirName, {
recursive: true, const backupFilePath = path.join(sshDirName, `${fileName}.backup`);
force: true, const filePath = path.join(sshDirName, fileName);
});
return dirName; if (fs.existsSync(backupFilePath)) {
fs.unlinkSync(filePath);
fs.renameSync(backupFilePath, filePath);
console.log(`${backupFilePath} file restored from ${filePath} file`);
}
} }
/** /**