Skip to content

Commit

Permalink
Adds show status command
Browse files Browse the repository at this point in the history
  • Loading branch information
mukkachaitanya committed Jan 23, 2019
1 parent 126035d commit 9b4155c
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/cli/input/show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const getInput = async (args) => {
switch (args.stat) {
case 'status':
return {
name: 'status',
};
default:
return {
name: 'invalid_command',
};
}
};

module.exports = {
getInput,
};
32 changes: 32 additions & 0 deletions lib/cli/output/show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const util = require('util');
const { Spinner } = require('cli-spinner');

const spinner = new Spinner('Fetching request results, please wait...');
const startSpinner = () => {
spinner.setSpinnerString(0);
spinner.start();
};

const stopSpinner = () => {
spinner.stop();
};

const sendOutput = (event) => {
const lineBreakLength = 80;
switch (event.name) {
case 'fetching_results':
startSpinner();
break;
case 'status':
stopSpinner();
console.log(`\n ${util.inspect(event.details.status,
{ compact: true, breakLength: lineBreakLength, colors: true })}`);
break;
default:
break;
}
};

module.exports = {
sendOutput,
};
2 changes: 2 additions & 0 deletions lib/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const init = require('./init');
const exit = require('./exit');
const prefs = require('./prefs');
const evaluate = require('./eval');
const show = require('./show');


const start = async () => {
Expand All @@ -21,6 +22,7 @@ const start = async () => {
exit.addTo(program);
prefs.addTo(program);
evaluate.addTo(program);
show.addTo(program);

await program.parse(process.argv);
};
Expand Down
34 changes: 34 additions & 0 deletions lib/controller/show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const showInput = require('../cli/input/show');
const showOutput = require('../cli/output/show');
const showModel = require('../model/show');

const showValidate = require('./validate/show');

const onShowResults = (showResult) => {
showOutput.sendOutput(showResult);
};

const onShow = async (args, options, logger) => {
logger.moduleLog('info', 'Exit', 'Show command invoked.');

const showOptions = await showInput.getInput(args, options);
const validatedOptions = showValidate.validate(showOptions);

logger.moduleLog('debug', 'Eval', 'Show request for');
logger.moduleLog('debug', 'Eval', validatedOptions);

showOutput.sendOutput({ name: 'fetching_results' });

showModel.show(validatedOptions, onShowResults);
};

const addTo = (program) => {
program
.command('show', 'Shows server status and lab scores')
.argument('<stat>', 'Argument for show command')
.action(onShow);
};

module.exports = {
addTo,
};
10 changes: 10 additions & 0 deletions lib/controller/validate/show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const validate = (showEvent) => {
switch (showEvent.name) {
default:
return showEvent;
}
};

module.exports = {
validate,
};
52 changes: 52 additions & 0 deletions lib/model/show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const requestPromise = require('request-promise');
const preferenceManager = require('../utils/preference-manager');
const { logger } = require('../utils/logger');

const fetchStatus = async (host, port) => {
const options = {
method: 'GET',
uri: `https://${host}:${port}/status`,
rejectUnauthorized: false,
};

logger.moduleLog('debug', 'Show Model', `GET request to ${options.uri}`);

const response = await requestPromise(options).json();
return {
name: 'status',
details: {
status: response,
},
};
};

const getStatus = async (callback) => {
let response;
const { host, port } = preferenceManager.getPreference({ name: 'cliPrefs' }).main_server;
try {
response = await fetchStatus(host, port);
} catch (e) {
response = {
name: 'httpFailure',
deatils: {
code: e.getStatus || 4,
},
};
}

callback(response);
};

const show = (options, callback) => {
switch (options.name) {
case 'status':
getStatus(callback);
break;
default:
break;
}
};

module.exports = {
show,
};

0 comments on commit 9b4155c

Please sign in to comment.