From 3685ae93c6a40b652b6a50727fe4ea16838feaba Mon Sep 17 00:00:00 2001 From: shimataro Date: Sun, 7 Mar 2021 19:20:46 +0900 Subject: [PATCH] add "if_key_exists" --- action.yml | 4 ++ lib/index.js | 93 +++++++++++++++++++++++++++++++--------------- src/main.ts | 102 ++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 139 insertions(+), 60 deletions(-) diff --git a/action.yml b/action.yml index a7eb1ae..6fab2b1 100644 --- a/action.yml +++ b/action.yml @@ -21,6 +21,10 @@ inputs: description: "SSH config" required: false default: "" + if_key_exists: + description: "replace / ignore / fail" + required: false + default: "fail" runs: using: "node12" main: "lib/index.js" diff --git a/lib/index.js b/lib/index.js index fe1c710..4bf5c48 100644 --- a/lib/index.js +++ b/lib/index.js @@ -427,36 +427,16 @@ const core = __importStar(__nccwpck_require__(186)); */ function main() { try { - const files = [ - { - name: core.getInput("name"), - contents: insertLf(core.getInput("key", { - required: true, - }), false, true), - options: { - mode: 0o400, - flag: "ax", - }, - }, - { - 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", - }, - }, - ]; + // parameters + const key = core.getInput("key", { + required: true, + }); + const name = core.getInput("name"); + const knownHosts = core.getInput("known_hosts", { + required: true, + }); + const config = core.getInput("config"); + const ifKeyExists = core.getInput("if_key_exists"); // create ".ssh" directory const home = getHomeDirectory(); const dirName = path_1.default.resolve(home, ".ssh"); @@ -464,6 +444,35 @@ function main() { recursive: true, 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 for (const file of files) { const fileName = path_1.default.join(dirName, file.name); @@ -524,6 +533,30 @@ function insertLf(value, prepend, append) { } 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(); diff --git a/src/main.ts b/src/main.ts index d44005e..0187a72 100644 --- a/src/main.ts +++ b/src/main.ts @@ -17,36 +17,16 @@ function main(): void { try { - const files: FileInfo[] = [ - { - name: core.getInput("name"), - contents: insertLf(core.getInput("key", { - required: true, - }), false, true), - options: { - mode: 0o400, - flag: "ax", - }, - }, - { - 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", - }, - }, - ]; + // parameters + const key = core.getInput("key", { + required: true, + }); + const name = core.getInput("name"); + const knownHosts = core.getInput("known_hosts", { + required: true, + }); + const config = core.getInput("config"); + const ifKeyExists = core.getInput("if_key_exists"); // create ".ssh" directory const home = getHomeDirectory(); @@ -56,6 +36,37 @@ function main(): void 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 for(const file of files) { @@ -137,4 +148,35 @@ function insertLf(value: string, prepend: boolean, append: boolean): string 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();