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

add "if_key_exists"

This commit is contained in:
shimataro 2021-03-07 19:20:46 +09:00
parent 8053198ce5
commit 3685ae93c6
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
3 changed files with 139 additions and 60 deletions

View file

@ -21,6 +21,10 @@ inputs:
description: "SSH config" description: "SSH config"
required: false required: false
default: "" default: ""
if_key_exists:
description: "replace / ignore / fail"
required: false
default: "fail"
runs: runs:
using: "node12" using: "node12"
main: "lib/index.js" main: "lib/index.js"

View file

@ -427,36 +427,16 @@ const core = __importStar(__nccwpck_require__(186));
*/ */
function main() { function main() {
try { try {
const files = [ // parameters
{ const key = core.getInput("key", {
name: core.getInput("name"), required: true,
contents: insertLf(core.getInput("key", { });
required: true, const name = core.getInput("name");
}), false, true), const knownHosts = core.getInput("known_hosts", {
options: { required: true,
mode: 0o400, });
flag: "ax", const config = core.getInput("config");
}, const ifKeyExists = core.getInput("if_key_exists");
},
{
name: "known_hosts",
contents: insertLf(core.getInput("known_hosts", {
required: true,
}), true, true),
options: {
mode: 0o644,
flag: "a",
},
},
{
name: "config",
contents: insertLf(core.getInput("config"), true, true),
options: {
mode: 0o644,
flag: "a",
},
},
];
// create ".ssh" directory // create ".ssh" directory
const home = getHomeDirectory(); const home = getHomeDirectory();
const dirName = path_1.default.resolve(home, ".ssh"); const dirName = path_1.default.resolve(home, ".ssh");
@ -464,6 +444,35 @@ function main() {
recursive: true, recursive: true,
mode: 0o700, mode: 0o700,
}); });
// files to be created
const files = [
{
name: "known_hosts",
contents: insertLf(knownHosts, true, true),
options: {
mode: 0o644,
flag: "a",
},
},
{
name: "config",
contents: insertLf(config, true, true),
options: {
mode: 0o644,
flag: "a",
},
},
];
if (shouldCreateKeyFile(path_1.default.join(dirName, name), ifKeyExists)) {
files.push({
name: name,
contents: insertLf(key, false, true),
options: {
mode: 0o400,
flag: "ax",
},
});
}
// create files // create files
for (const file of files) { for (const file of files) {
const fileName = path_1.default.join(dirName, file.name); const fileName = path_1.default.join(dirName, file.name);
@ -524,6 +533,30 @@ function insertLf(value, prepend, append) {
} }
return affectedValue; return affectedValue;
} }
/**
* should create SSH key file?
* @param keyFilePath path of key file
* @param ifKeyExists action if SSH key exists
* @returns Yes/No
*/
function shouldCreateKeyFile(keyFilePath, ifKeyExists) {
if (!fs_1.default.existsSync(keyFilePath)) {
// should create if file does not exist
return true;
}
switch (ifKeyExists) {
case "replace":
// remove file and should create if replace
fs_1.default.unlinkSync(keyFilePath);
return true;
case "ignore":
// should NOT create if ignore
return false;
default:
// error otherwise
throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`);
}
}
main(); main();

View file

@ -17,36 +17,16 @@ function main(): void
{ {
try try
{ {
const files: FileInfo[] = [ // parameters
{ const key = core.getInput("key", {
name: core.getInput("name"), required: true,
contents: insertLf(core.getInput("key", { });
required: true, const name = core.getInput("name");
}), false, true), const knownHosts = core.getInput("known_hosts", {
options: { required: true,
mode: 0o400, });
flag: "ax", const config = core.getInput("config");
}, const ifKeyExists = core.getInput("if_key_exists");
},
{
name: "known_hosts",
contents: insertLf(core.getInput("known_hosts", {
required: true,
}), true, true),
options: {
mode: 0o644,
flag: "a",
},
},
{
name: "config",
contents: insertLf(core.getInput("config"), true, true),
options: {
mode: 0o644,
flag: "a",
},
},
];
// create ".ssh" directory // create ".ssh" directory
const home = getHomeDirectory(); const home = getHomeDirectory();
@ -56,6 +36,37 @@ function main(): void
mode: 0o700, mode: 0o700,
}); });
// files to be created
const files: FileInfo[] = [
{
name: "known_hosts",
contents: insertLf(knownHosts, true, true),
options: {
mode: 0o644,
flag: "a",
},
},
{
name: "config",
contents: insertLf(config, true, true),
options: {
mode: 0o644,
flag: "a",
},
},
];
if(shouldCreateKeyFile(path.join(dirName, name), ifKeyExists))
{
files.push({
name: name,
contents: insertLf(key, false, true),
options: {
mode: 0o400,
flag: "ax",
},
});
}
// create files // create files
for(const file of files) for(const file of files)
{ {
@ -137,4 +148,35 @@ function insertLf(value: string, prepend: boolean, append: boolean): string
return affectedValue; return affectedValue;
} }
/**
* should create SSH key file?
* @param keyFilePath path of key file
* @param ifKeyExists action if SSH key exists
* @returns Yes/No
*/
function shouldCreateKeyFile(keyFilePath: string, ifKeyExists: string): boolean
{
if(!fs.existsSync(keyFilePath))
{
// should create if file does not exist
return true;
}
switch(ifKeyExists)
{
case "replace":
// remove file and should create if replace
fs.unlinkSync(keyFilePath);
return true;
case "ignore":
// should NOT create if ignore
return false;
default:
// error otherwise
throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`);
}
}
main(); main();