From 56d02bd1de673f7ffb41884bb3429a1c3dbe0cce Mon Sep 17 00:00:00 2001 From: Robin Norwood Date: Wed, 19 Sep 2018 16:53:10 -0500 Subject: [PATCH 1/4] Implement #5096 - invoke local can override env vars with --env --- lib/plugins/invoke/invoke.js | 14 +++++++++++++- lib/plugins/invoke/invoke.test.js | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/plugins/invoke/invoke.js b/lib/plugins/invoke/invoke.js index 18f715735..41c843b33 100644 --- a/lib/plugins/invoke/invoke.js +++ b/lib/plugins/invoke/invoke.js @@ -5,8 +5,9 @@ const _ = require('lodash'); const userStats = require('../../utils/userStats'); class Invoke { - constructor(serverless) { + constructor(serverless, options) { this.serverless = serverless; + this.options = options || {}; this.commands = { invoke: { @@ -81,6 +82,10 @@ class Invoke { usage: 'Path to JSON or YAML file holding context data', shortcut: 'x', }, + env: { + usage: 'Override environment variables. e.g. --env VAR1=val1 --env VAR2=val2', + shortcut: 'e', + }, }, }, }, @@ -114,6 +119,13 @@ class Invoke { _.merge(process.env, defaultEnvVars); + // Turn zero or more --env options into an array + // ...then split --env NAME=value and put into process.env. + Array.concat(this.options.env || []).forEach(itm => { + let split_itm = itm.split('='); + process.env[split_itm[0]] = split_itm[1] || ''; + }); + return BbPromise.resolve(); } } diff --git a/lib/plugins/invoke/invoke.test.js b/lib/plugins/invoke/invoke.test.js index da707b70a..c4830bee6 100644 --- a/lib/plugins/invoke/invoke.test.js +++ b/lib/plugins/invoke/invoke.test.js @@ -41,6 +41,20 @@ describe('Invoke', () => { return expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled .then(() => expect(process.env.IS_LOCAL).to.equal('true')); }); + + it('should accept a single env option', () => { + invoke.options = { env: 'NAME=value' }; + expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled + .then(() => expect(process.env.NAME).to.equal('value')); + }); + + it('should accept multiple env options', () => { + invoke.options = { env: ['NAME1=val1', 'NAME2=val2'] }; + + expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled + .then(() => expect(process.env.NAME1).to.equal('val1')) + .then(() => expect(process.env.NAME2).to.equal('val2')); + }); }); }); }); From 3325118079d85d2245f36892fa0a86f308855497 Mon Sep 17 00:00:00 2001 From: Robin Norwood Date: Wed, 19 Sep 2018 17:05:02 -0500 Subject: [PATCH 2/4] Fix linting errors --- lib/plugins/invoke/invoke.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/plugins/invoke/invoke.js b/lib/plugins/invoke/invoke.js index 41c843b33..f38e66fda 100644 --- a/lib/plugins/invoke/invoke.js +++ b/lib/plugins/invoke/invoke.js @@ -122,8 +122,8 @@ class Invoke { // Turn zero or more --env options into an array // ...then split --env NAME=value and put into process.env. Array.concat(this.options.env || []).forEach(itm => { - let split_itm = itm.split('='); - process.env[split_itm[0]] = split_itm[1] || ''; + const splitItm = itm.split('='); + process.env[splitItm[0]] = splitItm[1] || ''; }); return BbPromise.resolve(); From 9fc463d8e6d000dac13faa607a54ca7ab61789e2 Mon Sep 17 00:00:00 2001 From: Robin Norwood Date: Wed, 19 Sep 2018 23:26:32 -0500 Subject: [PATCH 3/4] Use lodash .concat instead of Array.concat --- lib/plugins/invoke/invoke.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/plugins/invoke/invoke.js b/lib/plugins/invoke/invoke.js index f38e66fda..4890bc896 100644 --- a/lib/plugins/invoke/invoke.js +++ b/lib/plugins/invoke/invoke.js @@ -121,10 +121,11 @@ class Invoke { // Turn zero or more --env options into an array // ...then split --env NAME=value and put into process.env. - Array.concat(this.options.env || []).forEach(itm => { - const splitItm = itm.split('='); - process.env[splitItm[0]] = splitItm[1] || ''; - }); + _.concat(this.options.env || []) + .forEach(itm => { + const splitItm = _.split(itm, '='); + process.env[splitItm[0]] = splitItm[1] || ''; + }); return BbPromise.resolve(); } From 049b5b0fc818b5e9f440c07c32e18351f9d0a52e Mon Sep 17 00:00:00 2001 From: Robin Norwood Date: Wed, 19 Sep 2018 23:26:44 -0500 Subject: [PATCH 4/4] Add --env to docs --- docs/providers/aws/cli-reference/invoke-local.md | 11 +++++++++++ docs/providers/google/cli-reference/invoke-local.md | 11 +++++++++++ .../providers/openwhisk/cli-reference/invoke-local.md | 11 +++++++++++ 3 files changed, 33 insertions(+) diff --git a/docs/providers/aws/cli-reference/invoke-local.md b/docs/providers/aws/cli-reference/invoke-local.md index 20a618a98..0df47645b 100644 --- a/docs/providers/aws/cli-reference/invoke-local.md +++ b/docs/providers/aws/cli-reference/invoke-local.md @@ -28,6 +28,7 @@ serverless invoke local --function functionName - `--raw` Pass data as a raw string even if it is JSON. If not set, JSON data are parsed and passed as an object. - `--contextPath` or `-x`, The path to a json file holding input context to be passed to the invoked function. This path is relative to the root directory of the service. - `--context` or `-c`, String data to be passed as a context to your function. Same like with `--data`, context included in `--contextPath` will overwrite the context you passed with `--context` flag. +* `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `=`. Can be repeated for more than one environment variable. ## Environment @@ -94,6 +95,16 @@ serverless invoke local --function functionName --contextPath lib/context.json ``` This example will pass the json context in the `lib/context.json` file (relative to the root of the service) while invoking the specified/deployed function. +### Local function invocation, setting environment variables + +```bash +serverless invoke local -f functionName -e VAR1=value1 + +# Or more than one variable + +serverless invoke local -f functionName -e VAR1=value1 -e VAR2=value2 +``` + ### Limitations Currently, `invoke local` only supports the NodeJs, Python & Java runtimes. diff --git a/docs/providers/google/cli-reference/invoke-local.md b/docs/providers/google/cli-reference/invoke-local.md index 54ab11701..ecdfafc2b 100644 --- a/docs/providers/google/cli-reference/invoke-local.md +++ b/docs/providers/google/cli-reference/invoke-local.md @@ -28,6 +28,7 @@ serverless invoke -f functionName * `--raw` Pass data as a raw string even if it is JSON. If not set, JSON data are parsed and passed as an object. * `--contextPath` or `-x`, The path to a json file holding input context to be passed to the invoked function. This path is relative to the root directory of the service. * `--context` or `-c`, String data to be passed as a context to your function. Same like with `--data`, context included in `--contextPath` will overwrite the context you passed with `--context` flag. +* `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `=`. Can be repeated for more than one environment variable. > Keep in mind that if you pass both `--path` and `--data`, the data included in the `--path` file will overwrite the data you passed with the `--data` flag. @@ -54,3 +55,13 @@ serverless invoke local -f functionName -p path/to/file.json serverless invoke local -f functionName -p path/to/file.yaml ``` + +### Local function invocation, setting environment variables + +```bash +serverless invoke local -f functionName -e VAR1=value1 + +# Or more than one variable + +serverless invoke local -f functionName -e VAR1=value1 -e VAR2=value2 +``` diff --git a/docs/providers/openwhisk/cli-reference/invoke-local.md b/docs/providers/openwhisk/cli-reference/invoke-local.md index cad243fe1..4bb1ab52d 100644 --- a/docs/providers/openwhisk/cli-reference/invoke-local.md +++ b/docs/providers/openwhisk/cli-reference/invoke-local.md @@ -25,6 +25,7 @@ __*Please note that only the JavaScript and Python runtimes are supported with t - `--function` or `-f` The name of the function in your service that you want to invoke locally. **Required**. - `--path` or `-p` The path to a json file holding input data to be passed to the invoked function. This path is relative to the root directory of the service. The json file should have event and context properties to hold your mocked event and context data. - `--data` or `-d` String data to be passed as an event to your function. Keep in mind that if you pass both `--path` and `--data`, the data included in the `--path` file will overwrite the data you passed with the `--data` flag. +* `--env` or `-e` String representing an environment variable to set when invoking your function, in the form `=`. Can be repeated for more than one environment variable. ## Examples @@ -60,6 +61,16 @@ serverless invoke local --function functionName --path lib/data.json This example will pass the json data in the `lib/data.json` file (relative to the root of the service) while invoking the specified/deployed function. +### Local function invocation, setting environment variables + +```bash +serverless invoke local -f functionName -e VAR1=value1 + +# Or more than one variable + +serverless invoke local -f functionName -e VAR1=value1 -e VAR2=value2 +``` + ### Limitations Currently, `invoke local` only supports the NodeJs and Python runtimes.