From 9b454408a680b7b37ce5788c09eb62edebbc2c34 Mon Sep 17 00:00:00 2001 From: Tatsunori Uchino Date: Mon, 7 Sep 2020 02:13:00 +0900 Subject: [PATCH] Make up for LF in last line of SSH key file --- src/main.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4333884..af29bc4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,9 +20,9 @@ function main(): void const files: FileInfo[] = [ { name: core.getInput("name"), - contents: core.getInput("key", { + contents: insertLfToEnd(core.getInput("key", { required: true, - }), + })), options: { mode: 0o400, flag: "ax", @@ -103,6 +103,28 @@ function getHomeEnv(): string return "HOME"; } +/** + * append LF to value to the end if not empty + * @param value the value to prepend LF + * @returns prepended value + */ +function insertLfToEnd(value: string): string +{ + let affectedValue = value; + + if(value.length === 0) + { + // do nothing if empty + return ""; + } + if(!affectedValue.endsWith("\n")) + { + affectedValue = `${affectedValue}\n`; + } + + return affectedValue; +} + /** * prepend/append LF to value if not empty * @param value the value to prepend LF @@ -121,10 +143,7 @@ function insertLf(value: string): string { affectedValue = `\n${affectedValue}`; } - if(!affectedValue.endsWith("\n")) - { - affectedValue = `${affectedValue}\n`; - } + affectedValue = insertLfToEnd(affectedValue); return affectedValue; }