Skip to content

Commit

Permalink
Merge pull request #5313 from robin-norwood/invoke-local-override-env
Browse files Browse the repository at this point in the history
Invoke local override env
  • Loading branch information
horike37 committed Sep 26, 2018
2 parents 373efc4 + 049b5b0 commit 46aa939
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/providers/aws/cli-reference/invoke-local.md
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions docs/providers/google/cli-reference/invoke-local.md
Expand Up @@ -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.
Expand All @@ -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
```
11 changes: 11 additions & 0 deletions docs/providers/openwhisk/cli-reference/invoke-local.md
Expand Up @@ -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

Expand Down Expand Up @@ -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.
15 changes: 14 additions & 1 deletion lib/plugins/invoke/invoke.js
Expand Up @@ -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: {
Expand Down Expand Up @@ -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',
},
},
},
},
Expand Down Expand Up @@ -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();
}
}
Expand Down
14 changes: 14 additions & 0 deletions lib/plugins/invoke/invoke.test.js
Expand Up @@ -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'));
});
});
});
});

0 comments on commit 46aa939

Please sign in to comment.