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/src/main.ts
2019-12-22 23:11:34 +09:00

85 lines
1.4 KiB
TypeScript

import * as fs from "fs";
import * as path from "path";
import * as core from "@actions/core";
interface FileInfo
{
name: string;
mode: number;
contents: string;
}
/**
* main function
*/
function main(): void
{
try
{
const name = core.getInput("name");
const files: FileInfo[] = [
{
name: name,
mode: 0o400,
contents: core.getInput("private-key"),
},
{
name: `${name}.pub`,
mode: 0o444,
contents: core.getInput("public-key"),
},
{
name: "known_hosts",
mode: 0o644,
contents: core.getInput("known-hosts"),
},
{
name: "config",
mode: 0o644,
contents: core.getInput("config"),
},
];
// 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, {
mode: file.mode,
});
}
console.log(`SSH key has been stored to ${dirName} successfully.`);
}
catch(err)
{
core.setFailed(err.message);
}
}
/**
* get home directory
* @returns home directory
*/
function getHomeDirectory(): string
{
const homeEnv = process.platform == "win32" ? "USERPROFILE" : "HOME";
const home = process.env[homeEnv];
if(home === undefined)
{
throw Error(`${homeEnv} is not defined`);
}
return home;
}
main();