Skip to content

Commit

Permalink
feat(env): add command to list/get/set/import environment variables (#…
Browse files Browse the repository at this point in the history
…1162)

Adds env:list, env:get, env:set, env:unset and env:import for managing environment
variables from the CLI.
  • Loading branch information
Pyrax authored Sep 2, 2020
1 parent 21a0656 commit cb9cb1d
Show file tree
Hide file tree
Showing 15 changed files with 810 additions and 25 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ See the [CLI command line reference](https://cli.netlify.com/commands/) to get s
* [build](#build)
* [deploy](#deploy)
* [dev](#dev)
* [env](#env)
* [functions](#functions)
* [init](#init)
* [link](#link)
Expand Down Expand Up @@ -102,6 +103,19 @@ Local dev server
| [`dev:exec`](/docs/commands/dev.md#devexec) | Exec command |


### [env](/docs/commands/env.md)

(Beta) Control environment variables for the current site

| 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 |


### [functions](/docs/commands/functions.md)

Manage netlify functions
Expand Down
13 changes: 13 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ Local dev server
| [`dev:exec`](/docs/commands/dev.md#devexec) | Exec command |


### [env](/docs/commands/env.md)

(Beta) Control environment variables for the current site

| 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 |


### [functions](/docs/commands/functions.md)

Manage netlify functions
Expand Down
135 changes: 135 additions & 0 deletions docs/commands/env.md
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 -->
13 changes: 13 additions & 0 deletions docs/commands/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ Local dev server
| [`dev:exec`](/docs/commands/dev.md#devexec) | Exec command |


### [env](/docs/commands/env.md)

(Beta) Control environment variables for the current site

| 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 |


### [functions](/docs/commands/functions.md)

Manage netlify functions
Expand Down
1 change: 1 addition & 0 deletions site/src/_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const navOrder = [
'build',
'deploy',
'dev',
'env',
'functions',
'init',
'link',
Expand Down
53 changes: 53 additions & 0 deletions src/commands/env/get.js
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
95 changes: 95 additions & 0 deletions src/commands/env/import.js
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
33 changes: 33 additions & 0 deletions src/commands/env/index.js
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
Loading

0 comments on commit cb9cb1d

Please sign in to comment.