Merge pull request #5313 from robin-norwood/invoke-local-override-env

Invoke local override env
This commit is contained in:
Takahiro Horike 2018-09-27 05:53:20 +09:00 committed by GitHub
commit 46aa939373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 1 deletions

View File

@ -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 `<name>=<value>`. 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.

View File

@ -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 `<name>=<value>`. 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
```

View File

@ -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 `<name>=<value>`. 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.

View File

@ -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,14 @@ 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.
_.concat(this.options.env || [])
.forEach(itm => {
const splitItm = _.split(itm, '=');
process.env[splitItm[0]] = splitItm[1] || '';
});
return BbPromise.resolve();
}
}

View File

@ -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'));
});
});
});
});