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 function like env:export to make .env files from site config #3262

Closed
N0K0 opened this issue Aug 29, 2021 · 14 comments · Fixed by #5322
Closed

Add function like env:export to make .env files from site config #3262

N0K0 opened this issue Aug 29, 2021 · 14 comments · Fixed by #5322
Labels
area: command: env good first issue type: feature code contributing to the implementation of a feature and/or user facing functionality

Comments

@N0K0
Copy link

N0K0 commented Aug 29, 2021

Is your feature request related to a problem? Please describe.

When moving from one dev machine to an other i want to be able to quickly setup my .env file with secrets.

Describe the solution you'd like
I'm already using the env:import command to move all the .env content into netlify, that same system could be reversed to setup the new dev environment

Describe alternatives you've considered
At this time i can use env:list and write the file manually, but having an env:export which automatically formats the output would be nice

Additional context

Add any other context or screenshots about the feature request here.

Can you submit a pull request?

No. I don't really trust my own skills, but will look into it based on the env:list when i get some time.

Pull requests are welcome! If you would like to help us add this feature, please check our
contributions guidelines.

@N0K0 N0K0 added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Aug 29, 2021
@verythorough
Copy link
Contributor

Hi @N0K0! I agree there are times when this can be useful. Do you ever use netlify dev to run your site with env vars automatically included?

@N0K0
Copy link
Author

N0K0 commented Sep 1, 2021

Hi! Personally i've been using an alternative to the netlify dev command, since from what i can see running nuxt makes the rest of the netlify dev command hang while waiting for it to finish! And the default command, nuxt build removes all the nice dev features like Hot Module Reload from Nuxt.

Been using the following instead:

  "scripts": {
    "build": "nuxt build",
    "generate": "nuxt generate",
    "nuxt": "nuxt",
    "serve_func": "netlify functions:serve",
    "full": "concurrently --kill-others \"npm run nuxt\" \"npm run serve_func\""
  },

The full command gives me HMR for both functions and Nuxt 💪

@tinfoil-knight
Copy link
Contributor

tinfoil-knight commented Nov 3, 2021

@erezrokah
We currently output environment variables as easy to read ASCII tables:

> netlify env:list

site: my-first-site
.--------------.
| Environment variables |
|--------------|
| Key  | Value |
|------|-------|
| TEST | mucho |
| KEY  | 1234  |
'--------------'

Obviously, putting these in an .env file and then cleaning them up is tedious. So I do see the need of something like env:export.

But, since the code for env:export will almost be exactly the same as env:list, it doesn't make sense create a separate command for it.

Instead, we could provide a flag like --plain which would output something like this to the command line:

> netlify env:list --plain

TEST=mucho
KEY=1234

Further on this:
We could either take a path argument to write to since people also use .env.(dev|local|staging|prod) OR we could just list these variables and leave it to the users to redirect the output to the appropriate file.

with path

> netlify env:list --plain .env

with redirection

> netlify env:list --plain > .env

I feel like we should go with the 2nd option (with redirection) since I wouldn't normally expect a list command to create a file.

What are your thoughts on this?

@N0K0
Copy link
Author

N0K0 commented Nov 3, 2021

Just as good if not an better alternative in my eyes!

If its easy to add the flag i might be able to do it 😅
Not familiar with the codebase really :)

@erezrokah
Copy link
Contributor

I feel like we should go with the 2nd option (with redirection) since I wouldn't normally expect a list command to create a file.

👍 to this

@erezrokah
Copy link
Contributor

@tinfoil-knight, looks like this is a good opportunity to get @N0K0 involved.

@N0K0 would you like to take this on with @tinfoil-knight guiding you?

@tinfoil-knight
Copy link
Contributor

tinfoil-knight commented Nov 3, 2021

@erezrokah Should I take this one up since @N0K0 is a bit unfamiliar with the codebase or try to guide around @N0K0?
Edit: My previous comment got deleted due to a mis-click.

@erezrokah
Copy link
Contributor

@erezrokah Should I take this one up since @N0K0 is a bit unfamiliar with the codebase or try to guide around @N0K0?
Edit: My previous comment got deleted due to a mis-click.

#3262 (comment) is a response to ⬆️

@tinfoil-knight
Copy link
Contributor

@N0K0 if you decide to take this up, go through CONTRIBUTING.md once.

The specific code for the env:list command is here: https://github.com/netlify/cli/blob/main/src/commands/env/list.js


Note: The below code is not related to the env command. Just using this since it's a good reference.

To figure out how flags are working: see src/commands/dev/index.js (suggesting this file since it has a variety of flags)

Specifying a flag:

trafficMesh: flagsLib.boolean({

Using that flag:

if (flags.trafficMesh) {

@N0K0
Copy link
Author

N0K0 commented Nov 3, 2021

Just noticed that the --json flag is not exposed in the relevant help output, not directly related, but a hint would be nice 👍

❯ netlify env --help
(Beta) Control environment variables for the current site

USAGE
  $ netlify env

OPTIONS
  --debug                                                      Print debugging information
  --httpProxy=httpProxy                                        Proxy server address to route requests through.
  --httpProxyCertificateFilename=httpProxyCertificateFilename  Certificate file to use when connecting using a proxy server

EXAMPLES
  netlify env:list
  netlify env:get VAR_NAME
  netlify env:set VAR_NAME value
  netlify env:unset VAR_NAME
  netlify env:import fileName

COMMANDS
  env:get     Get resolved value of specified environment variable (includes netlify.toml)
  env:import  Import and set environment variables from .env file
  env:list    Lists resolved environment variables for site (includes netlify.toml)
  env:set     Set value of environment variable
  env:unset   Unset an environment variable which removes it from the UI

❯ netlify env:list --help
Lists resolved environment variables for site (includes netlify.toml)

USAGE
  $ netlify env:list

OPTIONS
  --debug                                                      Print debugging information
  --httpProxy=httpProxy                                        Proxy server address to route requests through.
  --httpProxyCertificateFilename=httpProxyCertificateFilename  Certificate file to use when connecting using a proxy server

@tinfoil-knight
Copy link
Contributor

Just noticed that the --json flag is not exposed in the relevant help output, not directly related, but a hint would be nice

Yup. I saw that too while going through the codebase. We could document the --json flag in a separate PR. Nice catch nonetheless.

@N0K0
Copy link
Author

N0K0 commented Nov 3, 2021

Looking at #371 for hints :)
Hopefully I'll have some time to fix it soon. Need to read up on contrib and such too 👍

@tinfoil-knight
Copy link
Contributor

@verythorough Do you think this issue is still relevant? If yes, I'll add the feature otherwise we can just close this issue.

@verythorough
Copy link
Contributor

@tinfoil-knight yes, I think this would still be useful! Your proposal for the --plain flag (without the path argument) sounds great, and if you can document --json at the same time, that'd be awesome.

Side note: for folks who may be looking to use env:list and env:import to copy env vars from one site to another, we now have env:clone (docs), which handles that in one step. ✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: command: env good first issue type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants