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
394
lib/index.js
394
lib/index.js
|
@ -2701,6 +2701,173 @@ function version(uuid) {
|
|||
var _default = version;
|
||||
exports["default"] = _default;
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 108:
|
||||
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.getSshDirectory = exports.getBackupSuffix = exports.generateBackupSuffix = exports.getPhase = void 0;
|
||||
const fs_1 = __importDefault(__nccwpck_require__(147));
|
||||
const path_1 = __importDefault(__nccwpck_require__(17));
|
||||
const core = __importStar(__nccwpck_require__(186));
|
||||
const STATE_PHASE = "phase";
|
||||
const STATE_BACKUP_SUFFIX = "backup-suffix";
|
||||
/**
|
||||
* current phase
|
||||
* @returns phase
|
||||
*/
|
||||
function getPhase() {
|
||||
const phase = core.getState(STATE_PHASE);
|
||||
// next: post
|
||||
core.saveState(STATE_PHASE, "post");
|
||||
if (phase === "") {
|
||||
return "main";
|
||||
}
|
||||
return phase;
|
||||
}
|
||||
exports.getPhase = getPhase;
|
||||
/**
|
||||
* generate backup suffix name
|
||||
* @returns backup suffix
|
||||
*/
|
||||
function generateBackupSuffix() {
|
||||
const dirName = getSshDirectory();
|
||||
if (!fs_1.default.existsSync(dirName)) {
|
||||
// do nothing if .ssh does not exist
|
||||
return "";
|
||||
}
|
||||
const backupSuffix = `.bak-${Date.now()}`;
|
||||
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
|
||||
return backupSuffix;
|
||||
}
|
||||
exports.generateBackupSuffix = generateBackupSuffix;
|
||||
/**
|
||||
* get backup suffix name
|
||||
* @returns backup suffix (if not, empty string)
|
||||
*/
|
||||
function getBackupSuffix() {
|
||||
return core.getState(STATE_BACKUP_SUFFIX);
|
||||
}
|
||||
exports.getBackupSuffix = getBackupSuffix;
|
||||
/**
|
||||
* get SSH directory
|
||||
* @returns SSH directory name
|
||||
*/
|
||||
function getSshDirectory() {
|
||||
return path_1.default.resolve(getHomeDirectory(), ".ssh");
|
||||
}
|
||||
exports.getSshDirectory = getSshDirectory;
|
||||
/**
|
||||
* get home directory
|
||||
* @returns home directory name
|
||||
*/
|
||||
function getHomeDirectory() {
|
||||
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() {
|
||||
if (process.platform === "win32") {
|
||||
// Windows
|
||||
return "USERPROFILE";
|
||||
}
|
||||
// macOS / Linux
|
||||
return "HOME";
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 144:
|
||||
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
const core = __importStar(__nccwpck_require__(186));
|
||||
const common = __importStar(__nccwpck_require__(108));
|
||||
const main_1 = __nccwpck_require__(399);
|
||||
const post_1 = __nccwpck_require__(51);
|
||||
try {
|
||||
switch (common.getPhase()) {
|
||||
case "main":
|
||||
(0, main_1.main)();
|
||||
break;
|
||||
case "post":
|
||||
(0, post_1.post)();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (err) {
|
||||
if (err instanceof Error) {
|
||||
core.setFailed(err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 399:
|
||||
|
@ -2735,51 +2902,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.main = void 0;
|
||||
const fs_1 = __importDefault(__nccwpck_require__(147));
|
||||
const path_1 = __importDefault(__nccwpck_require__(17));
|
||||
const core = __importStar(__nccwpck_require__(186));
|
||||
const STATE_BACKUP_SUFFIX = "backup-suffix";
|
||||
const common = __importStar(__nccwpck_require__(108));
|
||||
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() {
|
||||
if (!isPost()) {
|
||||
setup();
|
||||
setPost();
|
||||
}
|
||||
else {
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* is post process?
|
||||
* @returns Yes/No
|
||||
*/
|
||||
function isPost() {
|
||||
return Boolean(core.getState("isPost"));
|
||||
}
|
||||
/**
|
||||
* update post state
|
||||
*/
|
||||
function setPost() {
|
||||
core.saveState("isPost", "true");
|
||||
}
|
||||
/**
|
||||
* setup function
|
||||
*/
|
||||
function setup() {
|
||||
const backupSuffix = generateBackupSuffix();
|
||||
const backupSuffix = common.generateBackupSuffix();
|
||||
// parameters
|
||||
const key = core.getInput("key", {
|
||||
required: true,
|
||||
|
@ -2837,37 +2972,10 @@ function setup() {
|
|||
}
|
||||
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() {
|
||||
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() {
|
||||
const dirName = getSshDirectory();
|
||||
if (!fs_1.default.existsSync(dirName)) {
|
||||
// do nothing if .ssh does not exist
|
||||
return "";
|
||||
}
|
||||
const backupSuffix = `.bak-${Date.now()}`;
|
||||
core.saveState(STATE_BACKUP_SUFFIX, backupSuffix);
|
||||
return backupSuffix;
|
||||
}
|
||||
exports.main = main;
|
||||
/**
|
||||
* back up file
|
||||
* @param fileName file to back up
|
||||
|
@ -2890,85 +2998,18 @@ function backup(fileName, backupSuffix, removeOrig) {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* restore files
|
||||
* @param backupSuffix suffix of backup directory
|
||||
*/
|
||||
function restore(backupSuffix) {
|
||||
const dirName = getSshDirectory();
|
||||
const keyFileName = core.getInput("name");
|
||||
const restoredFileNames = [];
|
||||
for (const fileName of ["known_hosts", "config", keyFileName]) {
|
||||
const pathNameOrg = path_1.default.join(dirName, fileName);
|
||||
const pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
if (!fs_1.default.existsSync(pathNameBak)) {
|
||||
continue;
|
||||
}
|
||||
fs_1.default.rmSync(pathNameOrg);
|
||||
fs_1.default.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() {
|
||||
const dirName = getSshDirectory();
|
||||
const dirName = common.getSshDirectory();
|
||||
fs_1.default.mkdirSync(dirName, {
|
||||
recursive: true,
|
||||
mode: 0o700,
|
||||
});
|
||||
return dirName;
|
||||
}
|
||||
/**
|
||||
* remove ".ssh" directory
|
||||
* @returns removed directory name
|
||||
*/
|
||||
function removeSshDirectory() {
|
||||
const dirName = getSshDirectory();
|
||||
fs_1.default.rmSync(dirName, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
});
|
||||
return dirName;
|
||||
}
|
||||
/**
|
||||
* get SSH directory
|
||||
* @returns SSH directory name
|
||||
*/
|
||||
function getSshDirectory() {
|
||||
return path_1.default.resolve(getHomeDirectory(), ".ssh");
|
||||
}
|
||||
/**
|
||||
* get home directory
|
||||
* @returns home directory name
|
||||
*/
|
||||
function getHomeDirectory() {
|
||||
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() {
|
||||
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
|
||||
|
@ -3026,6 +3067,93 @@ function buildKnownHostsArray(knownHosts) {
|
|||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 51:
|
||||
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||
|
||||
"use strict";
|
||||
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
exports.post = void 0;
|
||||
const fs_1 = __importDefault(__nccwpck_require__(147));
|
||||
const path_1 = __importDefault(__nccwpck_require__(17));
|
||||
const core = __importStar(__nccwpck_require__(186));
|
||||
const common = __importStar(__nccwpck_require__(108));
|
||||
/**
|
||||
* cleanup function
|
||||
*/
|
||||
function post() {
|
||||
const backupSuffix = common.getBackupSuffix();
|
||||
if (backupSuffix === "") {
|
||||
// remove ".ssh" directory if suffix is not set
|
||||
removeSshDirectory();
|
||||
}
|
||||
else {
|
||||
// restore files from backup suffix
|
||||
restore(backupSuffix);
|
||||
}
|
||||
}
|
||||
exports.post = post;
|
||||
/**
|
||||
* remove ".ssh" directory
|
||||
*/
|
||||
function removeSshDirectory() {
|
||||
const dirName = common.getSshDirectory();
|
||||
fs_1.default.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) {
|
||||
const dirName = common.getSshDirectory();
|
||||
const keyFileName = core.getInput("name");
|
||||
const restoredFileNames = [];
|
||||
for (const fileName of ["known_hosts", "config", keyFileName]) {
|
||||
const pathNameOrg = path_1.default.join(dirName, fileName);
|
||||
const pathNameBak = `${pathNameOrg}${backupSuffix}`;
|
||||
if (!fs_1.default.existsSync(pathNameBak)) {
|
||||
continue;
|
||||
}
|
||||
fs_1.default.rmSync(pathNameOrg);
|
||||
fs_1.default.renameSync(pathNameBak, pathNameOrg);
|
||||
restoredFileNames.push(fileName);
|
||||
}
|
||||
console.log(`Following files in suffix "${backupSuffix}" are restored; ${restoredFileNames.join(", ")}`);
|
||||
}
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 491:
|
||||
|
@ -3158,7 +3286,7 @@ module.exports = require("util");
|
|||
/******/ // startup
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ // This entry module is referenced by other modules so it can't be inlined
|
||||
/******/ var __webpack_exports__ = __nccwpck_require__(399);
|
||||
/******/ var __webpack_exports__ = __nccwpck_require__(144);
|
||||
/******/ module.exports = __webpack_exports__;
|
||||
/******/
|
||||
/******/ })()
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
"npm": ">=5.7.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "ncc build src/main.ts -o lib",
|
||||
"build": "ncc build src/index.ts -o lib",
|
||||
"check-updates": "ncu",
|
||||
"lint": "run-p lint:*",
|
||||
"lint:ts": "eslint ./src --ext .ts",
|
||||
|
|
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