1
0
Fork 0
mirror of https://github.com/shimataro/ssh-key-action.git synced 2025-06-19 22:52:10 +10:00

refactor creating ".ssh" directoy

This commit is contained in:
shimataro 2022-10-30 17:50:42 +09:00
parent 37d0a89301
commit a94b58995c
No known key found for this signature in database
GPG key ID: BE92C05736911A9D
2 changed files with 37 additions and 20 deletions

View file

@ -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();

View file

@ -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();