Skip to content

Commit 2322564

Browse files
Use Subclassing introduced in repository.js
1 parent 208e1f3 commit 2322564

File tree

5 files changed

+44
-90
lines changed

5 files changed

+44
-90
lines changed

cli/core.js

+7-43
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,28 @@
11
"use strict";
22

3-
//
4-
// Imports
5-
//
63
var fs = require("fs");
7-
var _ = require("lodash");
84

95
var log = require("../lib/util/log");
106
var constants = require("../lib/constants");
117
var SOASTA = require("../lib/model/mPulse.js");
8+
var CLI = require("soasta-repository").CLI;
9+
10+
Object.keys(CLI).forEach(function(functionName) {
11+
exports[functionName] = CLI[functionName];
12+
});
1213

13-
//
14-
// Exports
15-
//
1614
/**
1715
* Initializes the command-line interface
1816
*
1917
* @param {object} options Options
2018
*/
2119
exports.init = function(options) {
22-
if (options && options.parent && options.parent.verbose) {
23-
log.transports.console.level = "debug";
24-
}
25-
26-
if (fs.existsSync("auth.json")) {
27-
// read in auth info
28-
var authContents = fs.readFileSync("auth.json", "utf-8");
29-
var auth = JSON.parse(authContents);
30-
31-
// use auth contents as fallback
32-
options.parent = _.merge({}, auth, options.parent);
33-
}
34-
35-
// use defaults for repository
36-
if (!options.parent.repository) {
37-
options.parent.repository = constants.REPOSITORY_URL;
38-
}
20+
log.transports.console.json = options.parent.json;
21+
CLI.init.call(null, options);
3922

4023
if (!options.parent.mpulse) {
4124
options.parent.mpulse = constants.QUERY_URL;
4225
}
43-
44-
console.log(options.parent);
45-
46-
// ensure username and password have been specified
47-
if (!options.parent.username) {
48-
log.error("--username (-u) is required");
49-
process.exit(1);
50-
}
51-
52-
if (!options.parent.password) {
53-
log.error("--password (-p) is required");
54-
process.exit(1);
55-
}
56-
57-
if (!options.parent.apikey) {
58-
log.error("--apikey (-a) is required!");
59-
process.exit(1);
60-
}
6126
};
6227

6328
/**
@@ -67,7 +32,6 @@ exports.init = function(options) {
6732
* @param {function(err, repo)} callback Callback
6833
*/
6934
exports.connectToRepository = function(options, callback) {
70-
7135
var repo = new SOASTA.mPulse(options.parent.apikey, options.parent.mpulse, options.parent.repository);
7236
repo.connect(options.parent.tenant, options.parent.username, options.parent.password, function(err) {
7337
return callback && callback(err, repo);

cli/mpulse.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,32 @@ var fs = require("fs");
66

77
module.exports = function(type, params, options) {
88
cmdCore.init(options);
9-
log.debug(params);
9+
var properties;
10+
11+
if (!options.parent.apikey) {
12+
log.error("--apikey (-a) is required!");
13+
process.exit(1);
14+
}
15+
16+
try {
17+
properties = JSON.parse(params);
18+
}
19+
catch(exception) {
20+
log.error("Parsing parameters failed!");
21+
process.exit(1);
22+
}
23+
24+
log.debug("Querying for Type: " + type + " with params: " + params);
25+
26+
cmdCore.connectToRepository(options, function(err, repo) {
27+
cmdCore.handleError(err);
28+
repo.setApiKey(options.parent.apikey);
29+
30+
repo.mPulse(type, properties, function(err, result) {
31+
32+
cmdCore.handleError(err);
33+
log.info(result);
34+
35+
});
36+
});
1037
};

cli/query.js

-25
This file was deleted.

cmd.js

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
#!/usr/bin/env node
2-
var program = require("commander");
3-
var fs = require("fs");
2+
var SOASTA = require("soasta-repository");
3+
var program = SOASTA.cmd;
44

55
program
6-
.version(JSON.parse(fs.readFileSync("./package.json", "utf-8")).version)
7-
.option("-u, --username <username>", "User name")
8-
.option("-p, --password <password>", "Password")
9-
.option("-t, --tenant <tenant>", "Tenant name")
10-
.option("-r, --repository <url>", "Repository URL")
116
.option("-m, --mpulse <url>", "mPulse Query API URL")
12-
.option("-o, --output <file>", "Output file")
13-
.option("-v, --verbose", "Verbose debugging")
14-
.option("-a, --apikey", "API Key for queried domain");
7+
.option("-a, --apikey <apikey>", "API Key for queried domain");
158

16-
// commands
17-
program.command("query <type>")
18-
.description("query objects")
19-
.action(require("./cli/query.js"));
20-
21-
program.command("mpulse <type> <params>")
9+
program.command("mpulse-query <type> <params>")
2210
.description("mPulse type parameters")
2311
.action(require("./cli/mpulse.js"));
24-
// go
12+
2513
program.parse(process.argv);

lib/model/mPulse.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var log = require("../util/log"),
44
constants = require("../constants"),
5-
Repository = require("soasta-repository").Repository;
5+
Repository = require("soasta-repository").SOASTA.Repository;
66

77
/**
88
* @namespace SOASTA
@@ -53,12 +53,12 @@ var SOASTA = {
5353
this.Query = query;
5454
var self = this;
5555

56-
this.query = function(type, props, callback) {
56+
this.mPulse = function(type, props, callback) {
5757
self.Query.run(self.token, type, props, callback);
5858
};
5959

6060
this.setApiKey = function(apiKey) {
61-
self.query = new Query(queryUrl, apiKey);
61+
self.Query = new Query(queryUrl, apiKey);
6262
};
6363

6464
this.asPromises = function(Promises) {
@@ -68,7 +68,7 @@ var SOASTA = {
6868
if (promisesMPulse.hasOwnProperty(name)) {
6969
var func = promisesMPulse[name];
7070

71-
if (typeof func === "function" && name !== "asPromises") {
71+
if (typeof func === "function" && name !== "asPromises" && name !== "setApiKey") {
7272
log.debug("Replacing function " + name + " with promise-ified version.");
7373
func = Promises.denodeify(func);
7474
promisesMPulse[name] = func;

0 commit comments

Comments
 (0)