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

use module

This commit is contained in:
shimataro 2023-10-10 16:22:19 +09:00
parent 31d4b8b483
commit 83659583bf
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
6 changed files with 434 additions and 284 deletions

View file

@ -3,6 +3,8 @@ import path from "path";
import * as core from "@actions/core";
import * as common from "./common";
interface FileInfo {
name: string;
contents: string;
@ -10,52 +12,15 @@ interface FileInfo {
mustNotExist: boolean; // file must not exist when creating
}
const STATE_BACKUP_SUFFIX = "backup-suffix";
const KNOWN_HOSTS = [
"github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=",
];
try {
main();
} catch (err) {
if (err instanceof Error) {
core.setFailed(err);
}
}
/**
* main function
*/
function main(): void {
if (!isPost()) {
setup();
setPost();
} else {
cleanup();
}
}
/**
* is post process?
* @returns Yes/No
*/
function isPost(): boolean {
return Boolean(core.getState("isPost"));
}
/**
* update post state
*/
function setPost(): void {
core.saveState("isPost", "true");
}
/**
* setup function
*/
function setup(): void {
const backupSuffix = generateBackupSuffix();
export function main(): void {
const backupSuffix = common.generateBackupSuffix();
// parameters
const key = core.getInput("key", {
@ -119,41 +84,10 @@ function setup(): void {
console.log(`SSH key has been stored to ${sshDirName} successfully.`);
if (backedUpFileNames.length > 0) {
console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(", ")}.`);
console.log(`Following files are backed up in suffix "${backupSuffix}"; ${backedUpFileNames.join(", ")}`);
}
}
/**
* cleanup function
*/
function cleanup(): void {
const backupSuffix = core.getState(STATE_BACKUP_SUFFIX);
if (backupSuffix === "") {
// remove ".ssh" directory if suffix is not set
const sshDirName = removeSshDirectory();
console.log(`SSH key in ${sshDirName} has been removed successfully.`);
} else {
restore(backupSuffix);
}
}
/**
* generate backup suffix name
* @returns backup suffix
*/
function generateBackupSuffix(): string {
const dirName = getSshDirectory();
if (!fs.existsSync(dirName)) {
// do nothing if .ssh does not exist
return "";
}
const backupSuffix = `.bak-${Date.now()}`;
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
return backupSuffix;
}
/**
* back up file
* @param fileName file to back up
@ -179,36 +113,12 @@ function backup(fileName: string, backupSuffix: string, removeOrig: boolean): bo
return true;
}
/**
* restore files
* @param backupSuffix suffix of backup directory
*/
function restore(backupSuffix: string): void {
const dirName = getSshDirectory();
const keyFileName = core.getInput("name");
const restoredFileNames: string[] = [];
for (const fileName of ["known_hosts", "config", keyFileName]) {
const pathNameOrg = path.join(dirName, fileName);
const pathNameBak = `${pathNameOrg}${backupSuffix}`;
if (!fs.existsSync(pathNameBak)) {
continue;
}
fs.rmSync(pathNameOrg);
fs.renameSync(pathNameBak, pathNameOrg);
restoredFileNames.push(fileName);
}
console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`);
}
/**
* create ".ssh" directory
* @returns directory name
*/
function createSshDirectory(): string {
const dirName = getSshDirectory();
const dirName = common.getSshDirectory();
fs.mkdirSync(dirName, {
recursive: true,
mode: 0o700,
@ -216,60 +126,6 @@ function createSshDirectory(): string {
return dirName;
}
/**
* remove ".ssh" directory
* @returns removed directory name
*/
function removeSshDirectory(): string {
const dirName = getSshDirectory();
fs.rmSync(dirName, {
recursive: true,
force: true,
});
return dirName;
}
/**
* get SSH directory
* @returns SSH directory name
*/
function getSshDirectory(): string {
return path.resolve(getHomeDirectory(), ".ssh");
}
/**
* get 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`);
}
if (home === "/github/home") {
// Docker container
return "/root";
}
return home;
}
/**
* get HOME environment name
* @returns HOME environment name
*/
function getHomeEnv(): string {
if (process.platform === "win32") {
// Windows
return "USERPROFILE";
}
// macOS / Linux
return "HOME";
}
/**
* prepend/append LF to value if not empty
* @param value the value to insert LF