Update main.ts

Code Cleanup changes:
- Pulled out this PR into a new function `configureHost`
- Placed args.knownHosts empty at the top of the function, this is known as "fail fast" and helps make the code more readable. Instead of wrapping our code in if/else blocks.
- Converted fs.writeFile to a awaitable function to make it more readable
- Pulled out sshFolder into a const
- Removed `console.log('Wrote ' + process.env['HOME'] + '/.ssh/known_hosts');` logging. Probably not needed because we log ` Configured known_hosts` just 2 lines later
- Small formatting changes

Bug fixes:
- Converted fs.writeFile to a awaitable function, previously the code continued to execute so this could open up a race condition. Also it's more readable :)
 - Race condition example: Previously we asked node to write the file `known_hosts` but we never verified the IO operation completed before modifying the files permission, or deploying the site. If this IO operation wasn't immediate the next function would throw or the deploy would error out.
- Any exception within the new method was swallowed because we had a catch without a throw. In this case let's end the program run instead of attempting to deploy
-
This commit is contained in:
Sam Kirkland 2020-03-30 23:20:38 -05:00 committed by GitHub
parent db3b78d8e7
commit af948b8060
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,30 +1,16 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import fs from 'fs';
import { promisify } from 'util';
import { IActionArguments } from './types';
const writeFileAsync = promisify(fs.writeFile);
async function run() {
const userArguments = getUserArguments();
if ( '' !== userArguments.knownHosts ) {
try {
await exec.exec(`mkdir -v -p ${process.env['HOME']}/.ssh`);
await exec.exec(`chmod 700 ${process.env['HOME']}/.ssh`);
fs.writeFile(
process.env['HOME'] + '/.ssh/known_hosts',
userArguments.knownHosts,
(err) => {
if (err) throw err;
console.log('Wrote ' + process.env['HOME'] + '/.ssh/known_hosts');
}
);
await exec.exec(`chmod 755 ${process.env['HOME']}/.ssh/known_hosts`);
console.log("✅ Configured known_hosts");
} catch( error ) {
console.error("⚠️ Error configuring known_hosts")
core.setFailed(error.message);;
}
}
try {
await configureHost(userArguments);
await syncFiles(userArguments);
console.log("✅ Deploy Complete");
@ -37,6 +23,26 @@ async function run() {
run();
async function configureHost(args: IActionArguments): Promise<void> {
if (args.knownHosts === "") {
return;
}
try {
const sshFolder = `${process.env['HOME']}/.ssh`;
await exec.exec(`mkdir -v -p ${sshFolder}`);
await exec.exec(`chmod 700 ${sshFolder}`);
writeFileAsync(`${sshFolder}/known_hosts`, args.knownHosts);
await exec.exec(`chmod 755 ${sshFolder}/known_hosts`);
console.log("✅ Configured known_hosts");
}
catch (error) {
console.error("⚠️ Error configuring known_hosts");
throw error;
}
}
function getUserArguments(): IActionArguments {
return {