diff --git a/.editorconfig b/.editorconfig index 0d46254..b40d053 100644 --- a/.editorconfig +++ b/.editorconfig @@ -25,6 +25,13 @@ spaces_around_brackets = none indent_brace_style = allman +# JavaScript/TypeScript +[*.{js,ts}] +indent_style = space +curly_bracket_next_line = false +indent_brace_style = K&R + + # JSON/YAML [*.{json,babelrc,code-workspace,yml,yaml}] indent_style = space diff --git a/.eslintrc.yml b/.eslintrc.yml index 7cea633..59697b3 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -29,9 +29,7 @@ rules: # https://eslint.org/docs/rules/ before: true block-scoped-var: error block-spacing: error - brace-style: - - error - - allman + brace-style: 'off' # see "@typescript-eslint/brace-style" callback-return: error capitalized-comments: 'off' class-methods-use-this: error @@ -87,26 +85,14 @@ rules: # https://eslint.org/docs/rules/ - below indent: - error - - tab + - 4 + - SwitchCase: 1 indent-legacy: 'off' init-declarations: error jsx-quotes: error key-spacing: error keyword-spacing: - error - - overrides: - catch: - after: false - for: - after: false - if: - after: false - switch: - after: false - while: - after: false - with: - after: false line-comment-position: 'off' linebreak-style: - error @@ -256,7 +242,7 @@ rules: # https://eslint.org/docs/rules/ rest-spread-spacing: - error - never - semi: error + semi: 'off' # see "@typescript-eslint/semi" semi-spacing: error semi-style: - error @@ -308,8 +294,21 @@ rules: # https://eslint.org/docs/rules/ # @typescript-eslint plugin "@typescript-eslint/ban-ts-ignore": 'off' + "@typescript-eslint/brace-style": + - error + - 1tbs + "@typescript-eslint/member-delimiter-style": + - error + - multiline: + delimiter: semi + requireLast: true + singleline: + delimiter: semi + requireLast: true "@typescript-eslint/no-empty-interface": 'off' + "@typescript-eslint/no-floating-promises": error "@typescript-eslint/no-use-before-define": - error - functions: false + "@typescript-eslint/semi": error "@typescript-eslint/strict-boolean-expressions": error diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c8ddc4f..2561371 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,10 +13,13 @@ jobs: matrix: os: - Windows-2019 + - Windows-2022 - macOS-10.15 - - Ubuntu-16.04 + - macOS-11 + - macOS-12 - Ubuntu-18.04 - Ubuntu-20.04 + - Ubuntu-22.04 nodejs: - 12 fail-fast: false diff --git a/.github/workflows/verify-on-container-alpine.yml b/.github/workflows/verify-on-container-alpine.yml index 762518b..e716f3b 100644 --- a/.github/workflows/verify-on-container-alpine.yml +++ b/.github/workflows/verify-on-container-alpine.yml @@ -15,6 +15,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -44,6 +45,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -73,6 +75,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -104,6 +107,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -139,6 +143,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -170,6 +175,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -205,6 +211,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -236,6 +243,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -272,6 +280,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 @@ -303,6 +312,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - alpine:3.10 - alpine:3.11 diff --git a/.github/workflows/verify-on-container-centos.yml b/.github/workflows/verify-on-container-centos.yml index dc45b81..8318e4a 100644 --- a/.github/workflows/verify-on-container-centos.yml +++ b/.github/workflows/verify-on-container-centos.yml @@ -15,9 +15,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 fail-fast: false steps: - name: Install packages @@ -43,9 +44,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 fail-fast: false steps: - name: Install packages @@ -71,9 +73,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 fail-fast: false steps: - name: Install packages @@ -101,9 +104,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 steps: - name: Install packages run: | @@ -135,9 +139,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 steps: - name: Install packages run: | @@ -165,9 +170,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 steps: - name: Install packages run: | @@ -199,9 +205,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 steps: - name: Install packages run: | @@ -229,9 +236,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 steps: - name: Install packages run: | @@ -264,9 +272,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 steps: - name: Install packages run: | @@ -294,9 +303,10 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - - centos:7 - - centos:8 + - quay.io/centos/centos:centos7 + - quay.io/centos/centos:stream8 steps: - name: Install packages run: | diff --git a/.github/workflows/verify-on-container-ubuntu.yml b/.github/workflows/verify-on-container-ubuntu.yml index ed512e2..1abf004 100644 --- a/.github/workflows/verify-on-container-ubuntu.yml +++ b/.github/workflows/verify-on-container-ubuntu.yml @@ -15,10 +15,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 fail-fast: false steps: - name: Install packages @@ -45,10 +47,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 fail-fast: false steps: - name: Install packages @@ -75,10 +79,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 fail-fast: false steps: - name: Install packages @@ -107,10 +113,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 steps: - name: Install packages run: | @@ -143,10 +151,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 steps: - name: Install packages run: | @@ -175,10 +185,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 steps: - name: Install packages run: | @@ -211,10 +223,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 steps: - name: Install packages run: | @@ -243,10 +257,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 steps: - name: Install packages run: | @@ -280,10 +296,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 steps: - name: Install packages run: | @@ -312,10 +330,12 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 container: - ubuntu:16.04 - ubuntu:18.04 - ubuntu:20.04 + - ubuntu:22.04 steps: - name: Install packages run: | diff --git a/.github/workflows/verify-on-macos.yml b/.github/workflows/verify-on-macos.yml index 251147a..e1b7e82 100644 --- a/.github/workflows/verify-on-macos.yml +++ b/.github/workflows/verify-on-macos.yml @@ -14,6 +14,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -34,6 +36,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -54,6 +58,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -74,6 +80,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -101,6 +109,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -127,6 +137,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -149,6 +161,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -175,6 +189,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -197,6 +213,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -224,6 +242,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -246,6 +266,8 @@ jobs: matrix: os: - macos-10.15 + - macos-11 + - macos-12 steps: - name: Checkout source codes uses: actions/checkout@v2 diff --git a/.github/workflows/verify-on-ubuntu.yml b/.github/workflows/verify-on-ubuntu.yml index 67dc3f6..f31c00d 100644 --- a/.github/workflows/verify-on-ubuntu.yml +++ b/.github/workflows/verify-on-ubuntu.yml @@ -36,6 +36,7 @@ jobs: os: - ubuntu-18.04 - ubuntu-20.04 + - ubuntu-22.04 steps: - name: Checkout source codes uses: actions/checkout@v2 diff --git a/.github/workflows/verify-on-windows.yml b/.github/workflows/verify-on-windows.yml index adadd87..5dc222a 100644 --- a/.github/workflows/verify-on-windows.yml +++ b/.github/workflows/verify-on-windows.yml @@ -13,8 +13,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -34,8 +34,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -55,8 +55,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -76,8 +76,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -104,8 +104,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -131,8 +131,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -154,8 +154,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -181,8 +181,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -204,8 +204,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -232,8 +232,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 @@ -255,8 +255,8 @@ jobs: fail-fast: false matrix: os: - - windows-2016 - windows-2019 + - windows-2022 steps: - name: Checkout source codes uses: actions/checkout@v2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dde2b4..f5eb524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Others + +* add `windows-2022`, `macos-11`, `macos-12`, `ubuntu-22.04`, and `CentOS 8 Stream (Docker container)` +* drop `ubuntu-16.04`, and `CentOS 8 (Docker container)` + ## [2.3.1] - 2021-08-01 ### Security diff --git a/README.md b/README.md index 6e3e615..f249ed9 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,9 @@ Useful for SCP, SFTP, and `rsync` over SSH in deployment script. tested on: -* [all available virtual machines](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources) (Windows Server 2019/2016, macOS Catalina, and Ubuntu 20.04/18.04) +* [all available virtual machines](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/virtual-environments-for-github-hosted-runners#supported-runners-and-hardware-resources) (Windows Server 2022/2019, macOS Monterey/Big Sur/Catalina, and Ubuntu 22.04/20.04/18.04) * [Docker container (Ubuntu)](https://hub.docker.com/_/ubuntu) / requires `openssh-client` package; `apt install -y openssh-client` -* [Docker container (CentOS)](https://hub.docker.com/_/centos) / requires `openssh-clients` package; `yum install -y openssh-clients` +* [Docker container (CentOS)](https://quay.io/repository/centos/centos) / requires `openssh-clients` package; `yum install -y openssh-clients` * [Docker container (Alpine Linux)](https://hub.docker.com/_/alpine) / requires `openssh-client` package; `apk add openssh-client` ## Usage diff --git a/lib/index.js b/lib/index.js index 88bd249..e57705c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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(); })(); diff --git a/src/main.ts b/src/main.ts index 737ec57..1dbc3df 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,126 +3,125 @@ import path from "path"; import * as core from "@actions/core"; -interface FileInfo -{ - name: string; - contents: string; - options: fs.WriteFileOptions; +interface FileInfo { + name: string; + contents: string; + options: fs.WriteFileOptions; +} + +try { + main(); +} catch (err) { + if (err instanceof Error) { + core.setFailed(err); + } } /** * main function */ -function main(): void -{ - try - { - // parameters - const key = core.getInput("key", { - required: true, - }); - const name = core.getInput("name"); - const knownHosts = core.getInput("known_hosts", { - required: true, - }); - const config = core.getInput("config"); - const ifKeyExists = core.getInput("if_key_exists"); +function main(): void { + // parameters + const key = core.getInput("key", { + required: true, + }); + const name = core.getInput("name"); + const knownHosts = core.getInput("known_hosts", { + required: true, + }); + const config = core.getInput("config"); + 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, - }); + // create ".ssh" directory + const sshDirName = createSshDirectory(); - // files to be created - const files: FileInfo[] = []; - if(shouldCreateKeyFile(path.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", - }, - }); - } + // files to be created + const files: FileInfo[] = []; + if (shouldCreateKeyFile(path.join(sshDirName, 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.join(dirName, file.name); - fs.writeFileSync(fileName, file.contents, file.options); - } + // create files + for (const file of files) { + const fileName = path.join(sshDirName, file.name); + fs.writeFileSync(fileName, file.contents, file.options); + } - console.log(`SSH key has been stored to ${dirName} successfully.`); - } - catch(err) - { - core.setFailed(err.message); - } + 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(); - const home = process.env[homeEnv]; - if(home === undefined) - { - throw Error(`${homeEnv} is not defined`); - } +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"; - } + if (home === "/github/home") { + // Docker container + return "/root"; + } - return home; + return home; } /** * get HOME environment name * @returns HOME environment name */ -function getHomeEnv(): string -{ - if(process.platform === "win32") - { - // Windows - return "USERPROFILE"; - } +function getHomeEnv(): string { + if (process.platform === "win32") { + // Windows + return "USERPROFILE"; + } - // macOS / Linux - return "HOME"; + // macOS / Linux + return "HOME"; } /** @@ -132,25 +131,21 @@ function getHomeEnv(): string * @param append true to append * @returns new value */ -function insertLf(value: string, prepend: boolean, append: boolean): string -{ - let affectedValue = value; +function insertLf(value: string, prepend: boolean, append: boolean): string { + let affectedValue = value; - if(value.length === 0) - { - // do nothing if empty - return ""; - } - if(prepend && !affectedValue.startsWith("\n")) - { - affectedValue = `\n${affectedValue}`; - } - if(append && !affectedValue.endsWith("\n")) - { - affectedValue = `${affectedValue}\n`; - } + if (value.length === 0) { + // do nothing if empty + return ""; + } + if (prepend && !affectedValue.startsWith("\n")) { + affectedValue = `\n${affectedValue}`; + } + if (append && !affectedValue.endsWith("\n")) { + affectedValue = `${affectedValue}\n`; + } - return affectedValue; + return affectedValue; } /** @@ -159,29 +154,24 @@ function insertLf(value: string, prepend: boolean, append: boolean): string * @param ifKeyExists action if SSH key exists * @returns Yes/No */ -function shouldCreateKeyFile(keyFilePath: string, ifKeyExists: string): boolean -{ - if(!fs.existsSync(keyFilePath)) - { - // should create if file does not exist - return true; - } +function shouldCreateKeyFile(keyFilePath: string, ifKeyExists: string): boolean { + if (!fs.existsSync(keyFilePath)) { + // should create if file does not exist + return true; + } - switch(ifKeyExists) - { - case "replace": - // remove file and should create if replace - fs.unlinkSync(keyFilePath); - return true; + switch (ifKeyExists) { + case "replace": + // remove file and should create if replace + fs.unlinkSync(keyFilePath); + return true; - case "ignore": - // should NOT create if ignore - return false; + case "ignore": + // should NOT create if ignore + return false; - default: - // error otherwise - throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`); - } + default: + // error otherwise + throw new Error(`SSH key is already installed. Set "if_key_exists" to "replace" or "ignore" in order to avoid this error.`); + } } - -main();