1
0
Fork 0
mirror of https://github.com/shimataro/ssh-key-action.git synced 2025-06-19 22:52:10 +10:00
ssh-key-action/lib/main.js
shimataro 0374d240c3
Feature/append config known hosts (#47)
* * append to "config" and "known_hosts" instead of overwriting

* * refactor options

* * add test

* * fix test

* * print created files twice

* * print the contents of known_hosts and config

* * fix revision for test

* * fix revision

* * add LF to known_hosts / config

* * append LF to config and known_hosts

* * fix test

* * reject overwriting private-key and public-key

* * update test (will cause error)

* * revert verify.yml

* * update README and CHANGELOG

* * fix example in README

* * update CHANGELOG
2019-12-30 08:24:22 +09:00

84 lines
No EOL
2.4 KiB
JavaScript

"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const core = __importStar(require("@actions/core"));
/**
* main function
*/
function main() {
try {
const name = core.getInput("name");
const files = [
{
name: name,
contents: core.getInput("private-key"),
options: {
mode: 0o400,
flag: "ax",
},
},
{
name: `${name}.pub`,
contents: core.getInput("public-key"),
options: {
mode: 0o444,
flag: "ax",
},
},
{
name: "known_hosts",
contents: core.getInput("known-hosts") + "\n",
options: {
mode: 0o644,
flag: "a",
},
},
{
name: "config",
contents: core.getInput("config") + "\n",
options: {
mode: 0o644,
flag: "a",
},
},
];
// create ".ssh" directory
const home = getHomeDirectory();
const dirName = path.resolve(home, ".ssh");
fs.mkdirSync(dirName, {
recursive: true,
mode: 0o700,
});
// create files
for (const file of files) {
const fileName = path.join(dirName, 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);
}
}
/**
* get home directory
* @returns home directory
*/
function getHomeDirectory() {
const homeEnv = process.platform == "win32" ? "USERPROFILE" : "HOME";
const home = process.env[homeEnv];
if (home === undefined) {
throw Error(`${homeEnv} is not defined`);
}
return home;
}
main();
//# sourceMappingURL=main.js.map