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

refactor insertLf()

This commit is contained in:
shimataro 2021-02-24 21:18:56 +09:00
parent 65f58a9a8d
commit 5a0f410a4f
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
2 changed files with 24 additions and 43 deletions

View file

@ -430,9 +430,9 @@ function main() {
const files = [ const files = [
{ {
name: core.getInput("name"), name: core.getInput("name"),
contents: insertLf(core.getInput("key", { required: true }), { contents: insertLf(core.getInput("key", {
append: true, required: true,
}), }), false, true),
options: { options: {
mode: 0o400, mode: 0o400,
flag: "ax", flag: "ax",
@ -440,10 +440,9 @@ function main() {
}, },
{ {
name: "known_hosts", name: "known_hosts",
contents: insertLf(core.getInput("known_hosts", { required: true }), { contents: insertLf(core.getInput("known_hosts", {
prepend: true, required: true,
append: true, }), true, true),
}),
options: { options: {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
@ -451,10 +450,7 @@ function main() {
}, },
{ {
name: "config", name: "config",
contents: insertLf(core.getInput("config"), { contents: insertLf(core.getInput("config"), true, true),
prepend: true,
append: true,
}),
options: { options: {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
@ -510,20 +506,20 @@ function getHomeEnv() {
/** /**
* prepend/append LF to value if not empty * prepend/append LF to value if not empty
* @param value the value to insert LF * @param value the value to insert LF
* @param options options * @param prepend true to prepend
* @param append true to append
* @returns new value * @returns new value
*/ */
function insertLf(value, options) { function insertLf(value, prepend, append) {
const normalizedOptions = Object.assign({ prepend: false, append: false }, options);
let affectedValue = value; let affectedValue = value;
if (value.length === 0) { if (value.length === 0) {
// do nothing if empty // do nothing if empty
return ""; return "";
} }
if (normalizedOptions.prepend && !affectedValue.startsWith("\n")) { if (prepend && !affectedValue.startsWith("\n")) {
affectedValue = `\n${affectedValue}`; affectedValue = `\n${affectedValue}`;
} }
if (normalizedOptions.append && !affectedValue.endsWith("\n")) { if (append && !affectedValue.endsWith("\n")) {
affectedValue = `${affectedValue}\n`; affectedValue = `${affectedValue}\n`;
} }
return affectedValue; return affectedValue;

View file

@ -10,13 +10,6 @@ interface FileInfo
options: fs.WriteFileOptions; options: fs.WriteFileOptions;
} }
/** options for insertLf() */
interface InsertLfOptions
{
prepend?: boolean;
append?: boolean;
}
/** /**
* main function * main function
*/ */
@ -27,9 +20,9 @@ function main(): void
const files: FileInfo[] = [ const files: FileInfo[] = [
{ {
name: core.getInput("name"), name: core.getInput("name"),
contents: insertLf(core.getInput("key", {required: true}), { contents: insertLf(core.getInput("key", {
append: true, required: true,
}), }), false, true),
options: { options: {
mode: 0o400, mode: 0o400,
flag: "ax", flag: "ax",
@ -37,10 +30,9 @@ function main(): void
}, },
{ {
name: "known_hosts", name: "known_hosts",
contents: insertLf(core.getInput("known_hosts", {required: true}), { contents: insertLf(core.getInput("known_hosts", {
prepend: true, required: true,
append: true, }), true, true),
}),
options: { options: {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
@ -48,10 +40,7 @@ function main(): void
}, },
{ {
name: "config", name: "config",
contents: insertLf(core.getInput("config"), { contents: insertLf(core.getInput("config"), true, true),
prepend: true,
append: true,
}),
options: { options: {
mode: 0o644, mode: 0o644,
flag: "a", flag: "a",
@ -123,16 +112,12 @@ function getHomeEnv(): string
/** /**
* prepend/append LF to value if not empty * prepend/append LF to value if not empty
* @param value the value to insert LF * @param value the value to insert LF
* @param options options * @param prepend true to prepend
* @param append true to append
* @returns new value * @returns new value
*/ */
function insertLf(value: string, options: InsertLfOptions): string function insertLf(value: string, prepend: boolean, append: boolean): string
{ {
const normalizedOptions: Required<InsertLfOptions> = {
prepend: false,
append: false,
...options,
};
let affectedValue = value; let affectedValue = value;
if(value.length === 0) if(value.length === 0)
@ -140,11 +125,11 @@ function insertLf(value: string, options: InsertLfOptions): string
// do nothing if empty // do nothing if empty
return ""; return "";
} }
if(normalizedOptions.prepend && !affectedValue.startsWith("\n")) if(prepend && !affectedValue.startsWith("\n"))
{ {
affectedValue = `\n${affectedValue}`; affectedValue = `\n${affectedValue}`;
} }
if(normalizedOptions.append && !affectedValue.endsWith("\n")) if(append && !affectedValue.endsWith("\n"))
{ {
affectedValue = `${affectedValue}\n`; affectedValue = `${affectedValue}\n`;
} }