|
| 1 | +import { promisify } from "util"; |
| 2 | +import * as path from "path"; |
| 3 | +import * as fs from "fs"; |
| 4 | +import * as spawn from "cross-spawn"; |
| 5 | + |
| 6 | +import { FirebaseError } from "../../../../error"; |
| 7 | +import { Options } from "../../../../options"; |
| 8 | +import { logger } from "../../../../logger"; |
| 9 | +import * as args from "../../args"; |
| 10 | +import * as backend from "../../backend"; |
| 11 | +import * as getProjectId from "../../../../getProjectId"; |
| 12 | +import * as runtimes from ".."; |
| 13 | + |
| 14 | +const VERSION_TO_RUNTIME: Record<string, runtimes.Runtime> = { |
| 15 | + "1.13": "go113", |
| 16 | +}; |
| 17 | + |
| 18 | +export async function tryCreateDelegate( |
| 19 | + context: args.Context, |
| 20 | + options: Options |
| 21 | +): Promise<Delegate | undefined> { |
| 22 | + const sourceDirName = options.config.get("functions.source") as string; |
| 23 | + const sourceDir = options.config.path(sourceDirName); |
| 24 | + const goModPath = path.join(sourceDir, "go.mod"); |
| 25 | + const projectId = getProjectId(options); |
| 26 | + |
| 27 | + let module: Module; |
| 28 | + try { |
| 29 | + const modBuffer = await promisify(fs.readFile)(goModPath); |
| 30 | + module = parseModule(modBuffer.toString("utf8")); |
| 31 | + } catch (err) { |
| 32 | + logger.debug("Customer code is not Golang code (or they aren't using modules)"); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + let runtime = options.config.get("functions.runtime"); |
| 37 | + if (!runtime) { |
| 38 | + if (!module.version) { |
| 39 | + throw new FirebaseError("Could not detect Golang version from go.mod"); |
| 40 | + } |
| 41 | + if (!VERSION_TO_RUNTIME[module.version]) { |
| 42 | + throw new FirebaseError( |
| 43 | + `go.mod specifies Golang version ${ |
| 44 | + module.version |
| 45 | + } which is unsupported by Google Cloud Functions. Valid values are ${Object.keys( |
| 46 | + VERSION_TO_RUNTIME |
| 47 | + ).join(", ")}` |
| 48 | + ); |
| 49 | + } |
| 50 | + runtime = VERSION_TO_RUNTIME[module.version]; |
| 51 | + } |
| 52 | + |
| 53 | + return new Delegate(projectId, sourceDirName, sourceDir, runtime, module); |
| 54 | +} |
| 55 | + |
| 56 | +// A module can be much more complicated than this, but this is all we need so far. |
| 57 | +// for a full reference, see https://golang.org/doc/modules/gomod-ref |
| 58 | +interface Module { |
| 59 | + module: string; |
| 60 | + version: string; |
| 61 | + dependencies: Record<string, string>; |
| 62 | +} |
| 63 | + |
| 64 | +export function parseModule(mod: string): Module { |
| 65 | + const module: Module = { |
| 66 | + module: "", |
| 67 | + version: "", |
| 68 | + dependencies: {}, |
| 69 | + }; |
| 70 | + const lines = mod.split("\n"); |
| 71 | + let inRequire = false; |
| 72 | + for (const line of lines) { |
| 73 | + if (inRequire) { |
| 74 | + const endRequireMatch = /\)/.exec(line); |
| 75 | + if (endRequireMatch) { |
| 76 | + inRequire = false; |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + const requireMatch = /([^ ]+) (.*)/.exec(line); |
| 81 | + if (requireMatch) { |
| 82 | + module.dependencies[requireMatch[1]] = requireMatch[2]; |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + if (line.trim()) { |
| 87 | + logger.debug("Don't know how to handle line", line, "inside a mod.go require block"); |
| 88 | + } |
| 89 | + continue; |
| 90 | + } |
| 91 | + const modMatch = /^module (.*)$/.exec(line); |
| 92 | + if (modMatch) { |
| 93 | + module.module = modMatch[1]; |
| 94 | + continue; |
| 95 | + } |
| 96 | + const versionMatch = /^go (\d+\.\d+)$/.exec(line); |
| 97 | + if (versionMatch) { |
| 98 | + module.version = versionMatch[1]; |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + const requireMatch = /^require ([^ ]+) (.*)$/.exec(line); |
| 103 | + if (requireMatch) { |
| 104 | + module.dependencies[requireMatch[1]] = requireMatch[2]; |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + const requireBlockMatch = /^require +\(/.exec(line); |
| 109 | + if (requireBlockMatch) { |
| 110 | + inRequire = true; |
| 111 | + continue; |
| 112 | + } |
| 113 | + |
| 114 | + if (line.trim()) { |
| 115 | + logger.debug("Don't know how to handle line", line, "in mod.go"); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return module; |
| 120 | +} |
| 121 | + |
| 122 | +export class Delegate { |
| 123 | + public readonly name = "golang"; |
| 124 | + |
| 125 | + constructor( |
| 126 | + private readonly projectId: string, |
| 127 | + private readonly sourceDirName: string, |
| 128 | + private readonly sourceDir: string, |
| 129 | + public readonly runtime: runtimes.Runtime, |
| 130 | + private readonly module: Module |
| 131 | + ) {} |
| 132 | + validate(): Promise<void> { |
| 133 | + // throw new FirebaseError("Cannot yet analyze Go source code"); |
| 134 | + return Promise.resolve(); |
| 135 | + } |
| 136 | + |
| 137 | + build(): Promise<void> { |
| 138 | + const res = spawn.sync("go", ["build"], { |
| 139 | + cwd: this.sourceDir, |
| 140 | + stdio: "inherit", |
| 141 | + }); |
| 142 | + if (res.error) { |
| 143 | + logger.debug("Got error running go build", res); |
| 144 | + throw new FirebaseError("Failed to build functions source", { children: [res.error] }); |
| 145 | + } |
| 146 | + |
| 147 | + return Promise.resolve(); |
| 148 | + } |
| 149 | + |
| 150 | + watch(): Promise<() => Promise<void>> { |
| 151 | + return Promise.resolve(() => Promise.resolve()); |
| 152 | + } |
| 153 | + |
| 154 | + discoverSpec( |
| 155 | + configValues: backend.RuntimeConfigValues, |
| 156 | + envs: backend.EnvironmentVariables |
| 157 | + ): Promise<backend.Backend> { |
| 158 | + const stubbed: backend.Backend = { |
| 159 | + requiredAPIs: {}, |
| 160 | + topics: [], |
| 161 | + schedules: [], |
| 162 | + cloudFunctions: [ |
| 163 | + { |
| 164 | + apiVersion: 1, |
| 165 | + id: "HelloWorld", |
| 166 | + region: "us-central1", |
| 167 | + project: this.projectId, |
| 168 | + entryPoint: "HelloWorld", |
| 169 | + runtime: this.runtime, |
| 170 | + trigger: { |
| 171 | + allowInsecure: false, |
| 172 | + }, |
| 173 | + }, |
| 174 | + ], |
| 175 | + environmentVariables: envs, |
| 176 | + }; |
| 177 | + return Promise.resolve(stubbed); |
| 178 | + } |
| 179 | +} |
0 commit comments