Skip to content

Commit 3977a3b

Browse files
committed
More consistent suppport of --json flag
1 parent 346f9bb commit 3977a3b

File tree

7 files changed

+63
-15
lines changed

7 files changed

+63
-15
lines changed

lib/commands/hash.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ try { bcrypt = require('bcrypt'); }
1919
catch(e) { bcrypt = require('bcryptjs'); }
2020

2121
function command(argv,result) {
22+
if (argv.json) {
23+
console.warn("hash-pw command does not support json format output")
24+
}
2225
return new Promise(resolve => {
2326
prompt.read({prompt:"Password:",silent: true},function(err, password) {
2427
if (password) {
@@ -28,7 +31,7 @@ function command(argv,result) {
2831
});
2932
});
3033
}
31-
command.alias = "hash-pwd";
34+
command.alias = "hash-pw";
3235
command.usage = command.alias;
3336
command.description = "Creates a password hash suitable for use with adminAuth or httpNodeAuth";
3437

lib/commands/login.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ var prompt = require("../prompt");
2020

2121
function command(argv,result) {
2222
config.tokens(null);
23+
if (argv.json) {
24+
console.warn("login command does not support json format output")
25+
}
2326
return request.request('/auth/login',{}).then(function(resp) {
2427
if (resp.type) {
2528
if (resp.type == "credentials") {

lib/commands/remove.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
**/
1616

1717
var request = require("../request");
18-
18+
1919
function command(argv,result) {
2020
var module = argv._[1];
2121
if (!module) {
@@ -24,7 +24,11 @@ function command(argv,result) {
2424
return request.request('/nodes/' + module, {
2525
method: "DELETE"
2626
}).then(function() {
27-
result.log("Uninstalled " + module);
27+
if (argv.json) {
28+
result.log(JSON.stringify({message: "Uninstalled " + module}," ",4));
29+
} else {
30+
result.log("Uninstalled " + module);
31+
}
2832
}).catch(result.warn);
2933
}
3034
command.alias = "remove";

lib/commands/search.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,30 @@ function command(argv,result) {
3939
var index = label.indexOf(module);
4040
matches.push({
4141
label: label,
42-
index: index===-1?1000:index
42+
index: index===-1?1000:index,
43+
n:n
4344
});
4445
}
4546
matches.sort(function(A,B) { return A.index - B.index; });
46-
matches.forEach(function(m) {
47-
result.log(m.label);
48-
});
47+
if (argv.json) {
48+
result.log(JSON.stringify(matches.map(function(m) { return {
49+
name: m.n.name,
50+
description: m.n.description,
51+
version: (m.n['dist-tags']&& m.n['dist-tags'].latest)?m.n['dist-tags'].latest:undefined,
52+
updated_at: m.n.updated_at
53+
}})," ",4));
54+
} else {
55+
matches.forEach(function(m) {
56+
result.log(m.label);
57+
});
58+
}
4959

5060
} else {
51-
result.log("No results found");
61+
if (argv.json) {
62+
result.log("[]")
63+
} else {
64+
result.log("No results found");
65+
}
5266
}
5367
} else {
5468
result.warn(response.status + ": " + response.data);

lib/commands/target.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ function command(argv,result) {
2929
config.target(target);
3030

3131
}
32-
result.log("Target: " + config.target());
32+
if (argv.json) {
33+
result.log(JSON.stringify({target: config.target()}," ",4))
34+
} else {
35+
result.log("Target: " + config.target());
36+
}
3337
}
3438

3539
command.alias = "target";

lib/result.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,38 @@ module.exports = {
154154
}
155155
if (msg.response) {
156156
if (msg.response.status === 401) {
157-
console.warn("Not logged in. Use 'login' to log in.");
157+
if (outputFormat === "json") {
158+
console.log({error:"Not logged in. Use 'login' to log in.", status: 401});
159+
} else {
160+
console.warn("Not logged in. Use 'login' to log in.");
161+
}
158162
} else if (msg.response.data) {
159163
if (msg.response.status === 404 && !msg.response.data.message) {
160-
console.warn("Node-RED Admin API not found. Use 'target' to set API location");
164+
if (outputFormat === "json") {
165+
console.log({error:"Node-RED Admin API not found. Use 'target' to set API location", status: 404});
166+
} else {
167+
console.warn("Node-RED Admin API not found. Use 'target' to set API location");
168+
}
161169
} else {
162-
console.warn(msg.response.status+": "+msg.response.data.message);
170+
if (outputFormat === "json") {
171+
console.log({error:msg.response.data.message, status: msg.response.status});
172+
} else {
173+
console.warn(msg.response.status+": "+msg.response.data.message);
174+
}
163175
}
164176
} else {
165-
console.warn(msg.response.status+": "+msg.toString());
177+
if (outputFormat === "json") {
178+
console.log({error:msg.toString(), status: msg.response.status});
179+
} else {
180+
console.warn(msg.response.status+": "+msg.toString());
181+
}
166182
}
167183
} else {
168-
console.warn(msg.toString());
184+
if (outputFormat === "json") {
185+
console.log({error:msg.toString()});
186+
} else {
187+
console.warn(msg.toString());
188+
}
169189
}
170190
},
171191
help:function(command) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-admin",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "The Node-RED admin command line interface",
55
"homepage": "http://nodered.org",
66
"bugs": {

0 commit comments

Comments
 (0)