Skip to content

Commit

Permalink
feat: Support using go build (#1334) (#1356)
Browse files Browse the repository at this point in the history
  • Loading branch information
uh-zz authored May 11, 2022
1 parent 6ffc199 commit a79b15c
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/lambda/handler-runner/go-runner/GoRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -15,22 +15,21 @@ 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
this.progress = v3Utils.progress
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() {
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
}

0 comments on commit a79b15c

Please sign in to comment.