1
0
Fork 0
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:
shimataro 2022-10-30 19:57:06 +09:00 committed by GitHub
parent b38d88da8a
commit adea214356
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 324 additions and 247 deletions

View file

@ -586,74 +586,85 @@ __nccwpck_require__.r(__webpack_exports__);
try {
main();
}
catch (err) {
if (err instanceof Error) {
_actions_core__WEBPACK_IMPORTED_MODULE_2__.setFailed(err);
}
}
/**
* main function
*/
function main() {
try {
// parameters
const key = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("key", {
required: true,
// parameters
const key = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("key", {
required: true,
});
const name = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("name");
const knownHosts = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("known_hosts", {
required: true,
});
const config = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("config");
const ifKeyExists = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("if_key_exists");
// create ".ssh" directory
const sshDirName = createSshDirectory();
// files to be created
const files = [];
if (shouldCreateKeyFile(path__WEBPACK_IMPORTED_MODULE_1___default().join(sshDirName, name), ifKeyExists)) {
files.push({
name: name,
contents: insertLf(key, false, true),
options: {
mode: 0o400,
flag: "wx",
},
});
const name = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("name");
const knownHosts = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("known_hosts", {
required: true,
});
const config = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("config");
const ifKeyExists = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("if_key_exists");
// create ".ssh" directory
const home = getHomeDirectory();
const dirName = path__WEBPACK_IMPORTED_MODULE_1___default().resolve(home, ".ssh");
fs__WEBPACK_IMPORTED_MODULE_0___default().mkdirSync(dirName, {
recursive: true,
mode: 0o700,
});
// files to be created
const files = [];
if (shouldCreateKeyFile(path__WEBPACK_IMPORTED_MODULE_1___default().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",
},
});
}
// create files
for (const file of files) {
const fileName = path__WEBPACK_IMPORTED_MODULE_1___default().join(dirName, file.name);
fs__WEBPACK_IMPORTED_MODULE_0___default().writeFileSync(fileName, file.contents, file.options);
}
console.log(`SSH key has been stored to ${dirName} successfully.`);
}
catch (err) {
_actions_core__WEBPACK_IMPORTED_MODULE_2__.setFailed(err.message);
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__WEBPACK_IMPORTED_MODULE_1___default().join(sshDirName, file.name);
fs__WEBPACK_IMPORTED_MODULE_0___default().writeFileSync(fileName, file.contents, file.options);
}
console.log(`SSH key has been stored to ${sshDirName} successfully.`);
}
/**
* create ".ssh" directory
* @returns directory name
*/
function createSshDirectory() {
const home = getHomeDirectory();
const dirName = path__WEBPACK_IMPORTED_MODULE_1___default().resolve(home, ".ssh");
fs__WEBPACK_IMPORTED_MODULE_0___default().mkdirSync(dirName, {
recursive: true,
mode: 0o700,
});
return dirName;
}
/**
* get home directory
* @returns home directory
* @returns home directory name
*/
function getHomeDirectory() {
const homeEnv = getHomeEnv();
@ -724,7 +735,6 @@ function shouldCreateKeyFile(keyFilePath, ifKeyExists) {
throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`);
}
}
main();
})();