Skip to content

Commit 38ab4ed

Browse files
committed
chore: [A] add --sandbox param and make only one param required
1 parent 3b71003 commit 38ab4ed

File tree

3 files changed

+95
-18
lines changed

3 files changed

+95
-18
lines changed

src/cli/config.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ module.exports = ({ commandProcessor, root }) => {
1313

1414
commandProcessor.createCommand(env, 'list', 'List all environment variables', {
1515
options: {
16+
'sandbox': {
17+
description: 'Target the sandbox',
18+
boolean: true
19+
},
1620
'org': {
1721
description: 'Specify the organization'
1822
},
@@ -29,10 +33,10 @@ module.exports = ({ commandProcessor, root }) => {
2933
},
3034
handler: (args) => {
3135
const EnvVarsCommand = require('../cmd/env');
32-
return new EnvVarsCommand(args).rollout(args);
36+
return new EnvVarsCommand(args).list(args);
3337
},
3438
examples: {
35-
'$0 $command': 'List all environment variables.',
39+
'$0 $command --sandbox': 'List all environment variables from sandbox',
3640
'$0 $command --org <org>': 'List all environment variables from an specific organization',
3741
'$0 $command --product <productId>': 'List all environment variables from an specific product',
3842
'$0 $command --device <deviceId>': 'List all environment variables from an specific device',
@@ -42,6 +46,10 @@ module.exports = ({ commandProcessor, root }) => {
4246
commandProcessor.createCommand(env, 'set', 'Set an environment variable', {
4347
params: '<key> <value>',
4448
options: {
49+
'sandbox': {
50+
description: 'Target the sandbox',
51+
boolean: true
52+
},
4553
'org': {
4654
description: 'Specify the organization'
4755
},
@@ -54,16 +62,23 @@ module.exports = ({ commandProcessor, root }) => {
5462
},
5563
handler: (args) => {
5664
const EnvVarsCommand = require('../cmd/env');
57-
return new EnvVarsCommand(args).renderEnvVars(args);
65+
return new EnvVarsCommand(args).setEnvVars(args);
5866
},
5967
examples: {
60-
'$0 $command <key> <value>': 'Set env var to user\'s sandbox',
68+
'$0 $command <key> <value> --sandbox': 'Set env var to user\'s sandbox',
69+
'$0 $command <key> <value> --org <org>': 'Set env var for an organization',
70+
'$0 $command <key> <value> --product <productId>': 'Set env var for a product',
71+
'$0 $command <key> <value> --device <deviceId>': 'Set env var for a device',
6172
}
6273
});
6374

6475
commandProcessor.createCommand(env, 'delete', 'Delete an environment variable', {
6576
params: '<key>',
6677
options: {
78+
'sandbox': {
79+
description: 'Target the sandbox',
80+
boolean: true
81+
},
6782
'org': {
6883
description: 'Specify the organization'
6984
},
@@ -76,10 +91,13 @@ module.exports = ({ commandProcessor, root }) => {
7691
},
7792
handler: (args) => {
7893
const EnvVarsCommand = require('../cmd/env');
79-
return new EnvVarsCommand(args).patchEnvVars(args);
94+
return new EnvVarsCommand(args).deleteEnv(args);
8095
},
8196
examples: {
82-
'$0 $command <key>': 'Unset env var from user\'s sandbox',
97+
'$0 $command <key> --sandbox': 'Delete env var from user\'s sandbox',
98+
'$0 $command <key> --org <org>': 'Delete env var from an organization',
99+
'$0 $command <key> --product <productId>': 'Delete env var from a product',
100+
'$0 $command <key> --device <deviceId>': 'Delete env var from a device',
83101
}
84102
});
85103

src/cmd/env.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@ module.exports = class EnvVarsCommand extends CLICommandBase {
1111
this.api = createAPI();
1212
}
1313

14-
async list({ org, product, device, json }){
14+
_validateScope({ sandbox, org, product, device }) {
15+
const scopes = [
16+
{ name: 'sandbox', value: sandbox },
17+
{ name: 'org', value: org },
18+
{ name: 'product', value: product },
19+
{ name: 'device', value: device }
20+
].filter(scope => scope.value);
21+
22+
if (scopes.length === 0) {
23+
throw new Error('You must specify one of: --sandbox, --org, --product, or --device');
24+
}
25+
26+
if (scopes.length > 1) {
27+
const scopeNames = scopes.map(s => `--${s.name}`).join(', ');
28+
throw new Error(`You can only specify one scope at a time. You provided: ${scopeNames}`);
29+
}
30+
}
31+
32+
async list({ org, product, device, sandbox, json }){
33+
this._validateScope({ sandbox, org, product, device });
1534
const envVars = await this.ui.showBusySpinnerUntilResolved('Retrieving environment variables...',
1635
this.api.listEnvVars({ org, productId: product, deviceId: device }));
1736
if (json) {
@@ -69,7 +88,8 @@ module.exports = class EnvVarsCommand extends CLICommandBase {
6988
this.ui.write('---------------------------------------------');
7089
};
7190

72-
async setEnvVars({ params: { key, value }, org, product, device }) {
91+
async setEnvVars({ params: { key, value }, org, product, device, sandbox }) {
92+
this._validateScope({ sandbox, org, product, device });
7393
const operation = this._buildEnvVarOperation({ key, value, operation: 'Set' });
7494
await this.ui.showBusySpinnerUntilResolved('Setting environment variable...',
7595
this.api.patchEnvVars({
@@ -81,7 +101,8 @@ module.exports = class EnvVarsCommand extends CLICommandBase {
81101
this.ui.write(`Key ${key} has been successfully set.`);
82102
}
83103

84-
async deleteEnv({ params: { key }, org, product, device }) {
104+
async deleteEnv({ params: { key }, org, product, device, sandbox }) {
105+
this._validateScope({ sandbox, org, product, device });
85106
const operation = this._buildEnvVarOperation({ key, operation: 'Unset' });
86107
await this.ui.showBusySpinnerUntilResolved('Deleting environment variable...',
87108
this.api.patchEnvVars({

src/cmd/env.test.js

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('config env Command', () => {
6363
nock('https://api.particle.io/v1')
6464
.intercept('/env-vars', 'GET')
6565
.reply(200, sandboxList);
66-
await envVarsCommands.list({});
66+
await envVarsCommands.list({ sandbox: true });
6767
expect(envVarsCommands.ui.showBusySpinnerUntilResolved).calledWith('Retrieving environment variables...');
6868
const writeCalls = envVarsCommands.ui.write.getCalls().map(c => c.args[0]);
6969
const blocks = parseBlocksFromCalls(writeCalls);
@@ -88,11 +88,11 @@ describe('config env Command', () => {
8888
]);
8989
});
9090

91-
it('list all env vars for a device that belongs to a product', async () => {
91+
it('list all env vars for a device', async () => {
9292
nock('https://api.particle.io/v1')
93-
.intercept('/products/product-id-123/env-vars/abc123', 'GET')
93+
.intercept('/env-vars/abc123', 'GET')
9494
.reply(200, sandboxDeviceProductList);
95-
await envVarsCommands.list({ product: 'product-id-123', device: 'abc123' });
95+
await envVarsCommands.list({ device: 'abc123' });
9696
expect(envVarsCommands.ui.showBusySpinnerUntilResolved).calledWith('Retrieving environment variables...');
9797
const writeCalls = envVarsCommands.ui.write.getCalls().map(c => c.args[0]);
9898
const blocks = parseBlocksFromCalls(writeCalls);
@@ -108,7 +108,7 @@ describe('config env Command', () => {
108108
nock('https://api.particle.io/v1')
109109
.intercept('/env-vars', 'GET')
110110
.reply(200, emptyList);
111-
await envVarsCommands.list({ });
111+
await envVarsCommands.list({ sandbox: true });
112112
expect(envVarsCommands.ui.showBusySpinnerUntilResolved).calledWith('Retrieving environment variables...');
113113
expect(envVarsCommands.ui.write).to.have.been.calledWith('No environment variables found.');
114114
});
@@ -117,7 +117,7 @@ describe('config env Command', () => {
117117
nock('https://api.particle.io/v1')
118118
.intercept('/env-vars', 'GET')
119119
.reply(200, emptyListWithKeys);
120-
await envVarsCommands.list({ });
120+
await envVarsCommands.list({ sandbox: true });
121121
expect(envVarsCommands.ui.showBusySpinnerUntilResolved).calledWith('Retrieving environment variables...');
122122
expect(envVarsCommands.ui.write).to.have.been.calledWith('No environment variables found.');
123123
});
@@ -128,7 +128,7 @@ describe('config env Command', () => {
128128
nock('https://api.particle.io/v1')
129129
.intercept('/env-vars', 'PATCH')
130130
.reply(200, sandboxList);
131-
await envVarsCommands.setEnvVars({ params });
131+
await envVarsCommands.setEnvVars({ params, sandbox: true });
132132
expect(envVarsCommands.ui.showBusySpinnerUntilResolved).calledWith('Setting environment variable...');
133133
expect(envVarsCommands.ui.write).to.have.been.calledWith(`Key ${params.key} has been successfully set.`);
134134
});
@@ -143,7 +143,7 @@ describe('config env Command', () => {
143143
.reply(400, apiError);
144144
let error;
145145
try {
146-
await envVarsCommands.setEnvVars({ params: { } });
146+
await envVarsCommands.setEnvVars({ params: {}, sandbox: true });
147147
} catch (_error) {
148148
error = _error;
149149
}
@@ -190,7 +190,7 @@ describe('config env Command', () => {
190190
receivedBody = requestBody;
191191
return [200, {}];
192192
});
193-
await envVarsCommands.deleteEnv({ params });
193+
await envVarsCommands.deleteEnv({ params, sandbox: true });
194194
expect(receivedBody).to.deep.equal({ ops: [{ key: 'FOO', op: 'Unset' }] });
195195
expect(envVarsCommands.ui.showBusySpinnerUntilResolved).calledWith('Deleting environment variable...');
196196
expect(envVarsCommands.ui.write).to.have.been.calledWith(`Key ${params.key} has been successfully deleted.`);
@@ -240,4 +240,42 @@ describe('config env Command', () => {
240240
expect(envVarsCommands.ui.write).to.have.been.calledWith(`Key ${params.key} has been successfully deleted.`);
241241
});
242242
});
243+
244+
describe('scope validation', () => {
245+
it('throws error when no scope is provided', async () => {
246+
let error;
247+
try {
248+
await envVarsCommands.list({});
249+
} catch (_error) {
250+
error = _error;
251+
}
252+
expect(error.message).to.equal('You must specify one of: --sandbox, --org, --product, or --device');
253+
});
254+
255+
it('throws error when multiple scopes are provided', async () => {
256+
let error;
257+
try {
258+
await envVarsCommands.list({ sandbox: true, org: 'my-org' });
259+
} catch (_error) {
260+
error = _error;
261+
}
262+
expect(error.message).to.equal('You can only specify one scope at a time. You provided: --sandbox, --org');
263+
});
264+
265+
it('throws error when all scopes are provided', async () => {
266+
let error;
267+
try {
268+
await envVarsCommands.setEnvVars({
269+
params: { key: 'FOO', value: 'bar' },
270+
sandbox: true,
271+
org: 'my-org',
272+
product: 'my-product',
273+
device: 'abc123'
274+
});
275+
} catch (_error) {
276+
error = _error;
277+
}
278+
expect(error.message).to.equal('You can only specify one scope at a time. You provided: --sandbox, --org, --product, --device');
279+
});
280+
});
243281
});

0 commit comments

Comments
 (0)