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:
parent
0ea914c065
commit
c6162f319b
2 changed files with 50 additions and 46 deletions
28
lib/index.js
28
lib/index.js
|
@ -430,8 +430,8 @@ function main() {
|
||||||
const files = [
|
const files = [
|
||||||
{
|
{
|
||||||
name: core.getInput("name"),
|
name: core.getInput("name"),
|
||||||
contents: core.getInput("key", {
|
contents: insertLf(core.getInput("key", { required: true }), {
|
||||||
required: true,
|
append: true,
|
||||||
}),
|
}),
|
||||||
options: {
|
options: {
|
||||||
mode: 0o400,
|
mode: 0o400,
|
||||||
|
@ -440,9 +440,10 @@ function main() {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "known_hosts",
|
name: "known_hosts",
|
||||||
contents: insertLf(core.getInput("known_hosts", {
|
contents: insertLf(core.getInput("known_hosts", { required: true }), {
|
||||||
required: true,
|
prepend: true,
|
||||||
})),
|
append: true,
|
||||||
|
}),
|
||||||
options: {
|
options: {
|
||||||
mode: 0o644,
|
mode: 0o644,
|
||||||
flag: "a",
|
flag: "a",
|
||||||
|
@ -450,7 +451,10 @@ function main() {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "config",
|
name: "config",
|
||||||
contents: insertLf(core.getInput("config")),
|
contents: insertLf(core.getInput("config"), {
|
||||||
|
prepend: true,
|
||||||
|
append: true,
|
||||||
|
}),
|
||||||
options: {
|
options: {
|
||||||
mode: 0o644,
|
mode: 0o644,
|
||||||
flag: "a",
|
flag: "a",
|
||||||
|
@ -505,19 +509,21 @@ function getHomeEnv() {
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* prepend/append LF to value if not empty
|
* prepend/append LF to value if not empty
|
||||||
* @param value the value to prepend LF
|
* @param value the value to insert LF
|
||||||
* @returns prepended value
|
* @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;
|
let affectedValue = value;
|
||||||
if (value.length === 0) {
|
if (value.length === 0) {
|
||||||
// do nothing if empty
|
// do nothing if empty
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if (!affectedValue.startsWith("\n")) {
|
if (normalizedOptions.prepend && !affectedValue.startsWith("\n")) {
|
||||||
affectedValue = `\n${affectedValue}`;
|
affectedValue = `\n${affectedValue}`;
|
||||||
}
|
}
|
||||||
if (!affectedValue.endsWith("\n")) {
|
if (normalizedOptions.append && !affectedValue.endsWith("\n")) {
|
||||||
affectedValue = `${affectedValue}\n`;
|
affectedValue = `${affectedValue}\n`;
|
||||||
}
|
}
|
||||||
return affectedValue;
|
return affectedValue;
|
||||||
|
|
68
src/main.ts
68
src/main.ts
|
@ -10,6 +10,13 @@ interface FileInfo
|
||||||
options: fs.WriteFileOptions;
|
options: fs.WriteFileOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** options for insertLf() */
|
||||||
|
interface InsertLfOptions
|
||||||
|
{
|
||||||
|
prepend?: boolean;
|
||||||
|
append?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* main function
|
* main function
|
||||||
*/
|
*/
|
||||||
|
@ -20,9 +27,9 @@ function main(): void
|
||||||
const files: FileInfo[] = [
|
const files: FileInfo[] = [
|
||||||
{
|
{
|
||||||
name: core.getInput("name"),
|
name: core.getInput("name"),
|
||||||
contents: insertLfToEnd(core.getInput("key", {
|
contents: insertLf(core.getInput("key", {required: true}), {
|
||||||
required: true,
|
append: true,
|
||||||
})),
|
}),
|
||||||
options: {
|
options: {
|
||||||
mode: 0o400,
|
mode: 0o400,
|
||||||
flag: "ax",
|
flag: "ax",
|
||||||
|
@ -30,9 +37,10 @@ function main(): void
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "known_hosts",
|
name: "known_hosts",
|
||||||
contents: insertLf(core.getInput("known_hosts", {
|
contents: insertLf(core.getInput("known_hosts", {required: true}), {
|
||||||
required: true,
|
prepend: true,
|
||||||
})),
|
append: true,
|
||||||
|
}),
|
||||||
options: {
|
options: {
|
||||||
mode: 0o644,
|
mode: 0o644,
|
||||||
flag: "a",
|
flag: "a",
|
||||||
|
@ -40,7 +48,10 @@ function main(): void
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "config",
|
name: "config",
|
||||||
contents: insertLf(core.getInput("config")),
|
contents: insertLf(core.getInput("config"), {
|
||||||
|
prepend: true,
|
||||||
|
append: true,
|
||||||
|
}),
|
||||||
options: {
|
options: {
|
||||||
mode: 0o644,
|
mode: 0o644,
|
||||||
flag: "a",
|
flag: "a",
|
||||||
|
@ -110,12 +121,18 @@ function getHomeEnv(): string
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* append LF to value to the end if not empty
|
* prepend/append LF to value if not empty
|
||||||
* @param value the value to prepend LF
|
* @param value the value to insert LF
|
||||||
* @returns prepended value
|
* @param options options
|
||||||
|
* @returns new value
|
||||||
*/
|
*/
|
||||||
function insertLfToEnd(value: string): string
|
function insertLf(value: string, options: InsertLfOptions): string
|
||||||
{
|
{
|
||||||
|
const normalizedOptions: Required<InsertLfOptions> = {
|
||||||
|
prepend: false,
|
||||||
|
append: false,
|
||||||
|
...options,
|
||||||
|
};
|
||||||
let affectedValue = value;
|
let affectedValue = value;
|
||||||
|
|
||||||
if(value.length === 0)
|
if(value.length === 0)
|
||||||
|
@ -123,7 +140,11 @@ function insertLfToEnd(value: string): string
|
||||||
// do nothing if empty
|
// do nothing if empty
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if(!affectedValue.endsWith("\n"))
|
if(normalizedOptions.prepend && !affectedValue.startsWith("\n"))
|
||||||
|
{
|
||||||
|
affectedValue = `\n${affectedValue}`;
|
||||||
|
}
|
||||||
|
if(normalizedOptions.append && !affectedValue.endsWith("\n"))
|
||||||
{
|
{
|
||||||
affectedValue = `${affectedValue}\n`;
|
affectedValue = `${affectedValue}\n`;
|
||||||
}
|
}
|
||||||
|
@ -131,27 +152,4 @@ function insertLfToEnd(value: string): string
|
||||||
return affectedValue;
|
return affectedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* prepend/append LF to value if not empty
|
|
||||||
* @param value the value to prepend LF
|
|
||||||
* @returns prepended value
|
|
||||||
*/
|
|
||||||
function insertLf(value: string): string
|
|
||||||
{
|
|
||||||
let affectedValue = value;
|
|
||||||
|
|
||||||
if(value.length === 0)
|
|
||||||
{
|
|
||||||
// do nothing if empty
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
if(!affectedValue.startsWith("\n"))
|
|
||||||
{
|
|
||||||
affectedValue = `\n${affectedValue}`;
|
|
||||||
}
|
|
||||||
affectedValue = insertLfToEnd(affectedValue);
|
|
||||||
|
|
||||||
return affectedValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue