Skip to content

Commit

Permalink
fix; JSON parse error on exit, if if_key_exists=fail and key exis…
Browse files Browse the repository at this point in the history
…ts (#260)

* fix; JSON parse error on exit, if `if_key_exists`=`fail` and key exists

* update CHANGELOG

* fix cleanup error

* add built files

* output only if created/removed/restored file exist.
  • Loading branch information
shimataro authored Oct 12, 2023
1 parent 18e8292 commit b49a036
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

* JSON parse error on exit, if `if_key_exists`=`fail` and key exists

## [2.6.0] - 2023-10-11

### Others
Expand Down
28 changes: 14 additions & 14 deletions dist/main.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/main.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/post.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/post.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}

Expand Down Expand Up @@ -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[];
}

Expand Down
69 changes: 42 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,46 @@ try {
* main function
*/
export function main(): void {
const sshDirName = common.getSshDirectory();

// create ".ssh" directory
const backupSuffix = common.createBackupSuffix(sshDirName);
if (backupSuffix === "") {
createDirectory(sshDirName);
console.log(`✅SSH directory "${sshDirName}" has been created successfully.`);
}

// files to be created
const files = buildFilesToCreate(sshDirName);

// 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);

if (createdFileNames.length > 0) {
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,
Expand All @@ -45,14 +85,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[] = [
{
Expand All @@ -65,7 +97,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),
Expand All @@ -88,24 +120,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;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ export function post(): void {
} else {
// remove created files and restore from backup
const removedFileNames = removeCreatedFiles(sshDirName);
console.log(`✅Following files have been removed successfully; ${removedFileNames.join(", ")}`);
if (removedFileNames.length > 0) {
console.log(`✅Following files have been removed successfully; ${removedFileNames.join(", ")}`);
}

const restoredFileNames = restoreFiles(sshDirName, backupSuffix);
console.log(`✅Following files in suffix "${backupSuffix}" have been restored successfully; ${restoredFileNames.join(", ")}`);
if (restoredFileNames.length > 0) {
console.log(`✅Following files in suffix "${backupSuffix}" have been restored successfully; ${restoredFileNames.join(", ")}`);
}
}
}

Expand Down

0 comments on commit b49a036

Please sign in to comment.