Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | #!/usr/bin/env node |
| 2 | ;(function() { |
| 3 | /** |
| 4 | * Copyright 2014 IBM Corp. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | **/ |
| 18 | |
| 19 | var util = require("util"); |
| 20 | var request = require("request"); |
| 21 | var colors = require('colors'); |
| 22 | var apiRequest = require("./lib/request"); |
| 23 | var config = require("./lib/config"); |
| 24 | |
| 25 | var commands = { |
| 26 | "target": function() { |
| 27 | var target = process.argv[3]; |
| 28 | if (target) { |
| 29 | if (!/^https?:\/\/.+/.test(target)) { |
| 30 | console.warn("Invalid target url"); |
| 31 | return; |
| 32 | } |
| 33 | if (target.slice(-1) == "/") { |
| 34 | target = target.slice(0,target.length-1); |
| 35 | } |
| 36 | var oldTarget = config.target; |
| 37 | config.target = target; |
| 38 | } else { |
| 39 | console.log("Target: ".yellow+config.target); |
| 40 | } |
| 41 | |
| 42 | }, |
| 43 | "nodes": function() { |
| 44 | apiRequest('/nodes',{}).then(logNodeList).otherwise(logFailure); |
| 45 | }, |
| 46 | "node": function() { |
| 47 | apiRequest('/nodes/'+process.argv[3],{}).then(logNodeList).otherwise(logFailure); |
| 48 | }, |
| 49 | "enable-node": function() { |
| 50 | apiRequest('/nodes/'+process.argv[3],{ |
| 51 | method: "PUT", |
| 52 | body: JSON.stringify({enabled:true}) |
| 53 | }).then(logNodeList).otherwise(logFailure); |
| 54 | }, |
| 55 | "disable-node": function() { |
| 56 | apiRequest('/nodes/'+process.argv[3],{ |
| 57 | method: "PUT", |
| 58 | body: JSON.stringify({enabled:false}) |
| 59 | }).then(logNodeList).otherwise(logFailure); |
| 60 | }, |
| 61 | "install": function() { |
| 62 | apiRequest('/nodes',{ |
| 63 | method: "POST", |
| 64 | body: JSON.stringify({module:process.argv[3]}) |
| 65 | }).then(logNodeList).otherwise(logFailure); |
| 66 | }, |
| 67 | "remove": function() { |
| 68 | apiRequest('/nodes/'+process.argv[3],{ |
| 69 | method: "DELETE" |
| 70 | }).then(logNodeList).otherwise(logFailure); |
| 71 | }, |
| 72 | "search": function() { |
| 73 | var options = { |
| 74 | method: "GET", |
| 75 | url: 'https://registry.npmjs.org/-/_view/byKeyword?startkey=["node-red"]&endkey=["node-red",{}]&group_level=3' , |
| 76 | headers: { |
| 77 | 'Accept': 'application/json', |
| 78 | } |
| 79 | }; |
| 80 | request(options, function (error, response, body) { |
| 81 | if (!error && response.statusCode == 200) { |
| 82 | var info = (JSON.parse(body)).rows; |
| 83 | var filter = null; |
| 84 | if (process.argv[3]) { |
| 85 | filter = new RegExp(process.argv[3]); |
| 86 | } |
| 87 | for (var i=0;i<info.length;i++) { |
| 88 | var n = info[i]; |
| 89 | if (!filter || filter.test(n.key[1]) || filter.test(n.key[2])) { |
| 90 | console.log(n.key[1] + (" - "+ n.key[2]).grey); |
| 91 | } |
| 92 | } |
| 93 | } else if (error) { |
| 94 | console.log(error.toString().red); |
| 95 | } else { |
| 96 | console.log((response.statusCode+": "+body).red); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function logNodeList(nodes) { |
| 103 | if (!util.isArray(nodes)) { |
| 104 | nodes = [nodes]; |
| 105 | } |
| 106 | for (var i=0;i<nodes.length;i++) { |
| 107 | var n = nodes[i]; |
| 108 | console.log(formatNodeInfo(n)) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | function logFailure(msg) { |
| 113 | console.log(msg.red); |
| 114 | } |
| 115 | |
| 116 | function formatBoolean(v,c) { |
| 117 | if (v) { |
| 118 | return ("["+c+"]"); |
| 119 | } else { |
| 120 | return ("[ ]"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | function formatNodeInfo(n) { |
| 125 | var inError = n.hasOwnProperty("err"); |
| 126 | |
| 127 | var str = formatBoolean(n.enabled,"X")+formatBoolean(n.loaded,"L")+" "; |
| 128 | str += n.id; |
| 129 | if (n.enabled && n.loaded) { |
| 130 | str = str.green; |
| 131 | } else if (n.enabled && n.err) { |
| 132 | str = str.red; |
| 133 | } else { |
| 134 | str = str.yellow; |
| 135 | } |
| 136 | if (n.module) { |
| 137 | str += " ["+n.module+"]"; |
| 138 | } |
| 139 | str += " "+n.types.join(", "); |
| 140 | if (n.err) { |
| 141 | str+=" "+n.err.red; |
| 142 | } |
| 143 | return str; |
| 144 | } |
| 145 | |
| 146 | if (commands[process.argv[2]]) { |
| 147 | commands[process.argv[2]].call(); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | })(); |