From af948b80607eb0d13a18a3c2f03d6d238dd8f68e Mon Sep 17 00:00:00 2001 From: Sam Kirkland Date: Mon, 30 Mar 2020 23:20:38 -0500 Subject: [PATCH] Update main.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 - --- src/main.ts | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/main.ts b/src/main.ts index 8262958..ffb3699 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { + 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 {