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 (#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
8 changed files with 79 additions and 53 deletions
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
* JSON parse error on exit, if `if_key_exists`=`fail` and key exists
|
||||||
|
|
||||||
## [2.6.0] - 2023-10-11
|
## [2.6.0] - 2023-10-11
|
||||||
|
|
||||||
### Others
|
### Others
|
||||||
|
|
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
6
dist/post.js
vendored
6
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
|
* create backup suffix name
|
||||||
* @param dirName directory to back up
|
* @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 {
|
export function createBackupSuffix(dirName: string): string {
|
||||||
if (!fs.existsSync(dirName)) {
|
if (!fs.existsSync(dirName)) {
|
||||||
// do nothing if directory does not exist
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,6 +46,10 @@ export function saveCreatedFileNames(fileNames: string[]): void {
|
||||||
*/
|
*/
|
||||||
export function loadCreatedFileNames(): string[] {
|
export function loadCreatedFileNames(): string[] {
|
||||||
const json = core.getState(STATE_CREATED_FILES);
|
const json = core.getState(STATE_CREATED_FILES);
|
||||||
|
if (json === "") {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
return JSON.parse(json) as string[];
|
return JSON.parse(json) as string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
69
src/main.ts
69
src/main.ts
|
@ -34,6 +34,46 @@ try {
|
||||||
* main function
|
* main function
|
||||||
*/
|
*/
|
||||||
export function main(): void {
|
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
|
// parameters
|
||||||
const key = core.getInput("key", {
|
const key = core.getInput("key", {
|
||||||
required: true,
|
required: true,
|
||||||
|
@ -45,14 +85,6 @@ export function main(): void {
|
||||||
const config = core.getInput("config");
|
const config = core.getInput("config");
|
||||||
const ifKeyExists = core.getInput("if_key_exists");
|
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
|
// files to be created
|
||||||
const files: FileInfo[] = [
|
const files: FileInfo[] = [
|
||||||
{
|
{
|
||||||
|
@ -65,7 +97,7 @@ export function main(): void {
|
||||||
mustNotExist: false,
|
mustNotExist: false,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
|
if (shouldCreateKeyFile(path.join(dirName, name), ifKeyExists)) {
|
||||||
files.push({
|
files.push({
|
||||||
name: name,
|
name: name,
|
||||||
contents: insertLf(key, false, true),
|
contents: insertLf(key, false, true),
|
||||||
|
@ -88,24 +120,7 @@ export function main(): void {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// create files
|
return 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(", ")}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,11 +26,15 @@ export function post(): void {
|
||||||
} else {
|
} else {
|
||||||
// remove created files and restore from backup
|
// remove created files and restore from backup
|
||||||
const removedFileNames = removeCreatedFiles(sshDirName);
|
const removedFileNames = removeCreatedFiles(sshDirName);
|
||||||
|
if (removedFileNames.length > 0) {
|
||||||
console.log(`✅Following files have been removed successfully; ${removedFileNames.join(", ")}`);
|
console.log(`✅Following files have been removed successfully; ${removedFileNames.join(", ")}`);
|
||||||
|
}
|
||||||
|
|
||||||
const restoredFileNames = restoreFiles(sshDirName, backupSuffix);
|
const restoredFileNames = restoreFiles(sshDirName, backupSuffix);
|
||||||
|
if (restoredFileNames.length > 0) {
|
||||||
console.log(`✅Following files in suffix "${backupSuffix}" have been restored successfully; ${restoredFileNames.join(", ")}`);
|
console.log(`✅Following files in suffix "${backupSuffix}" have been restored successfully; ${restoredFileNames.join(", ")}`);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue