-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Better Functions initialization #3507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f914da2
Basic create support
inlined b8bcd2c
Use vendoring to fetch SDK
inlined bd9fa14
Update sample code
inlined 7940dca
Simplify unarchive pipe
inlined 834a8cb
TSLint
inlined 6ea031c
PR feedback
inlined 6b84772
Use proper replace and get commands
inlined cf48d12
Fetch from newly public GH repo
inlined 9fe49b9
PR feedback
inlined 596640d
Format
inlined ae804e0
Merge remote-tracking branch 'origin/master' into inlined.create-func…
inlined File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { promisify } from "util"; | ||
| import * as path from "path"; | ||
| import * as fs from "fs"; | ||
| import * as spawn from "cross-spawn"; | ||
|
|
||
| import { FirebaseError } from "../../../../error"; | ||
| import { Options } from "../../../../options"; | ||
| import { logger } from "../../../../logger"; | ||
| import * as args from "../../args"; | ||
| import * as backend from "../../backend"; | ||
| import * as getProjectId from "../../../../getProjectId"; | ||
| import * as runtimes from ".."; | ||
|
|
||
| export const ADMIN_SDK = "firebase.google.com/go/v4"; | ||
| export const FUNCTIONS_SDK = "github.com/FirebaseExtended/firebase-functions-go"; | ||
|
|
||
| const VERSION_TO_RUNTIME: Record<string, runtimes.Runtime> = { | ||
| "1.13": "go113", | ||
| }; | ||
|
|
||
| export async function tryCreateDelegate( | ||
| context: args.Context, | ||
| options: Options | ||
| ): Promise<Delegate | undefined> { | ||
| const relativeSourceDir = options.config.get("functions.source") as string; | ||
| const sourceDir = options.config.path(relativeSourceDir); | ||
| const goModPath = path.join(sourceDir, "go.mod"); | ||
| const projectId = getProjectId(options); | ||
|
|
||
| let module: Module; | ||
| try { | ||
| const modBuffer = await promisify(fs.readFile)(goModPath); | ||
| module = parseModule(modBuffer.toString("utf8")); | ||
| } catch (err) { | ||
| logger.debug("Customer code is not Golang code (or they aren't using modules)"); | ||
| return; | ||
| } | ||
|
|
||
| let runtime = options.config.get("functions.runtime"); | ||
| if (!runtime) { | ||
| if (!module.version) { | ||
| throw new FirebaseError("Could not detect Golang version from go.mod"); | ||
| } | ||
| if (!VERSION_TO_RUNTIME[module.version]) { | ||
| throw new FirebaseError( | ||
| `go.mod specifies Golang version ${ | ||
| module.version | ||
| } which is unsupported by Google Cloud Functions. Valid values are ${Object.keys( | ||
| VERSION_TO_RUNTIME | ||
| ).join(", ")}` | ||
| ); | ||
| } | ||
| runtime = VERSION_TO_RUNTIME[module.version]; | ||
| } | ||
|
|
||
| return new Delegate(projectId, sourceDir, runtime, module); | ||
| } | ||
|
|
||
| // A module can be much more complicated than this, but this is all we need so far. | ||
| // For a full reference, see https://golang.org/doc/modules/gomod-ref | ||
| interface Module { | ||
| module: string; | ||
| version: string; | ||
| dependencies: Record<string, string>; | ||
| } | ||
|
|
||
| export function parseModule(mod: string): Module { | ||
| const module: Module = { | ||
| module: "", | ||
| version: "", | ||
| dependencies: {}, | ||
| }; | ||
| const lines = mod.split("\n"); | ||
| let inRequire = false; | ||
| for (const line of lines) { | ||
| if (inRequire) { | ||
| const endRequireMatch = /\)/.exec(line); | ||
| if (endRequireMatch) { | ||
| inRequire = false; | ||
| continue; | ||
| } | ||
|
|
||
| const requireMatch = /([^ ]+) (.*)/.exec(line); | ||
| if (requireMatch) { | ||
| module.dependencies[requireMatch[1]] = requireMatch[2]; | ||
| continue; | ||
| } | ||
|
|
||
| if (line.trim()) { | ||
| logger.debug("Don't know how to handle line", line, "inside a mod.go require block"); | ||
| } | ||
| continue; | ||
| } | ||
| const modMatch = /^module (.*)$/.exec(line); | ||
| if (modMatch) { | ||
| module.module = modMatch[1]; | ||
| continue; | ||
| } | ||
| const versionMatch = /^go (\d+\.\d+)$/.exec(line); | ||
| if (versionMatch) { | ||
| module.version = versionMatch[1]; | ||
| continue; | ||
| } | ||
|
|
||
| const requireMatch = /^require ([^ ]+) (.*)$/.exec(line); | ||
| if (requireMatch) { | ||
| module.dependencies[requireMatch[1]] = requireMatch[2]; | ||
| continue; | ||
| } | ||
|
|
||
| const requireBlockMatch = /^require +\(/.exec(line); | ||
| if (requireBlockMatch) { | ||
| inRequire = true; | ||
| continue; | ||
| } | ||
|
|
||
| if (line.trim()) { | ||
| logger.debug("Don't know how to handle line", line, "in mod.go"); | ||
| } | ||
| } | ||
|
|
||
| if (!module.module) { | ||
| throw new FirebaseError("Module has no name"); | ||
| } | ||
| if (!module.version) { | ||
| throw new FirebaseError(`Module ${module.module} has no go version`); | ||
| } | ||
|
|
||
| return module; | ||
| } | ||
|
|
||
| export class Delegate { | ||
| public readonly name = "golang"; | ||
|
|
||
| constructor( | ||
| private readonly projectId: string, | ||
| private readonly sourceDir: string, | ||
| public readonly runtime: runtimes.Runtime, | ||
| private readonly module: Module | ||
| ) {} | ||
| validate(): Promise<void> { | ||
| throw new FirebaseError("Cannot yet analyze Go source code"); | ||
| } | ||
|
|
||
| build(): Promise<void> { | ||
| const res = spawn.sync("go", ["build"], { | ||
| cwd: this.sourceDir, | ||
| stdio: "inherit", | ||
| }); | ||
| if (res.error) { | ||
| logger.debug("Got error running go build", res); | ||
| throw new FirebaseError("Failed to build functions source", { children: [res.error] }); | ||
| } | ||
|
|
||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| watch(): Promise<() => Promise<void>> { | ||
| return Promise.resolve(() => Promise.resolve()); | ||
| } | ||
|
|
||
| discoverSpec( | ||
| configValues: backend.RuntimeConfigValues, | ||
| envs: backend.EnvironmentVariables | ||
| ): Promise<backend.Backend> { | ||
| throw new FirebaseError("Cannot yet discover function specs"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { promisify } from "util"; | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
| import * as spawn from "cross-spawn"; | ||
|
|
||
| import { FirebaseError } from "../../../error"; | ||
| import { Config } from "../../../config"; | ||
| import { promptOnce } from "../../../prompt"; | ||
| import * as utils from "../../../utils"; | ||
| import * as go from "../../../deploy/functions/runtimes/golang"; | ||
| import { logger } from "../../../logger"; | ||
|
|
||
| const clc = require("cli-color"); | ||
|
|
||
| const RUNTIME_VERSION = "1.13"; | ||
|
|
||
| const TEMPLATE_ROOT = path.resolve(__dirname, "../../../../templates/init/functions/golang"); | ||
| const MAIN_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "functions.go"), "utf8"); | ||
| const GITIGNORE_TEMPLATE = fs.readFileSync(path.join(TEMPLATE_ROOT, "_gitignore"), "utf8"); | ||
|
|
||
| async function init(setup: unknown, config: Config) { | ||
| await writeModFile(config); | ||
|
|
||
| const modName = config.get("functions.go.module") as string; | ||
| const [pkg] = modName.split("/").slice(-1); | ||
| await config.askWriteProjectFile("functions/functions.go", MAIN_TEMPLATE.replace("PACKAGE", pkg)); | ||
| await config.askWriteProjectFile("functions/.gitignore", GITIGNORE_TEMPLATE); | ||
| } | ||
|
|
||
| // writeModFile is meant to look like askWriteProjectFile but it generates the contents | ||
| // dynamically using the go tool | ||
| async function writeModFile(config: Config) { | ||
| const modPath = config.path("functions/go.mod"); | ||
| if (await promisify(fs.exists)(modPath)) { | ||
| const shoudlWriteModFile = await promptOnce({ | ||
| type: "confirm", | ||
| message: "File " + clc.underline("functions/go.mod") + " already exists. Overwrite?", | ||
| default: false, | ||
| }); | ||
| if (!shoudlWriteModFile) { | ||
| return; | ||
| } | ||
|
|
||
| // Go will refuse to overwrite an existing mod file. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just copy go's behavior and error here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TypeScript and JavaScript do a prompt to overwrite and we do above here. That seems correct to me. |
||
| await promisify(fs.unlink)(modPath); | ||
| } | ||
|
|
||
| // Nit(inlined) can we look at functions code and see if there's a domain mapping? | ||
| const modName = await promptOnce({ | ||
| type: "input", | ||
| message: "What would you like to name your module?", | ||
| default: "acme.com/functions", | ||
| }); | ||
| config.set("functions.go.module", modName); | ||
|
|
||
| // Manually create a go mod file because (A) it's easier this way and (B) it seems to be the only | ||
| // way to set the min Go version to anything but what the user has installed. | ||
| config.writeProjectFile("functions/go.mod", `module ${modName} \n\ngo ${RUNTIME_VERSION}\n\n`); | ||
| utils.logSuccess("Wrote " + clc.bold("functions/go.mod")); | ||
|
|
||
| for (const dep of [go.FUNCTIONS_SDK, go.ADMIN_SDK]) { | ||
| const result = spawn.sync("go", ["get", dep], { | ||
| cwd: config.path("functions"), | ||
| stdio: "inherit", | ||
| }); | ||
| if (result.error) { | ||
| logger.debug("Full output from go get command:", JSON.stringify(result, null, 2)); | ||
| throw new FirebaseError("Error installing dependencies", { children: [result.error] }); | ||
| } | ||
| } | ||
| utils.logSuccess("Installed dependencies"); | ||
| } | ||
|
|
||
| module.exports = init; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this error on cases where module.module or module.version doesn't get defined?