forked from node-red/node-red-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.js
214 lines (199 loc) · 6.86 KB
/
result.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* Copyright OpenJS Foundation and other contributors, https://openjsf.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
var Table = require('cli-table');
var util = require("util");
const config = require("./config");
var outputFormat = "text";
function logDetails(result) {
if (result.nodes) { // summary of node-module
logModule(result);
} else { // detailed node-set
logNodeSet(result);
}
}
function logModule(result) {
if (outputFormat === "json") {
console.log(JSON.stringify(result, null, 4));
return;
}
var table = plainTable({plain:true});
table.push(["Module:",result.name]);
table.push(["Version:",result.version]);
console.log(table.toString());
console.log();
logNodeList(result.nodes);
}
function logList(result) {
if (outputFormat === "json") {
console.log(JSON.stringify(result, null, 4));
return;
}
if (result.nodes) { // summary of node-module
logNodeList(result.nodes);
} else { // summary of node-set
logNodeList(result);
}
}
function logNodeSet(node) {
if (outputFormat === "json") {
console.log(JSON.stringify(node, null, 4));
return;
}
if (Array.isArray(node)) {
if (node.length > 0) {
node = node[0];
}
}
var table = plainTable({plain:true});
table.push(["Name:", node.id]);
table.push(["Module:",node.module]);
table.push(["Version:",node.version]);
table.push(["Types:", node.types.join(", ")]);
table.push(["State:", (node.err?node.err:(node.enabled?"enabled":"disabled"))]);
console.log(table.toString());
}
function logNodeList(nodes) {
if (outputFormat === "json") {
console.log(JSON.stringify(nodes, null, 4));
return;
}
if (!Array.isArray(nodes)) {
nodes = [nodes];
}
nodes.sort(function(n1,n2) {
var id1 = n1.id.toLowerCase();
var id2 = n2.id.toLowerCase();
if (id1<id2) {
return -1;
}
if (id1>id2) {
return 1;
}
return 0;
});
var nodeTable = plainTable();
nodeTable.push(["Nodes","Types","State"]);
for(var i=0;i<nodes.length;i++) {
var node = nodes[i];
var state = node.enabled?(node.err?"error":"enabled"):"disabled";
var enabled = node.enabled&&!node.err;
var types = "";
if (node.types) {
for (var j=0;j<node.types.length;j++) {
types += (j>0?"\n":"")+(enabled?node.types[j]:node.types[j]);
}
}
if (types.length === 0) {
types = "none";
}
nodeTable.push([enabled?node.id:node.id,
enabled?types:types,
state]);
}
console.log(nodeTable.toString());
}
function logProjectList(projects) {
if (outputFormat === "json") {
console.log(JSON.stringify(projects, null, 4));
return;
}
var projectList = projects.projects || [];
if (projectList.length === 0) {
console.log("No projects available");
return;
}
projectList.sort();
projectList.forEach(proj => {
console.log((projects.active === proj ? "*":" ")+" "+proj);
});
}
function plainTable(opts) {
opts = opts||{};
opts.chars = {
'top': '' , 'top-mid': '' , 'top-left': '' , 'top-right': '',
'bottom': '' , 'bottom-mid': '' , 'bottom-left': '' ,
'bottom-right': '', 'left': '' , 'left-mid': '' , 'mid': '' ,
'mid-mid': '', 'right': '' , 'right-mid': '' , 'middle': ' ' };
opts.style = { 'padding-left': 0, 'padding-right': 0 };
return new Table(opts);
}
module.exports = {
log:function(msg) {
console.log(msg);
},
warn:function(msg) {
if (process.env.NR_TRACE && msg.stack) {
console.warn(msg.stack);
}
if (msg.response) {
if (msg.response.status === 401) {
if (outputFormat === "json") {
console.log(JSON.stringify({error:"Not logged in. Use 'login' to log in.", status: 401}, null, 4));
} else {
console.warn("Not logged in. Use 'login' to log in.");
}
} else if (msg.response.data) {
if (msg.response.status === 404 && !msg.response.data.message) {
if (outputFormat === "json") {
console.log(JSON.stringify({error:"Node-RED Admin API not found. Use 'target' to set API location", status: 404}, null, 4));
} else {
console.warn("Node-RED Admin API not found. Use 'target' to set API location");
}
} else {
if (outputFormat === "json") {
console.log(JSON.stringify({error:msg.response.data.message, status: msg.response.status}, null, 4));
} else {
console.warn(msg.response.status+": "+msg.response.data.message);
}
}
} else {
if (outputFormat === "json") {
console.log(JSON.stringify({error:msg.toString(), status: msg.response.status}, null, 4));
} else {
console.warn(msg.response.status+": "+msg.toString());
}
}
} else {
var text = msg.toString();
if (/ECONNREFUSED/.test(text)) {
text = "Failed to connect to "+config.target();
}
if (outputFormat === "json") {
console.log(JSON.stringify({error:text}));
} else {
console.warn(text);
}
}
},
help: function(command) {
var helpText = "Usage:" + "\n" +
" node-red-admin " + command.usage + "\n\n" +
"Description:" + "\n" +
" " + command.description + "\n\n" +
"Options:" + "\n" +
(command.options ? " " + command.options + "\n" : "") +
" -h|? --help display this help text and exit";
console.log(helpText);
return Promise.resolve();
},
logList:logList,
logNodeList:logNodeList,
logDetails:logDetails,
logProjectList:logProjectList,
format: function(format) {
outputFormat = format;
}
};