-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin Reed
committed
Jun 2, 2017
1 parent
dfd45c0
commit 93c8778
Showing
3 changed files
with
179 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters