Skip to content
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

Add command to export firebase config as environment variables #3690

Merged
merged 38 commits into from
Oct 1, 2021

Conversation

taeold
Copy link
Contributor

@taeold taeold commented Aug 18, 2021

To make it easy for users to transition from functions config to environment variables, functions:config:export commands steps through the process for reading configuration values stored in Runtime Config API for projects associated with the project directory and exporting them as local .env files in a valid dotenv file format.

$ cat .firebaserc
{
  "projects": {
    "default": "p0,
    "dev": "p0",
    "prod": "p1",
    "local": "p2"
  },
}
$ firebase functions:config:export
⚠  Project alias (local) is reserved for internal use. Saving exported config in .env.p2 instead.
i  Importing functions configs from projects [p0, p1]
⚠  The following configs keys could not be exported as environment variables:

⚠ p1 (prod):
	1foo.a => 1FOO_A (Key 1FOO_A must start with an uppercase ASCII letter or underscore, and then consist of uppercase ASCII letters, digits, and underscores.)
	x.google.foo.a => X_GOOGLE_FOO_A (Key X_GOOGLE_FOO_A starts with a reserved prefix (X_GOOGLE_ or FIREBASE_))
	k.service => K_SERVICE (Key K_SERVICE is reserved for internal use.)

? Enter a PREFIX to rename invalid environment variable keys: CONFIG_
? /Users/danielylee/google/cf3-env-vars/functions/.env.prod already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env.staging already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env.dev already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env.local already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env already exists. Overwrite file? Yes
✔  Wrote files:
	/Users/danielylee/google/cf3-env-vars/functions/.env.prod
	/Users/danielylee/google/cf3-env-vars/functions/.env.staging
	/Users/danielylee/google/cf3-env-vars/functions/.env.local
	/Users/danielylee/google/cf3-env-vars/functions/.env

$ cat functions/.env.prod
# Exported firebase functions:config:export command on 8/18/2021
SOMESERVICE_KEY="THE API KEY"                   # from someservice.key
SOMESERVICE_FOO_BAR="car"                       # from someservice.foo.bar
ABC_D="efg"                                     # from abc.d
SOME_OTHER_SERVICE_A="hey ohhh"                 # from some-other-service.a
CONFIG_K_SERVICE="hey ohhh"                     # from k.service
SLACK_USER="NewSlackUser\""                     # from slack.user
FOO_BAR_TEST="12"                               # from foo.bar.test
FOO_BAR_CAR="\"{\"test\":\"12\",\"next\":45}\"" # from foo.bar.car
CONFIG_1FOO_A="hey ohhh"                        # from 1foo.a
TEST_VAR="abc"                                  # from test.var
A_B="hello\nworld"                              # from a.b
APP_NAME="foobar"                               # from app.name
CONFIG_X_GOOGLE_FOO_A="hey ohhh"                # from x.google.foo.a
NEW_LINE="hello\nworld"                         # from new.line

@google-cla google-cla bot added the cla: yes Manual indication that this has passed CLA. label Aug 18, 2021
package.json Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
@taeold taeold marked this pull request as ready for review August 21, 2021 03:59
}

interface ConfigToEnvResult {
success: EnvMap[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you're using Required below. You could theoretically also have this be an Omit<EnvMap, 'err'>[]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the utility types! Do you think there's a reason to prefer one over the other?

src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/functions/env.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Show resolved Hide resolved
src/functions/env.ts Outdated Show resolved Hide resolved
src/functions/runtimeConfigExport.ts Show resolved Hide resolved
src/functions/runtimeConfigExport.ts Outdated Show resolved Hide resolved
src/functions/runtimeConfigExport.ts Outdated Show resolved Hide resolved
src/functions/runtimeConfigExport.ts Outdated Show resolved Hide resolved
src/commands/functions-config-export.ts Outdated Show resolved Hide resolved
@taeold taeold requested review from mbleigh and removed request for mbleigh September 30, 2021 21:47
@taeold taeold merged commit f0d4962 into master Oct 1, 2021
@taeold taeold deleted the dl-cf3-dotenv-migrate branch October 1, 2021 03:53
devpeerapong pushed a commit to devpeerapong/firebase-tools that referenced this pull request Dec 14, 2021
…ase#3690)

To make it easy for users to transition from functions config to environment variables, `functions:config:export` commands steps through the process for reading configuration values stored in Runtime Config API for projects associated with the project directory and exporting them as local .env files in a valid dotenv file format.

```
$ cat .firebaserc
{
  "projects": {
    "default": "p0,
    "dev": "p0",
    "prod": "p1",
    "local": "p2"
  },
}
$ firebase functions:config:export
⚠  Project alias (local) is reserved for internal use. Saving exported config in .env.p2 instead.
i  Importing functions configs from projects [p0, p1]
⚠  The following configs keys could not be exported as environment variables:

⚠ p1 (prod):
	1foo.a => 1FOO_A (Key 1FOO_A must start with an uppercase ASCII letter or underscore, and then consist of uppercase ASCII letters, digits, and underscores.)
	x.google.foo.a => X_GOOGLE_FOO_A (Key X_GOOGLE_FOO_A starts with a reserved prefix (X_GOOGLE_ or FIREBASE_))
	k.service => K_SERVICE (Key K_SERVICE is reserved for internal use.)

? Enter a PREFIX to rename invalid environment variable keys: CONFIG_
? /Users/danielylee/google/cf3-env-vars/functions/.env.prod already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env.staging already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env.dev already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env.local already exists. Overwrite file? Yes
? /Users/danielylee/google/cf3-env-vars/functions/.env already exists. Overwrite file? Yes
✔  Wrote files:
	/Users/danielylee/google/cf3-env-vars/functions/.env.prod
	/Users/danielylee/google/cf3-env-vars/functions/.env.staging
	/Users/danielylee/google/cf3-env-vars/functions/.env.local
	/Users/danielylee/google/cf3-env-vars/functions/.env

$ cat functions/.env.prod
# Exported firebase functions:config:export command on 8/18/2021
SOMESERVICE_KEY="THE API KEY"                   # from someservice.key
SOMESERVICE_FOO_BAR="car"                       # from someservice.foo.bar
ABC_D="efg"                                     # from abc.d
SOME_OTHER_SERVICE_A="hey ohhh"                 # from some-other-service.a
CONFIG_K_SERVICE="hey ohhh"                     # from k.service
SLACK_USER="NewSlackUser\""                     # from slack.user
FOO_BAR_TEST="12"                               # from foo.bar.test
FOO_BAR_CAR="\"{\"test\":\"12\",\"next\":45}\"" # from foo.bar.car
CONFIG_1FOO_A="hey ohhh"                        # from 1foo.a
TEST_VAR="abc"                                  # from test.var
A_B="hello\nworld"                              # from a.b
APP_NAME="foobar"                               # from app.name
CONFIG_X_GOOGLE_FOO_A="hey ohhh"                # from x.google.foo.a
NEW_LINE="hello\nworld"                         # from new.line
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Manual indication that this has passed CLA.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants