From a94b58995c9a282396c3a8790d342dbd4aee44b0 Mon Sep 17 00:00:00 2001 From: shimataro Date: Sun, 30 Oct 2022 17:50:42 +0900 Subject: [PATCH] refactor creating ".ssh" directoy --- lib/index.js | 28 ++++++++++++++++++---------- src/main.ts | 29 +++++++++++++++++++---------- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/lib/index.js b/lib/index.js index d52da62..c2d8813 100644 --- a/lib/index.js +++ b/lib/index.js @@ -601,15 +601,10 @@ function main() { 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, - }); + const sshDirName = createSshDirectory(); // files to be created const files = []; - if (shouldCreateKeyFile(path__WEBPACK_IMPORTED_MODULE_1___default().join(dirName, name), ifKeyExists)) { + if (shouldCreateKeyFile(path__WEBPACK_IMPORTED_MODULE_1___default().join(sshDirName, name), ifKeyExists)) { files.push({ name: name, contents: insertLf(key, false, true), @@ -641,14 +636,27 @@ function main() { } // create files for (const file of files) { - const fileName = path__WEBPACK_IMPORTED_MODULE_1___default().join(dirName, file.name); + 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 ${dirName} successfully.`); + 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(); diff --git a/src/main.ts b/src/main.ts index c1d5df4..fef28f1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -25,16 +25,11 @@ function main(): void { 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, - }); + const sshDirName = createSshDirectory(); // files to be created const files: FileInfo[] = []; - if (shouldCreateKeyFile(path.join(dirName, name), ifKeyExists)) { + if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) { files.push({ name: name, contents: insertLf(key, false, true), @@ -67,16 +62,30 @@ function main(): void { // create files for (const file of files) { - const fileName = path.join(dirName, file.name); + const fileName = path.join(sshDirName, file.name); fs.writeFileSync(fileName, file.contents, file.options); } - console.log(`SSH key has been stored to ${dirName} successfully.`); + 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();