mirror of
https://github.com/shimataro/ssh-key-action.git
synced 2025-06-19 22:52:10 +10:00
fix; JSON parse error on exit, if if_key_exists
=fail
and key exists
This commit is contained in:
parent
d05e1bdf7d
commit
f782e91986
6 changed files with 65 additions and 49 deletions
28
dist/main.js
vendored
28
dist/main.js
vendored
File diff suppressed because one or more lines are too long
6
dist/main.js.map
vendored
6
dist/main.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/post.js
vendored
2
dist/post.js
vendored
File diff suppressed because one or more lines are too long
4
dist/post.js.map
vendored
4
dist/post.js.map
vendored
File diff suppressed because one or more lines are too long
|
@ -11,11 +11,10 @@ const STATE_CREATED_FILES = "created-files";
|
|||
/**
|
||||
* create backup suffix name
|
||||
* @param dirName directory to back up
|
||||
* @returns backup suffix
|
||||
* @returns backup suffix; empty string if directory does not exist
|
||||
*/
|
||||
export function createBackupSuffix(dirName: string): string {
|
||||
if (!fs.existsSync(dirName)) {
|
||||
// do nothing if directory does not exist
|
||||
return "";
|
||||
}
|
||||
|
||||
|
@ -47,6 +46,10 @@ export function saveCreatedFileNames(fileNames: string[]): void {
|
|||
*/
|
||||
export function loadCreatedFileNames(): string[] {
|
||||
const json = core.getState(STATE_CREATED_FILES);
|
||||
if (json === "") {
|
||||
return [];
|
||||
}
|
||||
|
||||
return JSON.parse(json) as string[];
|
||||
}
|
||||
|
||||
|
|
67
src/main.ts
67
src/main.ts
|
@ -34,6 +34,44 @@ try {
|
|||
* main function
|
||||
*/
|
||||
export function main(): void {
|
||||
const sshDirName = common.getSshDirectory();
|
||||
|
||||
// files to be created
|
||||
const files = buildFilesToCreate(sshDirName);
|
||||
|
||||
// create ".ssh" directory
|
||||
const backupSuffix = common.createBackupSuffix(sshDirName);
|
||||
if (backupSuffix === "") {
|
||||
createDirectory(sshDirName);
|
||||
console.log(`✅SSH directory "${sshDirName}" has been created successfully.`);
|
||||
}
|
||||
|
||||
// back up & create files
|
||||
const createdFileNames: string[] = [];
|
||||
const backedUpFileNames: string[] = [];
|
||||
for (const file of files) {
|
||||
const pathName = path.join(sshDirName, file.name);
|
||||
if (backup(pathName, backupSuffix, file.mustNotExist)) {
|
||||
backedUpFileNames.push(file.name);
|
||||
}
|
||||
|
||||
fs.writeFileSync(pathName, file.contents, file.options);
|
||||
createdFileNames.push(file.name);
|
||||
}
|
||||
common.saveCreatedFileNames(createdFileNames);
|
||||
|
||||
console.log(`✅Following files have been created in "${sshDirName}" successfully; ${createdFileNames.join(", ")}`);
|
||||
if (backedUpFileNames.length > 0) {
|
||||
console.log(`✅Following files have been backed up in suffix "${backupSuffix}" successfully; ${backedUpFileNames.join(", ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* build files to create
|
||||
* @param dirName directory name in where files will be created
|
||||
* @returns files
|
||||
*/
|
||||
function buildFilesToCreate(dirName: string): FileInfo[] {
|
||||
// parameters
|
||||
const key = core.getInput("key", {
|
||||
required: true,
|
||||
|
@ -45,14 +83,6 @@ export function main(): void {
|
|||
const config = core.getInput("config");
|
||||
const ifKeyExists = core.getInput("if_key_exists");
|
||||
|
||||
// create ".ssh" directory
|
||||
const sshDirName = common.getSshDirectory();
|
||||
const backupSuffix = common.createBackupSuffix(sshDirName);
|
||||
if (backupSuffix === "") {
|
||||
createDirectory(sshDirName);
|
||||
console.log(`✅SSH directory "${sshDirName}" has been created successfully.`);
|
||||
}
|
||||
|
||||
// files to be created
|
||||
const files: FileInfo[] = [
|
||||
{
|
||||
|
@ -65,7 +95,7 @@ export function main(): void {
|
|||
mustNotExist: false,
|
||||
},
|
||||
];
|
||||
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
|
||||
if (shouldCreateKeyFile(path.join(dirName, name), ifKeyExists)) {
|
||||
files.push({
|
||||
name: name,
|
||||
contents: insertLf(key, false, true),
|
||||
|
@ -88,24 +118,7 @@ export function main(): void {
|
|||
});
|
||||
}
|
||||
|
||||
// create files
|
||||
const createdFileNames: string[] = [];
|
||||
const backedUpFileNames: string[] = [];
|
||||
for (const file of files) {
|
||||
const fileName = path.join(sshDirName, file.name);
|
||||
if (backup(fileName, backupSuffix, file.mustNotExist)) {
|
||||
backedUpFileNames.push(file.name);
|
||||
}
|
||||
|
||||
fs.writeFileSync(fileName, file.contents, file.options);
|
||||
createdFileNames.push(file.name);
|
||||
}
|
||||
common.saveCreatedFileNames(createdFileNames);
|
||||
|
||||
console.log(`✅Following files have been created in "${sshDirName}" successfully; ${createdFileNames.join(", ")}`);
|
||||
if (backedUpFileNames.length > 0) {
|
||||
console.log(`✅Following files have been backed up in suffix "${backupSuffix}" successfully; ${backedUpFileNames.join(", ")}`);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue