fix; JSON parse error on exit, if `if_key_exists`=`fail` and key exists (#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.
This commit is contained in:
parent
18e8292da0
commit
b49a036be4
|
@ -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
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
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[];
|
||||
}
|
||||
|
||||
|
|
69
src/main.ts
69
src/main.ts
|
@ -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,
|
||||
|
@ -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[] = [
|
||||
{
|
||||
|
@ -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),
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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(", ")}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue