Skip to content

Commit 496ec52

Browse files
authored
Merge pull request #22 from lsphillips/add-back-cli-params
Added back support for CLI parameters.
2 parents 580e1d6 + 91ae078 commit 496ec52

File tree

2 files changed

+115
-48
lines changed
  • lib/configuration/variables/sources/instance-dependent
  • test/unit/lib/configuration/variables/sources/instance-dependent

2 files changed

+115
-48
lines changed

lib/configuration/variables/sources/instance-dependent/param.js

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ const resolveParams = memoizee(async (stage, serverlessInstance) => {
1212

1313
const resultParams = Object.create(null);
1414

15+
if (serverlessInstance.processedInput.options.param) {
16+
const regex = /(?<key>[^=]+)=(?<value>.+)/;
17+
for (const item of serverlessInstance.processedInput.options.param) {
18+
const res = item.match(regex);
19+
if (!res) {
20+
throw new ServerlessError(
21+
`Encountered invalid "--param" CLI option value: "${item}". Supported format: "--param='<key>=<val>'"`,
22+
'INVALID_CLI_PARAM_FORMAT'
23+
);
24+
}
25+
resultParams[res.groups.key] = { value: res.groups.value.trimEnd(), type: 'cli' };
26+
}
27+
}
28+
1529
for (const [name, value] of Object.entries(configParams.get(stage) || {})) {
1630
if (value == null) continue;
1731
if (resultParams[name] != null) continue;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict';
22

33
const { expect } = require('chai');
4-
const _ = require('lodash');
54

65
const resolveMeta = require('../../../../../../../lib/configuration/variables/resolve-meta');
76
const resolve = require('../../../../../../../lib/configuration/variables/resolve');
@@ -10,105 +9,159 @@ const getParamSource = require('../../../../../../../lib/configuration/variables
109
const Serverless = require('../../../../../../../lib/serverless');
1110

1211
describe('test/unit/lib/configuration/variables/sources/instance-dependent/param.test.js', () => {
13-
let configuration;
14-
let variablesMeta;
15-
let serverlessInstance;
16-
17-
const initializeServerless = async ({ configExt, options, setupOptions = {} } = {}) => {
18-
configuration = {
19-
service: 'foo',
12+
const runServerless = async ({
13+
cliParameters = [],
14+
stageParameters = {},
15+
stage,
16+
resolveWithoutInstance = false,
17+
} = {}) => {
18+
const configuration = {
19+
service: 'param-test-service',
2020
provider: {
21+
stage,
2122
name: 'aws',
2223
deploymentBucket: '${param:bucket}',
2324
timeout: '${param:timeout}',
25+
region: '${param:region}',
2426
},
2527
custom: {
2628
missingAddress: '${param:}',
2729
unsupportedAddress: '${param:foo}',
2830
nonStringAddress: '${param:${self:custom.someObject}}',
2931
someObject: {},
3032
},
31-
params: {
32-
default: {
33-
bucket: 'global.bucket',
34-
timeout: 10,
35-
},
36-
dev: {
37-
bucket: 'my.bucket',
38-
},
39-
},
33+
params: stageParameters,
4034
};
41-
if (configExt) {
42-
configuration = _.merge(configuration, configExt);
43-
}
44-
variablesMeta = resolveMeta(configuration);
45-
serverlessInstance = new Serverless({
35+
36+
const variablesMeta = resolveMeta(configuration);
37+
38+
const serverlessInstance = new Serverless({
4639
configuration,
40+
options: {
41+
param: cliParameters,
42+
},
4743
serviceDir: process.cwd(),
4844
configurationFilename: 'serverless.yml',
4945
commands: ['package'],
50-
options: options || {},
5146
});
47+
5248
serverlessInstance.init();
49+
5350
await resolve({
5451
serviceDir: process.cwd(),
5552
configuration,
5653
variablesMeta,
5754
sources: {
5855
self: selfSource,
59-
param: getParamSource(setupOptions.withoutInstance ? null : serverlessInstance),
56+
param: getParamSource(resolveWithoutInstance ? null : serverlessInstance),
57+
},
58+
options: {
59+
param: cliParameters,
6060
},
61-
options: options || {},
6261
fulfilledSources: new Set(['self', 'param']),
6362
});
63+
64+
return {
65+
configuration,
66+
serverlessInstance,
67+
variablesMeta,
68+
};
6469
};
6570

66-
it('should resolve ${param:timeout}', async () => {
67-
await initializeServerless();
68-
if (variablesMeta.get('param\0timeout')) throw variablesMeta.get('param\0timeout').error;
69-
expect(configuration.provider.timeout).to.equal(10);
71+
it('should resolve parameters from CLI parameters', async () => {
72+
const { configuration } = await runServerless({
73+
cliParameters: ['region=eu-west-1'],
74+
});
75+
expect(configuration.provider.region).to.equal('eu-west-1');
7076
});
7177

72-
it('should resolve ${param:bucket} for different stages', async () => {
73-
// Dev by default
74-
await initializeServerless();
75-
expect(configuration.provider.deploymentBucket).to.equal('my.bucket');
78+
it('should resolve parameter from parameters for the configured stage', async () => {
79+
const { configuration } = await runServerless({
80+
stageParameters: {
81+
staging: {
82+
timeout: 10,
83+
},
84+
},
85+
stage: 'staging',
86+
});
87+
expect(configuration.provider.timeout).to.equal(10);
88+
});
7689

77-
// Forced prod
78-
await initializeServerless({
79-
configExt: {
80-
provider: {
81-
stage: 'prod',
90+
it('should resolve parameter from default parameters if the parameter is not set for the configured stage', async () => {
91+
const { configuration } = await runServerless({
92+
stageParameters: {
93+
staging: {},
94+
default: {
95+
bucket: 'global.bucket',
8296
},
8397
},
98+
stage: 'staging',
8499
});
85100
expect(configuration.provider.deploymentBucket).to.equal('global.bucket');
86101
});
87102

88-
it('should resolve ${param:bucket} when no serverless instance available', async () => {
89-
await initializeServerless({ setupOptions: { withoutInstance: true } });
90-
expect(variablesMeta.get('provider\0timeout')).to.have.property('variables');
91-
expect(variablesMeta.get('provider\0timeout')).to.not.have.property('error');
103+
it('should resolve parameter from `dev` parameter if the stage is not configured', async () => {
104+
const { configuration } = await runServerless({
105+
stageParameters: {
106+
dev: {
107+
timeout: 5,
108+
},
109+
staging: {
110+
timeout: 10,
111+
},
112+
},
113+
});
114+
expect(configuration.provider.timeout).to.equal(5);
92115
});
93116

94-
it('should report with an error missing address', async () => {
95-
await initializeServerless();
117+
it('should treat CLI parameters with a higher precedence than stage parameters', async () => {
118+
const { configuration } = await runServerless({
119+
cliParameters: ['region=eu-west-2'],
120+
stageParameters: {
121+
staging: {
122+
region: 'eu-west-1',
123+
},
124+
},
125+
stage: 'staging',
126+
});
127+
expect(configuration.provider.region).to.equal('eu-west-2');
128+
});
129+
130+
it('should report with an error when the CLI parameter is invalid', async () => {
131+
const { variablesMeta } = await runServerless({
132+
cliParameters: ['region'],
133+
});
134+
135+
expect(variablesMeta.get('provider\0region').error.code).to.equal('VARIABLE_RESOLUTION_ERROR');
136+
});
137+
138+
it('should report with an error when the address is missing', async () => {
139+
const { variablesMeta } = await runServerless();
96140
expect(variablesMeta.get('custom\0missingAddress').error.code).to.equal(
97141
'VARIABLE_RESOLUTION_ERROR'
98142
);
99143
});
100144

101-
it('should report with an error unsupported address', async () => {
102-
await initializeServerless();
145+
it('should report with an error when the address is not supported', async () => {
146+
const { variablesMeta } = await runServerless();
103147
expect(variablesMeta.get('custom\0unsupportedAddress').error.code).to.equal(
104148
'VARIABLE_RESOLUTION_ERROR'
105149
);
106150
});
107151

108-
it('should report with an error a non-string address', async () => {
109-
await initializeServerless();
152+
it('should report with an error when the address it not a string', async () => {
153+
const { variablesMeta } = await runServerless();
110154
expect(variablesMeta.get('custom\0nonStringAddress').error.code).to.equal(
111155
'VARIABLE_RESOLUTION_ERROR'
112156
);
113157
});
158+
159+
it('should still resolve variables when no Serverless instance is available', async () => {
160+
const { variablesMeta } = await runServerless({
161+
cliParameters: ['timeout=10'],
162+
resolveWithoutInstance: true,
163+
});
164+
expect(variablesMeta.get('provider\0timeout')).to.have.property('variables');
165+
expect(variablesMeta.get('provider\0timeout')).to.not.have.property('error');
166+
});
114167
});

0 commit comments

Comments
 (0)