Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

Commit

Permalink
feat(droplets:power): add power cycle flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Satya Rohith committed Mar 4, 2019
1 parent 435521f commit bfc75e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/commands/droplets/power.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ class DropletsPowerCommand extends BaseCommand {
async run() {
const {flags} = this.parse(DropletsPowerCommand);
const {api, spinner, styledJSON} = this;
const {json, id, on, off} = flags;
const {json, id, cycle, on, off} = flags;

const userChoice = on ? 'on' : off ? 'off' : null;
let userChoice;
if (cycle) {
userChoice = 'cycle';
} else if (on) {
userChoice = 'on';
} else if (off) {
userChoice = 'off';
}

try {
const action = {
Expand All @@ -19,6 +26,8 @@ class DropletsPowerCommand extends BaseCommand {
spinner.stop();
if (json) {
this.log(styledJSON(body));
} else if (cycle === true) {
this.log(`Your droplet will go through a power cycle.`);
} else {
this.log(`Your droplet will be powered ${userChoice} shortly.`);
}
Expand All @@ -29,13 +38,17 @@ class DropletsPowerCommand extends BaseCommand {
}
}

DropletsPowerCommand.description = `power on/off a droplet`;
DropletsPowerCommand.description = `power on/off/cycle a droplet`;

DropletsPowerCommand.flags = {
json: flags.boolean({char: 'j', description: 'output in json format'}),
id: flags.integer({char: 'i', description: 'droplet ID', required: true}),
on: flags.boolean({char: 'o', description: 'power on droplet'}),
off: flags.boolean({char: 'f', description: 'power off droplet'})
off: flags.boolean({char: 'f', description: 'power off droplet'}),
cycle: flags.boolean({
char: 'c',
description: 'power cycle (off and on) a droplet'
})
};

module.exports = DropletsPowerCommand;
15 changes: 15 additions & 0 deletions test/commands/droplets/power.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ describe('droplets:power', () => {
const expectedOutput = `Your droplet will be powered off shortly.\n`;
expect(ctx.stdout).to.equal(expectedOutput);
});

test
.nock('https://api.digitalocean.com/v2/', api =>
api.post('/droplets/123456/actions', {type: 'power_cycle'}).reply(201, {
action: {
status: 'in-progress'
}
})
)
.stdout()
.command(['droplets:power', '--id', '123456', '-c'])
.it('shows successful request message', ctx => {
const expectedOutput = `Your droplet will go through a power cycle.\n`;
expect(ctx.stdout).to.equal(expectedOutput);
});
});

0 comments on commit bfc75e0

Please sign in to comment.