From a79b15c529b60ac0d037716cf6e475bcda8f822e Mon Sep 17 00:00:00 2001 From: Reo Uehara <47747828+uh-zz@users.noreply.github.com> Date: Thu, 12 May 2022 08:59:18 +0900 Subject: [PATCH] feat: Support using go build (#1334) (#1356) --- .../handler-runner/go-runner/GoRunner.js | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/lambda/handler-runner/go-runner/GoRunner.js b/src/lambda/handler-runner/go-runner/GoRunner.js index 4814c9eeb..1f457b61d 100644 --- a/src/lambda/handler-runner/go-runner/GoRunner.js +++ b/src/lambda/handler-runner/go-runner/GoRunner.js @@ -5,7 +5,7 @@ import execa, { sync } from 'execa' const { writeFile, readFile, mkdir, rmdir } = fsPromises const { parse, stringify } = JSON -const { cwd } = process +const { cwd, chdir } = process const PAYLOAD_IDENTIFIER = 'offline_payload' @@ -15,12 +15,14 @@ export default class GoRunner { #tmpPath = null #tmpFile = null #goEnv = null + #codeDir = null constructor(funOptions, env, v3Utils) { - const { handlerPath } = funOptions + const { handlerPath, codeDir } = funOptions this.#env = env this.#handlerPath = handlerPath + this.#codeDir = codeDir if (v3Utils) { this.log = v3Utils.log @@ -28,9 +30,6 @@ export default class GoRunner { this.writeText = v3Utils.writeText this.v3Utils = v3Utils } - - // Make sure we have the mock-lambda runner - sync('go', ['get', 'github.com/icarus-sullivan/mock-lambda@e065469']) } async cleanup() { @@ -119,7 +118,18 @@ export default class GoRunner { // Remove our root, since we want to invoke go relatively const cwdPath = `${this.#tmpFile}`.replace(`${cwd()}${sep}`, '') - const { stdout, stderr } = await execa(`go`, ['run', cwdPath], { + + try { + chdir(cwdPath.substring(0, cwdPath.indexOf('main.go'))) + + // Make sure we have the mock-lambda runner + sync('go', ['get', 'github.com/icarus-sullivan/mock-lambda@e065469']) + sync('go', ['build']) + } catch (e) { + // @ignore + } + + const { stdout, stderr } = await execa(`./tmp`, { stdio: 'pipe', env: { ...this.#env, @@ -141,13 +151,20 @@ export default class GoRunner { encoding: 'utf-8', }) - // Clean up after we created the temporary file await this.cleanup() if (stderr) { return stderr } + try { + // refresh go.mod + sync('go', ['mod', 'tidy']) + chdir(this.#codeDir) + } catch (e) { + // @ignore + } + return this._parsePayload(stdout) } }