-
-
Notifications
You must be signed in to change notification settings - Fork 251
Add ability to set command flags to Gcloud emulators #926
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1dc91a0
feat(gloud) add ability to set cmd flags
digital88 b9340fa
refactored approach to flags
digital88 94cefb3
fixed after review
digital88 d71d67b
Refactor flag management to base class
cristianrgreco 0535f75
Merge branch 'main' into pr/digital88/926
cristianrgreco 7b70104
Naming conventions
cristianrgreco 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
33 changes: 31 additions & 2 deletions
33
packages/modules/gcloud/src/datastore-emulator-container.ts
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
62 changes: 62 additions & 0 deletions
62
packages/modules/gcloud/src/emulator-flags-manager.test.ts
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,62 @@ | ||
| import { EmulatorFlagsManager } from "./emulator-flags-manager"; | ||
|
|
||
| describe("EmulatorFlagsManager", () => { | ||
| it("should add flag without --", async () => { | ||
| const flagsManager = new EmulatorFlagsManager().withFlag("database-mode", "firestore-native"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=firestore-native"); | ||
| }); | ||
|
|
||
| it("should add flag with --", async () => { | ||
| const flagsManager = new EmulatorFlagsManager().withFlag("--database-mode", "firestore-native"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=firestore-native"); | ||
| }); | ||
|
|
||
| it("should add many flags", async () => { | ||
| const flagsManager = new EmulatorFlagsManager() | ||
| .withFlag("database-mode", "firestore-native") | ||
| .withFlag("--host-port", "0.0.0.0:8080"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=firestore-native --host-port=0.0.0.0:8080"); | ||
| }); | ||
|
|
||
| it("should overwrite same flag if added more than once", async () => { | ||
| const flagsManager = new EmulatorFlagsManager() | ||
| .withFlag("database-mode", "firestore-native") | ||
| .withFlag("--database-mode", "datastore-mode"); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode=datastore-mode"); | ||
| }); | ||
|
|
||
| it("should add flag with no value", async () => { | ||
| const flagsManager = new EmulatorFlagsManager().withFlag("database-mode", "").withFlag("--host-port", ""); | ||
|
|
||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual("--database-mode= --host-port="); | ||
| }); | ||
|
|
||
| it("should throw if flag name not set", async () => { | ||
| expect(() => new EmulatorFlagsManager().withFlag("", "firestore-native")).toThrowError(); | ||
| }); | ||
|
|
||
| it("should clear all flags added", async () => { | ||
| const flagsManager = new EmulatorFlagsManager() | ||
| .withFlag("database-mode", "firestore-native") | ||
| .withFlag("host-port", "0.0.0.0:8080"); | ||
|
|
||
| flagsManager.clearFlags(); | ||
| const flags = flagsManager.expandFlags(); | ||
|
|
||
| expect(flags.trim()).toEqual(""); | ||
| }); | ||
| }); |
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,42 @@ | ||
| export class EmulatorFlagsManager { | ||
| private flags: { name: string; value: string }[] = []; | ||
|
|
||
| /** | ||
| * Adds flag as argument to emulator start command. | ||
| * Adding same flag name twice replaces existing flag value. | ||
| * @param name flag name. Must be set to non-empty string. May optionally contain -- prefix. | ||
| * @param value flag value. May be empty string. | ||
| * @returns this instance for chaining. | ||
| */ | ||
| public withFlag(name: string, value: string): this { | ||
digital88 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!name) throw new Error("Flag name must be set."); | ||
| // replace flag if it already exists | ||
| const idx = this.flags.findIndex((f) => f.name == this.trimFlagName(name)); | ||
| if (idx >= 0) this.flags = [...this.flags.slice(0, idx), ...this.flags.slice(idx + 1)]; | ||
| this.flags.push({ name, value }); | ||
| return this; | ||
| } | ||
|
|
||
| private trimFlagName(name: string): string { | ||
| return name?.startsWith("--") ? name.slice(2) : name; | ||
| } | ||
|
|
||
| private flagToString(f: { name: string; value: string }): string { | ||
| return `--${this.trimFlagName(f.name)}=${f.value}`; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @returns string with all flag names and values, concatenated in same order they were added. | ||
| */ | ||
| public expandFlags(): string { | ||
| return `${this.flags.reduce((p, c) => p + " " + this.flagToString(c), "")}`; | ||
| } | ||
|
|
||
| /** | ||
| * Clears all added flags. | ||
| */ | ||
| public clearFlags() { | ||
| this.flags = []; | ||
| } | ||
| } | ||
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
32 changes: 30 additions & 2 deletions
32
packages/modules/gcloud/src/firestore-emulator-container.ts
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
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.
Uh oh!
There was an error while loading. Please reload this page.