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:
parent
37d0a89301
commit
a94b58995c
2 changed files with 37 additions and 20 deletions
28
lib/index.js
28
lib/index.js
|
@ -601,15 +601,10 @@ function main() {
|
||||||
const config = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("config");
|
const config = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("config");
|
||||||
const ifKeyExists = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("if_key_exists");
|
const ifKeyExists = _actions_core__WEBPACK_IMPORTED_MODULE_2__.getInput("if_key_exists");
|
||||||
// create ".ssh" directory
|
// create ".ssh" directory
|
||||||
const home = getHomeDirectory();
|
const sshDirName = createSshDirectory();
|
||||||
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
|
// files to be created
|
||||||
const files = [];
|
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({
|
files.push({
|
||||||
name: name,
|
name: name,
|
||||||
contents: insertLf(key, false, true),
|
contents: insertLf(key, false, true),
|
||||||
|
@ -641,14 +636,27 @@ function main() {
|
||||||
}
|
}
|
||||||
// create files
|
// create files
|
||||||
for (const file of 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);
|
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
|
* get home directory
|
||||||
* @returns home directory
|
* @returns home directory name
|
||||||
*/
|
*/
|
||||||
function getHomeDirectory() {
|
function getHomeDirectory() {
|
||||||
const homeEnv = getHomeEnv();
|
const homeEnv = getHomeEnv();
|
||||||
|
|
29
src/main.ts
29
src/main.ts
|
@ -25,16 +25,11 @@ function main(): void {
|
||||||
const ifKeyExists = core.getInput("if_key_exists");
|
const ifKeyExists = core.getInput("if_key_exists");
|
||||||
|
|
||||||
// create ".ssh" directory
|
// create ".ssh" directory
|
||||||
const home = getHomeDirectory();
|
const sshDirName = createSshDirectory();
|
||||||
const dirName = path.resolve(home, ".ssh");
|
|
||||||
fs.mkdirSync(dirName, {
|
|
||||||
recursive: true,
|
|
||||||
mode: 0o700,
|
|
||||||
});
|
|
||||||
|
|
||||||
// files to be created
|
// files to be created
|
||||||
const files: FileInfo[] = [];
|
const files: FileInfo[] = [];
|
||||||
if (shouldCreateKeyFile(path.join(dirName, name), ifKeyExists)) {
|
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
|
||||||
files.push({
|
files.push({
|
||||||
name: name,
|
name: name,
|
||||||
contents: insertLf(key, false, true),
|
contents: insertLf(key, false, true),
|
||||||
|
@ -67,16 +62,30 @@ function main(): void {
|
||||||
|
|
||||||
// create files
|
// create files
|
||||||
for (const file of 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);
|
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
|
* get home directory
|
||||||
* @returns home directory
|
* @returns home directory name
|
||||||
*/
|
*/
|
||||||
function getHomeDirectory(): string {
|
function getHomeDirectory(): string {
|
||||||
const homeEnv = getHomeEnv();
|
const homeEnv = getHomeEnv();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue