Skip to content

Commit

Permalink
Merge 3d6fa21 into 2d5b45e
Browse files Browse the repository at this point in the history
  • Loading branch information
busticated committed Apr 18, 2020
2 parents 2d5b45e + 3d6fa21 commit 185cc5f
Show file tree
Hide file tree
Showing 26 changed files with 921 additions and 714 deletions.
26 changes: 13 additions & 13 deletions src/cli/cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = ({ commandProcessor, root }) => {
return new CloudCommands().renameDevice(args);
},
examples: {
'$0 $command red green': 'Rename red device to green'
'$0 $command red green': 'Rename device `red` to `green`'
}
});

Expand All @@ -71,11 +71,11 @@ module.exports = ({ commandProcessor, root }) => {
return new CloudCommands().flashDevice(args);
},
examples: {
'$0 $command blue': 'Compile the source code in the current directory in the cloud and flash to device blue',
'$0 $command green tinker': 'Flash the default Tinker app to device green',
'$0 $command red blink.ino': 'Compile blink.ino in the cloud and flash to device red',
'$0 $command orange firmware.bin': 'Flash the pre-compiled binary to device orange',
'$0 $command blue --product 12345': 'Compile the source code in the current directory in the cloud and flash to device blue within product 12345'
'$0 $command blue': 'Compile the source code in the current directory in the cloud and flash to device `blue`',
'$0 $command green tinker': 'Flash the default `tinker` app to device `green`',
'$0 $command red blink.ino': 'Compile `blink.ino` in the cloud and flash to device `red`',
'$0 $command orange firmware.bin': 'Flash a pre-compiled `firmware.bin` binary to device `orange`',
'$0 $command 0123456789abcdef01234567 --product 12345': 'Compile the source code in the current directory in the cloud and flash to device `0123456789abcdef01234567` within product `12345`'
}
});

Expand All @@ -91,8 +91,8 @@ module.exports = ({ commandProcessor, root }) => {
return new CloudCommands().compileCode(args);
},
examples: {
'$0 $command photon': 'Compile the source code in the current directory in the cloud for a Photon',
'$0 $command electron project --saveTo electron.bin': 'Compile the source code in the project directory in the cloud for a Electron and save it to electron.bin',
'$0 $command photon': 'Compile the source code in the current directory in the cloud for a `photon`',
'$0 $command electron project --saveTo electron.bin': 'Compile the source code in the project directory in the cloud for an `electron` and save it to a file named `electron.bin`',
},
// TODO: get the platforms from config and document in epilogue
epilogue: 'Param deviceType can be: core, photon, p1, electron, argon, asom, boron, bsom, xenon, xsom, etc'
Expand All @@ -117,11 +117,6 @@ module.exports = ({ commandProcessor, root }) => {
});

commandProcessor.createCommand(cloud, 'login', 'Login to the cloud and store an access token locally', {
examples: {
'$0 $command': 'prompt for credentials and log in',
'$0 $command --username user@example.com --password test': 'log in with credentials provided on the command line',
'$0 $command --token <my-api-token>': 'log in with an access token provided on the command line'
},
options: {
u: {
description: 'your username',
Expand All @@ -142,6 +137,11 @@ module.exports = ({ commandProcessor, root }) => {
handler: (args) => {
const CloudCommands = require('../cmd/cloud');
return new CloudCommands().login(args);
},
examples: {
'$0 $command': 'prompt for credentials and log in',
'$0 $command --username user@example.com --password test': 'log in with credentials provided on the command line',
'$0 $command --token <my-api-token>': 'log in with an access token provided on the command line'
}
});

Expand Down
16 changes: 8 additions & 8 deletions src/cli/cloud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe('Cloud Command-Line Interface', () => {
'Usage: particle cloud name [options] <device> <name>',
'',
'Examples:',
' particle cloud name red green Rename red device to green',
' particle cloud name red green Rename device `red` to `green`',
''
].join(os.EOL));
});
Expand Down Expand Up @@ -241,11 +241,11 @@ describe('Cloud Command-Line Interface', () => {
' --product Target a device within the given Product ID or Slug [string]',
'',
'Examples:',
' particle cloud flash blue Compile the source code in the current directory in the cloud and flash to device blue',
' particle cloud flash green tinker Flash the default Tinker app to device green',
' particle cloud flash red blink.ino Compile blink.ino in the cloud and flash to device red',
' particle cloud flash orange firmware.bin Flash the pre-compiled binary to device orange',
' particle cloud flash blue --product 12345 Compile the source code in the current directory in the cloud and flash to device blue within product 12345',
' particle cloud flash blue Compile the source code in the current directory in the cloud and flash to device `blue`',
' particle cloud flash green tinker Flash the default `tinker` app to device `green`',
' particle cloud flash red blink.ino Compile `blink.ino` in the cloud and flash to device `red`',
' particle cloud flash orange firmware.bin Flash a pre-compiled `firmware.bin` binary to device `orange`',
' particle cloud flash 0123456789abcdef01234567 --product 12345 Compile the source code in the current directory in the cloud and flash to device `0123456789abcdef01234567` within product `12345`',
''
].join(os.EOL));
});
Expand Down Expand Up @@ -306,8 +306,8 @@ describe('Cloud Command-Line Interface', () => {
' --saveTo Filename for the compiled binary [string]',
'',
'Examples:',
' particle cloud compile photon Compile the source code in the current directory in the cloud for a Photon',
' particle cloud compile electron project --saveTo electron.bin Compile the source code in the project directory in the cloud for a Electron and save it to electron.bin',
' particle cloud compile photon Compile the source code in the current directory in the cloud for a `photon`',
' particle cloud compile electron project --saveTo electron.bin Compile the source code in the project directory in the cloud for an `electron` and save it to a file named `electron.bin`',
'',
'Param deviceType can be: core, photon, p1, electron, argon, asom, boron, bsom, xenon, xsom, etc',
''
Expand Down
6 changes: 3 additions & 3 deletions src/cli/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ module.exports = ({ commandProcessor, root }) => {

},
examples: {
'$0 $command coffee brew': 'Call the brew function on the coffee device',
'$0 $command board digitalWrite D7=HIGH': 'Call the digitalWrite function with argument D7=HIGH on the board device',
'$0 $command coffee brew --product 12345': 'Call the brew function on the coffee device within product 12345'
'$0 $command coffee brew': 'Call the `brew` function on the `coffee` device',
'$0 $command board digitalWrite D7=HIGH': 'Call the `digitalWrite` function with argument `D7=HIGH` on the `board` device',
'$0 $command 0123456789abcdef01234567 brew --product 12345': 'Call the `brew` function on the device with id `0123456789abcdef01234567` within product `12345`'
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/cli/function.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ describe('Function Command-Line Interface', () => {
' --product Target a device within the given Product ID or Slug [string]',
'',
'Examples:',
' particle function call coffee brew Call the brew function on the coffee device',
' particle function call board digitalWrite D7=HIGH Call the digitalWrite function with argument D7=HIGH on the board device',
' particle function call coffee brew --product 12345 Call the brew function on the coffee device within product 12345',
' particle function call coffee brew Call the `brew` function on the `coffee` device',
' particle function call board digitalWrite D7=HIGH Call the `digitalWrite` function with argument `D7=HIGH` on the `board` device',
' particle function call 0123456789abcdef01234567 brew --product 12345 Call the `brew` function on the device with id `0123456789abcdef01234567` within product `12345`',
''
].join(os.EOL));
});
Expand Down
10 changes: 5 additions & 5 deletions src/cli/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ module.exports = ({ commandProcessor, root }) => {
}
},
examples: {
'$0 $command 12345': 'Lists devices in Product 12345',
'$0 $command 12345 5a8ef38cb85f8720edce631a': 'Get details for device 5a8ef38cb85f8720edce631a within in product 12345',
'$0 $command 12345 --groups foo bar': 'Lists devices in Product which are assigned the `foo` or `bar` groups'
'$0 $command 12345': 'Lists devices in product `12345`',
'$0 $command 12345 0123456789abcdef01234567': 'Get details for device with id `0123456789abcdef01234567` within in product `12345`',
'$0 $command 12345 --groups foo bar': 'Lists devices in product which are assigned the `foo` or `bar` groups'
},
handler: (args) => {
const ProdCmd = require('../cmd/product');
Expand All @@ -49,8 +49,8 @@ module.exports = ({ commandProcessor, root }) => {
}
},
examples: {
'$0 $command 12345 5a8ef38cb85f8720edce631a': 'Add device id 5a8ef38cb85f8720edce631a into product 12345',
'$0 $command 12345 --file ./path/to/device_ids.txt': 'Adds a list of devices into product 12345',
'$0 $command 12345 0123456789abcdef01234567': 'Add device id `0123456789abcdef01234567` into product `12345`',
'$0 $command 12345 --file ./path/to/device_ids.txt': 'Adds a list of devices into product `12345`',
},
handler: (args) => {
const ProdCmd = require('../cmd/product');
Expand Down
10 changes: 5 additions & 5 deletions src/cli/product.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ describe('Product Command-Line Interface', () => {
' --json Output JSON formatted data (experimental) [boolean]',
'',
'Examples:',
' particle product device list 12345 Lists devices in Product 12345',
' particle product device list 12345 5a8ef38cb85f8720edce631a Get details for device 5a8ef38cb85f8720edce631a within in product 12345',
' particle product device list 12345 --groups foo bar Lists devices in Product which are assigned the `foo` or `bar` groups',
' particle product device list 12345 Lists devices in product `12345`',
' particle product device list 12345 0123456789abcdef01234567 Get details for device with id `0123456789abcdef01234567` within in product `12345`',
' particle product device list 12345 --groups foo bar Lists devices in product which are assigned the `foo` or `bar` groups',
''
].join(os.EOL));
});
Expand Down Expand Up @@ -128,8 +128,8 @@ describe('Product Command-Line Interface', () => {
' --file, -f Path to single column .txt file with list of IDs, S/Ns, IMEIs, or ICCIDs of the devices to add [string]',
'',
'Examples:',
' particle product device add 12345 5a8ef38cb85f8720edce631a Add device id 5a8ef38cb85f8720edce631a into product 12345',
' particle product device add 12345 --file ./path/to/device_ids.txt Adds a list of devices into product 12345',
' particle product device add 12345 0123456789abcdef01234567 Add device id `0123456789abcdef01234567` into product `12345`',
' particle product device add 12345 --file ./path/to/device_ids.txt Adds a list of devices into product `12345`',
''
].join(os.EOL));
});
Expand Down
9 changes: 5 additions & 4 deletions src/cli/subscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ module.exports = ({ commandProcessor, root }) => {
},
examples: {
'$0 $command': 'Subscribe to all event published by my devices',
'$0 $command update': 'Subscribe to events starting with update from my devices',
'$0 $command --device x': 'Subscribe to all events published by device x',
'$0 $command update': 'Subscribe to events starting with `update` from my devices',
'$0 $command --product 12345': 'Subscribe to all events published by devices within product `12345`',
'$0 $command --device blue': 'Subscribe to all events published by device `blue`',
'$0 $command --all': 'Subscribe to public events and all events published by my devices',
'$0 $command --until x': 'Subscribe to all events and exit when an event has data matching x',
'$0 $command --max x': 'Subscribe to all events and exit after seeing x events'
'$0 $command --until data': 'Subscribe to all events and exit when an event has data matching `data`',
'$0 $command --max 4': 'Subscribe to all events and exit after seeing `4` events'
}
});
};
Expand Down
13 changes: 7 additions & 6 deletions src/cli/subscribe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ describe('Subscribe Command-Line Interface', () => {
' --product Target a device within the given Product ID or Slug [string]',
'',
'Examples:',
' particle subscribe Subscribe to all event published by my devices',
' particle subscribe update Subscribe to events starting with update from my devices',
' particle subscribe --device x Subscribe to all events published by device x',
' particle subscribe --all Subscribe to public events and all events published by my devices',
' particle subscribe --until x Subscribe to all events and exit when an event has data matching x',
' particle subscribe --max x Subscribe to all events and exit after seeing x events',
' particle subscribe Subscribe to all event published by my devices',
' particle subscribe update Subscribe to events starting with `update` from my devices',
' particle subscribe --product 12345 Subscribe to all events published by devices within product `12345`',
' particle subscribe --device blue Subscribe to all events published by device `blue`',
' particle subscribe --all Subscribe to public events and all events published by my devices',
' particle subscribe --until data Subscribe to all events and exit when an event has data matching `data`',
' particle subscribe --max 4 Subscribe to all events and exit after seeing `4` events',
''
].join(os.EOL));
});
Expand Down
6 changes: 3 additions & 3 deletions src/cli/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ module.exports = ({ commandProcessor, root }) => {
return new VariableCommand().getValue(args);
},
examples: {
'$0 $command basement temperature': 'Read the temperature variable from the device basement',
'$0 $command basement temperature --product 12345': 'Read the temperature variable from the device basement within product 12345',
'$0 $command all temperature': 'Read the temperature variable from all my devices'
'$0 $command basement temperature': 'Read the `temperature` variable from the device `basement`',
'$0 $command 0123456789abcdef01234567 temperature --product 12345': 'Read the `temperature` variable from the device with id `0123456789abcdef01234567` within product `12345`',
'$0 $command all temperature': 'Read the `temperature` variable from all my devices'
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/cli/variable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ describe('Variable Command-Line Interface', () => {
' --product Target a device within the given Product ID or Slug [string]',
'',
'Examples:',
' particle variable get basement temperature Read the temperature variable from the device basement',
' particle variable get basement temperature --product 12345 Read the temperature variable from the device basement within product 12345',
' particle variable get all temperature Read the temperature variable from all my devices',
' particle variable get basement temperature Read the `temperature` variable from the device `basement`',
' particle variable get 0123456789abcdef01234567 temperature --product 12345 Read the `temperature` variable from the device with id `0123456789abcdef01234567` within product `12345`',
' particle variable get all temperature Read the `temperature` variable from all my devices',
''
].join(os.EOL));
});
Expand Down
35 changes: 35 additions & 0 deletions src/cmd/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { errors: { usageError } } = require('../app/command-processor');
const spinnerMixin = require('../lib/spinner-mixin');
const UI = require('../lib/ui');

const DEVICE_ID_PTN = /^[0-9a-f]{24}$/i;


module.exports = class CLICommandBase {
constructor({
stdin = process.stdin,
stdout = process.stdout,
stderr = process.stderr
} = {}) {
this.stdin = stdin;
this.stdout = stdout;
this.stderr = stderr;
this.ui = new UI({ stdin, stdout, stderr });
spinnerMixin(this);
}

isDeviceId(x){
return DEVICE_ID_PTN.test(x);
}

showUsageError(msg){
return Promise.reject(usageError(msg));
}

showProductDeviceNameUsageError(device){
return this.showUsageError(
`\`device\` must be an id when \`--product\` flag is set - received: ${device}`
);
}
};

68 changes: 68 additions & 0 deletions src/cmd/base.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { expect } = require('../../test/setup');
const CLICommandBase = require('./base');
const UI = require('../lib/ui');


describe('CLI Command Base Class', () => {
let cmd;

beforeEach(() => {
cmd = new CLICommandBase();
});

it('Initializes', () => {
expect(cmd).to.have.property('stdin', process.stdin);
expect(cmd).to.have.property('stdout', process.stdout);
expect(cmd).to.have.property('stderr', process.stderr);
expect(cmd).to.have.property('ui').that.is.an.instanceof(UI);
expect(cmd).to.respondTo('showUsageError');
expect(cmd).to.respondTo('isDeviceId');
});

it('Determines if input is a device id', () => {
expect(cmd.isDeviceId(null)).to.equal(false);
expect(cmd.isDeviceId(undefined)).to.equal(false);
expect(cmd.isDeviceId('')).to.equal(false);
expect(cmd.isDeviceId(666)).to.equal(false);
expect(cmd.isDeviceId('nope')).to.equal(false);
expect(cmd.isDeviceId(123456789123456789123456)).to.equal(false);
expect(cmd.isDeviceId('0123456789abcdef0123456')).to.equal(false);
expect(cmd.isDeviceId('0123456789abcdef01234567')).to.equal(true);
expect(cmd.isDeviceId('0123456789ABCDEF01234567')).to.equal(true);
});

it('Show a usage error', async () => {
const promise = cmd.showUsageError('test');

expect(promise).to.be.an.instanceof(Promise);

let error;

try {
await promise;
} catch (e){
error = e;
}

expect(error).to.be.an.instanceof(Error);
expect(error).to.have.property('message', 'test');
});

it('Show a product device name usage error', async () => {
const promise = cmd.showProductDeviceNameUsageError('my-device-name');

expect(promise).to.be.an.instanceof(Promise);

let error;

try {
await promise;
} catch (e){
error = e;
}

expect(error).to.be.an.instanceof(Error);
expect(error).to.have.property('message', '`device` must be an id when `--product` flag is set - received: my-device-name');
});
});

Loading

0 comments on commit 185cc5f

Please sign in to comment.