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:
parent
31d4b8b483
commit
83659583bf
6 changed files with 434 additions and 284 deletions
88
src/common.ts
Normal file
88
src/common.ts
Normal file
|
@ -0,0 +1,88 @@
|
|||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
import * as core from "@actions/core";
|
||||
|
||||
const STATE_PHASE = "phase";
|
||||
const STATE_BACKUP_SUFFIX = "backup-suffix";
|
||||
|
||||
/**
|
||||
* current phase
|
||||
* @returns phase
|
||||
*/
|
||||
export function getPhase(): string {
|
||||
const phase = core.getState(STATE_PHASE);
|
||||
|
||||
// next: post
|
||||
core.saveState(STATE_PHASE, "post");
|
||||
|
||||
if (phase === "") {
|
||||
return "main";
|
||||
}
|
||||
return phase;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate backup suffix name
|
||||
* @returns backup suffix
|
||||
*/
|
||||
export 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* get backup suffix name
|
||||
* @returns backup suffix (if not, empty string)
|
||||
*/
|
||||
export function getBackupSuffix(): string {
|
||||
return core.getState(STATE_BACKUP_SUFFIX);
|
||||
}
|
||||
|
||||
/**
|
||||
* get SSH directory
|
||||
* @returns SSH directory name
|
||||
*/
|
||||
export 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";
|
||||
}
|
21
src/index.ts
Normal file
21
src/index.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import * as core from "@actions/core";
|
||||
|
||||
import * as common from "./common";
|
||||
import {main} from "./main";
|
||||
import {post} from "./post";
|
||||
|
||||
try {
|
||||
switch (common.getPhase()) {
|
||||
case "main":
|
||||
main();
|
||||
break;
|
||||
|
||||
case "post":
|
||||
post();
|
||||
break;
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof Error) {
|
||||
core.setFailed(err);
|
||||
}
|
||||
}
|
156
src/main.ts
156
src/main.ts
|
@ -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
|
||||
|
|
57
src/post.ts
Normal file
57
src/post.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
import * as core from "@actions/core";
|
||||
|
||||
import * as common from "./common";
|
||||
|
||||
/**
|
||||
* cleanup function
|
||||
*/
|
||||
export function post(): void {
|
||||
const backupSuffix = common.getBackupSuffix();
|
||||
if (backupSuffix === "") {
|
||||
// remove ".ssh" directory if suffix is not set
|
||||
removeSshDirectory();
|
||||
} else {
|
||||
// restore files from backup suffix
|
||||
restore(backupSuffix);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* remove ".ssh" directory
|
||||
*/
|
||||
function removeSshDirectory(): void {
|
||||
const dirName = common.getSshDirectory();
|
||||
fs.rmSync(dirName, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
});
|
||||
|
||||
console.log(`SSH key in ${dirName} has been removed successfully.`);
|
||||
}
|
||||
|
||||
/**
|
||||
* restore files
|
||||
* @param backupSuffix suffix of backup directory
|
||||
*/
|
||||
function restore(backupSuffix: string): void {
|
||||
const dirName = common.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(", ")}`);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue