Skip to content

Commit

Permalink
feat(cli): add server info display
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jun 2, 2017
1 parent dfd45c0 commit 93c8778
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@
"dependencies": {
"axios": "^0.16.1",
"child_process": "^1.0.2",
"cliff": "^0.1.10",
"commander": "^2.9.0",
"fs": "^0.0.1-security",
"lodash.startcase": "^4.4.0",
"moment": "^2.12.0",
"superagent": "^3.5.2",
"typescript-logging": "^0.3.1",
Expand Down
143 changes: 123 additions & 20 deletions src/CLI.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,138 @@
import * as startCase from 'lodash.startcase';

import {Client} from './Client';

import {OnmsAuthConfig} from './api/OnmsAuthConfig';
import {OnmsServer} from './model/OnmsServer';
import {ServerType} from './api/Constants';

import {AxiosHTTP} from './rest/AxiosHTTP';
import {SuperAgentHTTP} from './rest/SuperAgentHTTP';

import {factory} from './api/Log';
import {log, catRoot, setLogLevel} from './api/Log';
import {Category,
CategoryServiceFactory,
CategoryDefaultConfiguration,
LogLevel} from 'typescript-logging';

const catCLI = new Category('cli', catRoot);

// tslint:disable
const cliff = require('cliff');
const colors = require('colors');
const fs = require('fs');
const path = require('path');
const program = require('commander');
// tslint:enable

const homedir = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
const defaultConfigFile = path.join(homedir, '.opennms-cli.config.json');

// tslint:disable-next-line
let program = require('commander');
function readConfig() {
const configfile = program.config || defaultConfigFile;
let config;
if (fs.exists) {
config = JSON.parse(fs.readFileSync(configfile));
} else {
config = {
password: undefined,
url: undefined,
username: undefined,
};
}
return config;
}

const log = factory.getLogger('CLI');
/* tslint:disable:no-console */

const cli = (url) => {
const server = new OnmsServer('OpenNMS', url, new OnmsAuthConfig(program.username, program.password));
const http = new AxiosHTTP(server);
Client.checkServer(server, http).then((res) => {
log.info(res.data);
return res;
}).catch((err) => {
if (err.stack) {
log.error(err.stack);
// global options
program
.option('-d, --debug', 'Enable debug output', () => {
setLogLevel(LogLevel.Debug);
})
.option('-c, --config <file>', 'Specify a configuration file (default: ~/.opennms-cli.config');

// connect (validate server and save config)
program
.command('connect [url]')
.description('Connect to an OpenNMS Horizon or Meridian server')
.option('-u, --username <username>', 'The username to authenticate as (default: admin)')
.option('-p, --password <password>', 'The password to authenticate with (default: admin)')
.action((url, options) => {
const config = readConfig();
if (url) {
config.url = url;
}
return err;
if (options.username) {
config.username = options.username;
}
if (options.password) {
config.password = options.password;
}
const server = new OnmsServer('OpenNMS', config.url, new OnmsAuthConfig(config.username, config.password));
const http = new AxiosHTTP(server);
return Client.checkServer(server, http).then(() => {
console.log(colors.green('Connection succeeded.'));
if (!program.config) { // don't write the config if a config was passed in
log.debug('Saving configuration to ' + defaultConfigFile, catCLI);
fs.writeFileSync(defaultConfigFile, JSON.stringify(config, undefined, 2), { mode: 0o600 });
}
/*
if (res.data.type === ServerType.MERIDIAN) {
console.log(colors.blue('Connected to OpenNMS Meridian ' + res.data.version.displayVersion));
} else {
console.log(colors.green('Connected to OpenNMS Horizon ' + res.data.version.displayVersion));
}
*/
return true;
}).catch((err) => {
if (err.stack) {
log.error(err.stack, err, catCLI);
}
return err;
});
});
};

// test another option
program
.arguments('<url>')
.option('-u, --username <username>', 'The user to authenticate as')
.option('-p, --password <password>', 'The password to authenticate with')
.action(cli)
.parse(process.argv);
.command('capabilities')
.description('List the API capabilities of the OpenNMS server')
.action(() => {
const config = readConfig();
const server = new OnmsServer('OpenNMS', config.url, new OnmsAuthConfig(config.username, config.password));
const http = new AxiosHTTP(server);
return Client.getMetadata(server, http).then((res) => {
let c = colors.green;
if (res.data.type === ServerType.MERIDIAN) {
console.log(colors.blue('OpenNMS Meridian ' + res.data.version.displayVersion + ' Capabilities:'));
c = colors.blue;
} else {
console.log(colors.green('OpenNMS Horizon ' + res.data.version.displayVersion + ' Capabilities:'));
}
console.log('');

const caps = res.data.capabilities();
const rows = [];
for (const cap in caps) {
if (cap === 'type') {
continue;
}
rows.push([startCase(cap), caps[cap]]);
}
console.log(cliff.stringifyRows(rows));
console.log('');

return res;
}).catch((err) => {
if (err.stack) {
log.error(err.stack, err, catCLI);
}
return err;
});
});

program.parse(process.argv);

if (!process.argv.slice(2).length) {
program.outputHelp();
}
56 changes: 54 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ async-each@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.1.tgz#19d386a1d9edc6e7c1c85d388aedbcc56d33602d"

async@0.2.x:
version "0.2.10"
resolved "https://registry.yarnpkg.com/async/-/async-0.2.10.tgz#b6bbe0b0674b9d719708ca38de8c237cb526c3d1"

async@^1.4.0, async@^1.5.0:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
Expand Down Expand Up @@ -1118,6 +1122,14 @@ cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3:
dependencies:
inherits "^2.0.1"

cliff@^0.1.10:
version "0.1.10"
resolved "https://registry.yarnpkg.com/cliff/-/cliff-0.1.10.tgz#53be33ea9f59bec85609ee300ac4207603e52013"
dependencies:
colors "~1.0.3"
eyes "~0.1.8"
winston "0.8.x"

cliui@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-2.1.0.tgz#4b475760ff80264c762c3a1719032e91c7fea0d1"
Expand Down Expand Up @@ -1160,7 +1172,15 @@ color-name@^1.1.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.2.tgz#5c8ab72b64bd2215d617ae9559ebb148475cf98d"

colors@^1.0.3, colors@^1.1.2:
colors@0.6.x:
version "0.6.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-0.6.2.tgz#2423fe6678ac0c5dae8852e5d0e5be08c997abcc"

colors@^1.0.3, colors@~1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.0.3.tgz#0433f44d809680fdeb60ed260f1b0c262e82a40b"

colors@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.1.2.tgz#168a4701756b6a7f51a12ce0c97bfa28c084ed63"

Expand Down Expand Up @@ -1392,6 +1412,10 @@ currently-unhandled@^0.4.1:
dependencies:
array-find-index "^1.0.1"

cycle@1.0.x:
version "1.0.3"
resolved "https://registry.yarnpkg.com/cycle/-/cycle-1.0.3.tgz#21e80b2be8580f98b468f379430662b046c34ad2"

dargs@^4.0.1:
version "4.1.0"
resolved "https://registry.yarnpkg.com/dargs/-/dargs-4.1.0.tgz#03a9dbb4b5c2f139bf14ae53f0b8a2a6a86f4e17"
Expand Down Expand Up @@ -1640,6 +1664,10 @@ extsprintf@1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.0.2.tgz#e1080e0658e300b06294990cc70e1502235fd550"

eyes@0.1.x, eyes@~0.1.8:
version "0.1.8"
resolved "https://registry.yarnpkg.com/eyes/-/eyes-0.1.8.tgz#62cf120234c683785d902348a800ef3e0cc20bc0"

fancy-log@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/fancy-log/-/fancy-log-1.3.0.tgz#45be17d02bb9917d60ccffd4995c999e6c8c9948"
Expand Down Expand Up @@ -2285,7 +2313,7 @@ isobject@^2.0.0:
dependencies:
isarray "1.0.0"

isstream@~0.1.2:
isstream@0.1.x, isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"

Expand Down Expand Up @@ -2809,6 +2837,10 @@ lodash.restparam@^3.0.0:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"

lodash.startcase@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.startcase/-/lodash.startcase-4.4.0.tgz#9436e34ed26093ed7ffae1936144350915d9add8"

lodash.template@^3.0.0:
version "3.6.2"
resolved "https://registry.yarnpkg.com/lodash.template/-/lodash.template-3.6.2.tgz#f8cdecc6169a255be9098ae8b0c53d378931d14f"
Expand Down Expand Up @@ -3361,6 +3393,10 @@ pkg-dir@^2.0.0:
dependencies:
find-up "^2.1.0"

pkginfo@0.3.x:
version "0.3.1"
resolved "https://registry.yarnpkg.com/pkginfo/-/pkginfo-0.3.1.tgz#5b29f6a81f70717142e09e765bbeab97b4f81e21"

pos@0.4.2:
version "0.4.2"
resolved "https://registry.yarnpkg.com/pos/-/pos-0.4.2.tgz#20e9c77fbeedcc356823cea63c7585cace93be2a"
Expand Down Expand Up @@ -3819,6 +3855,10 @@ stack-generator@^1.0.7:
dependencies:
stackframe "^1.0.2"

stack-trace@0.0.x:
version "0.0.10"
resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.10.tgz#547c70b347e8d32b4e108ea1a2a159e5fdde19c0"

stackframe@^0.3.1, stackframe@~0.3:
version "0.3.1"
resolved "https://registry.yarnpkg.com/stackframe/-/stackframe-0.3.1.tgz#33aa84f1177a5548c8935533cbfeb3420975f5a4"
Expand Down Expand Up @@ -4475,6 +4515,18 @@ window-size@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"

winston@0.8.x:
version "0.8.3"
resolved "https://registry.yarnpkg.com/winston/-/winston-0.8.3.tgz#64b6abf4cd01adcaefd5009393b1d8e8bec19db0"
dependencies:
async "0.2.x"
colors "0.6.x"
cycle "1.0.x"
eyes "0.1.x"
isstream "0.1.x"
pkginfo "0.3.x"
stack-trace "0.0.x"

wordwrap@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
Expand Down

0 comments on commit 93c8778

Please sign in to comment.