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-23 09:41:34 +09:00
parent 0ea914c065
commit c6162f319b
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
2 changed files with 50 additions and 46 deletions

View file

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