-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(env): add command to list/get/set/import environment variables (#…
…1162) Adds env:list, env:get, env:set, env:unset and env:import for managing environment variables from the CLI.
- Loading branch information
Showing
15 changed files
with
810 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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,135 @@ | ||
--- | ||
title: Netlify CLI env command | ||
description: Control environment variables for the current site | ||
--- | ||
|
||
# `env` | ||
|
||
<!-- AUTO-GENERATED-CONTENT:START (GENERATE_COMMANDS_DOCS) --> | ||
(Beta) Control environment variables for the current site | ||
|
||
**Usage** | ||
|
||
```bash | ||
netlify env | ||
``` | ||
|
||
**Flags** | ||
|
||
- `debug` (*boolean*) - Print debugging information | ||
|
||
| Subcommand | description | | ||
|:--------------------------- |:-----| | ||
| [`env:get`](/docs/commands/env.md#envget) | Get resolved value of specified environment variable (includes netlify.toml) | | ||
| [`env:import`](/docs/commands/env.md#envimport) | Import and set environment variables from .env file | | ||
| [`env:list`](/docs/commands/env.md#envlist) | Lists resolved environment variables for site (includes netlify.toml) | | ||
| [`env:set`](/docs/commands/env.md#envset) | Set value of environment variable | | ||
| [`env:unset`](/docs/commands/env.md#envunset) | Unset an environment variable which removes it from the UI | | ||
|
||
|
||
**Examples** | ||
|
||
```bash | ||
netlify env:list | ||
netlify env:get VAR_NAME | ||
netlify env:set VAR_NAME value | ||
netlify env:unset VAR_NAME | ||
netlify env:import fileName | ||
``` | ||
|
||
--- | ||
## `env:get` | ||
|
||
Get resolved value of specified environment variable (includes netlify.toml) | ||
|
||
**Usage** | ||
|
||
```bash | ||
netlify env:get | ||
``` | ||
|
||
**Arguments** | ||
|
||
- name - Environment variable name | ||
|
||
**Flags** | ||
|
||
- `debug` (*boolean*) - Print debugging information | ||
|
||
--- | ||
## `env:import` | ||
|
||
Import and set environment variables from .env file | ||
|
||
**Usage** | ||
|
||
```bash | ||
netlify env:import | ||
``` | ||
|
||
**Arguments** | ||
|
||
- fileName - .env file to import | ||
|
||
**Flags** | ||
|
||
- `debug` (*boolean*) - Print debugging information | ||
- `replaceExisting` (*boolean*) - Replace all existing variables instead of merging them with the current ones | ||
|
||
--- | ||
## `env:list` | ||
|
||
Lists resolved environment variables for site (includes netlify.toml) | ||
|
||
**Usage** | ||
|
||
```bash | ||
netlify env:list | ||
``` | ||
|
||
**Flags** | ||
|
||
- `debug` (*boolean*) - Print debugging information | ||
|
||
--- | ||
## `env:set` | ||
|
||
Set value of environment variable | ||
|
||
**Usage** | ||
|
||
```bash | ||
netlify env:set | ||
``` | ||
|
||
**Arguments** | ||
|
||
- name - Environment variable name | ||
- value - Value to set to | ||
|
||
**Flags** | ||
|
||
- `debug` (*boolean*) - Print debugging information | ||
|
||
--- | ||
## `env:unset` | ||
|
||
Unset an environment variable which removes it from the UI | ||
|
||
**Usage** | ||
|
||
```bash | ||
netlify env:unset | ||
``` | ||
|
||
**Arguments** | ||
|
||
- name - Environment variable name | ||
|
||
**Flags** | ||
|
||
- `debug` (*boolean*) - Print debugging information | ||
|
||
--- | ||
|
||
<!-- AUTO-GENERATED-CONTENT:END --> |
This file contains 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 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 |
---|---|---|
|
@@ -40,6 +40,7 @@ const navOrder = [ | |
'build', | ||
'deploy', | ||
'dev', | ||
'env', | ||
'functions', | ||
'init', | ||
'link', | ||
|
This file contains 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,53 @@ | ||
const Command = require('../../utils/command') | ||
|
||
class EnvGetCommand extends Command { | ||
async run() { | ||
const { args, flags } = this.parse(EnvGetCommand) | ||
const { api, site, config } = this.netlify | ||
const siteId = site.id | ||
|
||
if (!siteId) { | ||
this.log('No site id found, please run inside a site folder or `netlify link`') | ||
return false | ||
} | ||
|
||
await this.config.runHook('analytics', { | ||
eventName: 'command', | ||
payload: { | ||
command: 'env:get', | ||
}, | ||
}) | ||
|
||
const siteData = await api.getSite({ siteId }) | ||
const { | ||
build: { environment = {} }, | ||
} = config | ||
|
||
const { name } = args | ||
const value = environment[name] | ||
|
||
// Return json response for piping commands | ||
if (flags.json) { | ||
this.logJson(!value ? {} : { [name]: value }) | ||
return false | ||
} | ||
|
||
if (!value) { | ||
this.log(`Environment variable ${name} not set for site ${siteData.name}`) | ||
return false | ||
} | ||
|
||
this.log(value) | ||
} | ||
} | ||
|
||
EnvGetCommand.description = `Get resolved value of specified environment variable (includes netlify.toml)` | ||
EnvGetCommand.args = [ | ||
{ | ||
name: 'name', | ||
required: true, | ||
description: 'Environment variable name', | ||
}, | ||
] | ||
|
||
module.exports = EnvGetCommand |
This file contains 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,95 @@ | ||
const { flags } = require('@oclif/command') | ||
const AsciiTable = require('ascii-table') | ||
const Command = require('../../utils/command') | ||
const dotenv = require('dotenv') | ||
const fs = require('fs') | ||
const isEmpty = require('lodash.isempty') | ||
|
||
class EnvImportCommand extends Command { | ||
async run() { | ||
const { args, flags } = this.parse(EnvImportCommand) | ||
const { api, site } = this.netlify | ||
const siteId = site.id | ||
|
||
if (!siteId) { | ||
this.log('No site id found, please run inside a site folder or `netlify link`') | ||
return false | ||
} | ||
|
||
await this.config.runHook('analytics', { | ||
eventName: 'command', | ||
payload: { | ||
command: 'env:import', | ||
}, | ||
}) | ||
|
||
const siteData = await api.getSite({ siteId }) | ||
|
||
// Get current environment variables set in the UI | ||
const { | ||
build_settings: { env = {} }, | ||
} = siteData | ||
|
||
// Import environment variables from specified .env file | ||
const { fileName } = args | ||
let importedEnv = {} | ||
try { | ||
const envFileContents = fs.readFileSync(fileName) | ||
importedEnv = dotenv.parse(envFileContents) | ||
} catch (e) { | ||
this.error(e) | ||
} | ||
|
||
if (isEmpty(importedEnv)) { | ||
this.log(`No environment variables found in file ${fileName} to import`) | ||
return false | ||
} | ||
|
||
// Apply environment variable updates | ||
const siteResult = await api.updateSite({ | ||
siteId, | ||
body: { | ||
build_settings: { | ||
// Only set imported variables if --replaceExisting or otherwise merge | ||
// imported ones with the current environment variables. | ||
env: flags.replaceExisting ? importedEnv : { ...env, ...importedEnv }, | ||
}, | ||
}, | ||
}) | ||
|
||
// Return new environment variables of site if using json flag | ||
if (flags.json) { | ||
this.logJson(siteResult.build_settings.env) | ||
return false | ||
} | ||
|
||
// List newly imported environment variables in a table | ||
this.log(`site: ${siteData.name}`) | ||
const table = new AsciiTable(`Imported environment variables`) | ||
|
||
table.setHeading('Key', 'Value') | ||
for (const [key, value] of Object.entries(importedEnv)) { | ||
table.addRow(key, value) | ||
} | ||
this.log(table.toString()) | ||
} | ||
} | ||
|
||
EnvImportCommand.description = `Import and set environment variables from .env file` | ||
EnvImportCommand.flags = { | ||
...EnvImportCommand.flags, | ||
replaceExisting: flags.boolean({ | ||
char: 'r', | ||
description: 'Replace all existing variables instead of merging them with the current ones', | ||
default: false, | ||
}), | ||
} | ||
EnvImportCommand.args = [ | ||
{ | ||
name: 'fileName', | ||
required: true, | ||
description: '.env file to import', | ||
}, | ||
] | ||
|
||
module.exports = EnvImportCommand |
This file contains 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,33 @@ | ||
const Command = require('../../utils/command') | ||
const showHelp = require('../../utils/show-help') | ||
const { isEmptyCommand } = require('../../utils/check-command-inputs') | ||
|
||
class EnvCommand extends Command { | ||
async run() { | ||
const { flags, args } = this.parse(EnvCommand) | ||
|
||
// Show help on empty sub command | ||
if (isEmptyCommand(flags, args)) { | ||
showHelp(this.id) | ||
this.exit() | ||
} | ||
|
||
await this.config.runHook('analytics', { | ||
eventName: 'command', | ||
payload: { | ||
command: 'env', | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
EnvCommand.description = `(Beta) Control environment variables for the current site` | ||
EnvCommand.examples = [ | ||
'netlify env:list', | ||
'netlify env:get VAR_NAME', | ||
'netlify env:set VAR_NAME value', | ||
'netlify env:unset VAR_NAME', | ||
'netlify env:import fileName', | ||
] | ||
|
||
module.exports = EnvCommand |
Oops, something went wrong.