mirror of
https://github.com/shimataro/ssh-key-action.git
synced 2025-06-19 22:52:10 +10:00
Feature/refactor (#209)
* Change code style to 1TBS
* refactor creating ".ssh" directoy
* Add Windows-2022 / macOS-11 / macOS-12 / Ubuntu-22.04
Drop Ubuntu-16.04
* Drop centos:8 (Docker container)
* add CentOS 8 Stream (Docker container)
* use YAML alias
* Revert "use YAML alias"
This reverts commit 1ddbc7fde8
.
* update .eslintrc
* move calling main to front
This commit is contained in:
parent
b38d88da8a
commit
adea214356
13 changed files with 324 additions and 247 deletions
262
src/main.ts
262
src/main.ts
|
@ -3,126 +3,125 @@ import path from "path";
|
|||
|
||||
import * as core from "@actions/core";
|
||||
|
||||
interface FileInfo
|
||||
{
|
||||
name: string;
|
||||
contents: string;
|
||||
options: fs.WriteFileOptions;
|
||||
interface FileInfo {
|
||||
name: string;
|
||||
contents: string;
|
||||
options: fs.WriteFileOptions;
|
||||
}
|
||||
|
||||
try {
|
||||
main();
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
core.setFailed(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* main function
|
||||
*/
|
||||
function main(): void
|
||||
{
|
||||
try
|
||||
{
|
||||
// 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");
|
||||
function main(): void {
|
||||
// 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.resolve(home, ".ssh");
|
||||
fs.mkdirSync(dirName, {
|
||||
recursive: true,
|
||||
mode: 0o700,
|
||||
});
|
||||
// create ".ssh" directory
|
||||
const sshDirName = createSshDirectory();
|
||||
|
||||
// files to be created
|
||||
const files: FileInfo[] = [];
|
||||
if(shouldCreateKeyFile(path.join(dirName, name), ifKeyExists))
|
||||
{
|
||||
files.push({
|
||||
name: name,
|
||||
contents: insertLf(key, false, true),
|
||||
options: {
|
||||
mode: 0o400,
|
||||
flag: "wx",
|
||||
},
|
||||
});
|
||||
}
|
||||
if(knownHosts !== "unnecessary")
|
||||
{
|
||||
files.push({
|
||||
name: "known_hosts",
|
||||
contents: insertLf(knownHosts, true, true),
|
||||
options: {
|
||||
mode: 0o644,
|
||||
flag: "a",
|
||||
},
|
||||
});
|
||||
}
|
||||
if(config !== "")
|
||||
{
|
||||
files.push({
|
||||
name: "config",
|
||||
contents: insertLf(config, true, true),
|
||||
options: {
|
||||
mode: 0o644,
|
||||
flag: "a",
|
||||
},
|
||||
});
|
||||
}
|
||||
// files to be created
|
||||
const files: FileInfo[] = [];
|
||||
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
|
||||
files.push({
|
||||
name: name,
|
||||
contents: insertLf(key, false, true),
|
||||
options: {
|
||||
mode: 0o400,
|
||||
flag: "wx",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (knownHosts !== "unnecessary") {
|
||||
files.push({
|
||||
name: "known_hosts",
|
||||
contents: insertLf(knownHosts, true, true),
|
||||
options: {
|
||||
mode: 0o644,
|
||||
flag: "a",
|
||||
},
|
||||
});
|
||||
}
|
||||
if (config !== "") {
|
||||
files.push({
|
||||
name: "config",
|
||||
contents: insertLf(config, true, true),
|
||||
options: {
|
||||
mode: 0o644,
|
||||
flag: "a",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// create files
|
||||
for(const file of files)
|
||||
{
|
||||
const fileName = path.join(dirName, file.name);
|
||||
fs.writeFileSync(fileName, file.contents, file.options);
|
||||
}
|
||||
// create files
|
||||
for (const file of files) {
|
||||
const fileName = path.join(sshDirName, file.name);
|
||||
fs.writeFileSync(fileName, file.contents, file.options);
|
||||
}
|
||||
|
||||
console.log(`SSH key has been stored to ${dirName} successfully.`);
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
core.setFailed(err.message);
|
||||
}
|
||||
console.log(`SSH key has been stored to ${sshDirName} successfully.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* create ".ssh" directory
|
||||
* @returns directory name
|
||||
*/
|
||||
function createSshDirectory(): string {
|
||||
const home = getHomeDirectory();
|
||||
const dirName = path.resolve(home, ".ssh");
|
||||
fs.mkdirSync(dirName, {
|
||||
recursive: true,
|
||||
mode: 0o700,
|
||||
});
|
||||
return dirName;
|
||||
}
|
||||
|
||||
/**
|
||||
* get home directory
|
||||
* @returns home directory
|
||||
* @returns home directory name
|
||||
*/
|
||||
function getHomeDirectory(): string
|
||||
{
|
||||
const homeEnv = getHomeEnv();
|
||||
const home = process.env[homeEnv];
|
||||
if(home === undefined)
|
||||
{
|
||||
throw Error(`${homeEnv} is not defined`);
|
||||
}
|
||||
function getHomeDirectory(): string {
|
||||
const homeEnv = getHomeEnv();
|
||||
const home = process.env[homeEnv];
|
||||
if (home === undefined) {
|
||||
throw Error(`${homeEnv} is not defined`);
|
||||
}
|
||||
|
||||
if(home === "/github/home")
|
||||
{
|
||||
// Docker container
|
||||
return "/root";
|
||||
}
|
||||
if (home === "/github/home") {
|
||||
// Docker container
|
||||
return "/root";
|
||||
}
|
||||
|
||||
return home;
|
||||
return home;
|
||||
}
|
||||
|
||||
/**
|
||||
* get HOME environment name
|
||||
* @returns HOME environment name
|
||||
*/
|
||||
function getHomeEnv(): string
|
||||
{
|
||||
if(process.platform === "win32")
|
||||
{
|
||||
// Windows
|
||||
return "USERPROFILE";
|
||||
}
|
||||
function getHomeEnv(): string {
|
||||
if (process.platform === "win32") {
|
||||
// Windows
|
||||
return "USERPROFILE";
|
||||
}
|
||||
|
||||
// macOS / Linux
|
||||
return "HOME";
|
||||
// macOS / Linux
|
||||
return "HOME";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -132,25 +131,21 @@ function getHomeEnv(): string
|
|||
* @param append true to append
|
||||
* @returns new value
|
||||
*/
|
||||
function insertLf(value: string, prepend: boolean, append: boolean): string
|
||||
{
|
||||
let affectedValue = value;
|
||||
function insertLf(value: string, prepend: boolean, append: boolean): string {
|
||||
let affectedValue = value;
|
||||
|
||||
if(value.length === 0)
|
||||
{
|
||||
// do nothing if empty
|
||||
return "";
|
||||
}
|
||||
if(prepend && !affectedValue.startsWith("\n"))
|
||||
{
|
||||
affectedValue = `\n${affectedValue}`;
|
||||
}
|
||||
if(append && !affectedValue.endsWith("\n"))
|
||||
{
|
||||
affectedValue = `${affectedValue}\n`;
|
||||
}
|
||||
if (value.length === 0) {
|
||||
// do nothing if empty
|
||||
return "";
|
||||
}
|
||||
if (prepend && !affectedValue.startsWith("\n")) {
|
||||
affectedValue = `\n${affectedValue}`;
|
||||
}
|
||||
if (append && !affectedValue.endsWith("\n")) {
|
||||
affectedValue = `${affectedValue}\n`;
|
||||
}
|
||||
|
||||
return affectedValue;
|
||||
return affectedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -159,29 +154,24 @@ function insertLf(value: string, prepend: boolean, append: boolean): string
|
|||
* @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;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
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.`);
|
||||
}
|
||||
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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue