UNPKG

rainbow-cli

Version:

Rainbow CLI application based on the Rainbow SDK for Node.js

307 lines (263 loc) 9.68 kB
"use strict"; const NodeSDK = require("../common/SDK"); const Message = require("../common/Message"); const Exit = require("../common/Exit"); class CSite { constructor(prefs) { this._prefs = prefs; } _createSite(token, name, companyId) { let that = this; return new Promise(function(resolve, reject) { var data = { name: name, companyId: companyId || that._prefs.user.companyId, status: "active" }; NodeSDK.post("/api/rainbow/admin/v1.0/sites", token, data) .then(function(json) { resolve(json); }) .catch(function(err) { reject(err); }); }); } _getSite(token, id) { return new Promise(function(resolve, reject) { NodeSDK.get("/api/rainbow/admin/v1.0/sites/" + id, token) .then(function(json) { resolve(json); }) .catch(function(err) { reject(err); }); }); } _deleteSite(token, id) { var that = this; return new Promise(function(resolve, reject) { NodeSDK.delete("/api/rainbow/admin/v1.0/sites/" + id, token) .then(function(json) { resolve(json); }) .catch(function(err) { reject(err); }); }); } _getListOfSites(token, options) { return new Promise(function(resolve, reject) { var offset = ""; if (options.page > 0) { offset = "&offset="; if (options.page > 1) { offset += options.limit * (options.page - 1); } else { offset += 0; } } var limit = "&limit=" + Math.min(options.limit, 1000); var filter = "?format=full"; if (options.company) { filter += "&companyId=" + options.company; } if (options.name) { filter += "&name=" + encodeURIComponent(options.name); } NodeSDK.get("/api/rainbow/admin/v1.0/sites" + filter + offset + limit, token) .then(function(json) { resolve(json); }) .catch(function(err) { reject(err); }); }); } deleteSite(id, options) { var that = this; var doDelete = function(id) { Message.action("Delete site", id); let spin = Message.spin(options); NodeSDK.start(that._prefs.email, that._prefs.password, that._prefs.host) .then(function() { Message.log("execute action..."); return that._deleteSite(that._prefs.token, id); }) .then(function(json) { Message.unspin(spin); Message.log("action done...", json); if (options.noOutput) { Message.out(json); } else { Message.lineFeed(); Message.success(options); Message.log("finished! will exit!"); Exit.ok(); } }) .catch(function(err) { Message.unspin(spin); Message.error(err, options); Exit.error(); }); }; Message.welcome(options); if (this._prefs.token && this._prefs.user) { Message.loggedin(this._prefs, options); if (options.noconfirmation) { doDelete(id); } else { Message.confirm("Are-you sure ? It will remove definitively this site").then(function(confirm) { if (confirm) { doDelete(id); } else { Message.canceled(options); Exit.error(); } }); } } else { Message.notLoggedIn(options); Exit.error(); } } getSites(options) { var that = this; Message.welcome(options); if (this._prefs.token && this._prefs.user) { Message.loggedin(this._prefs, options); if (!options.csv) { Message.action("List sites", null, options); } let spin = Message.spin(options); NodeSDK.start( this._prefs.email, this._prefs.password, this._prefs.host, this._prefs.proxy, this._prefs.appid, this._prefs.appsecret ) .then(function() { Message.log("execute action..."); return that._getListOfSites(that._prefs.token, options); }) .then(function(json) { Message.unspin(spin); Message.log("action done...", json); if (options.csv) { Message.csv(options.csv, json.data, false) .then(() => {}) .catch(err => { Exit.error(); }); } else if (options.noOutput) { Message.out(json); } else { if (json.total > json.limit) { Message.tablePage(json, options); } Message.lineFeed(); Message.tableSites(json, options); } Message.log("finished! will exit!"); Exit.ok(); }) .catch(function(err) { Message.unspin(spin); Message.error(err, options); Exit.error(); }); } else { Message.notLoggedIn(options); Exit.error(); } } getSite(id, options) { var that = this; Message.welcome(options); if (this._prefs.token && this._prefs.user) { Message.loggedin(this._prefs, options); Message.action("Get informaton for site", id, options); let spin = Message.spin(options); NodeSDK.start( this._prefs.email, this._prefs.password, this._prefs.host, this._prefs.proxy, this._prefs.appid, this._prefs.appsecret ) .then(function() { Message.log("execute action..."); return that._getSite(that._prefs.token, id); }) .then(function(json) { Message.unspin(spin); Message.log("action done...", json); if (options.noOutput) { Message.out(json.data); } else { Message.lineFeed(); Message.table2D(json.data); Message.lineFeed(); Message.success(options); Message.log("finished! will exit!"); Exit.ok(); } }) .catch(function(err) { Message.unspin(spin); Message.error(err, options); Exit.error(); }); } else { Message.notLoggedIn(options); Exit.error(); } } createSite(name, companyId, options) { var that = this; Message.welcome(options); if (this._prefs.token && this._prefs.user) { Message.loggedin(this._prefs, options); Message.action("Create new site", name, options); let spin = Message.spin(options); NodeSDK.start( this._prefs.email, this._prefs.password, this._prefs.host, this._prefs.proxy, this._prefs.appid, this._prefs.appsecret ) .then(function() { Message.log("execute action..."); return that._createSite(that._prefs.token, name, companyId, options); }) .then(function(json) { Message.unspin(spin); Message.log("action done...", json); if (options.noOutput) { Message.out(json.data); } else { Message.lineFeed(); Message.printSuccess("Site created with Id", json.data.id, options); Message.success(options); Message.log("finished! will exit!"); Exit.ok(); } }) .catch(function(err) { Message.unspin(spin); Message.error(err, options); Exit.error(); }); } else { Message.notLoggedIn(options); Exit.error(); } } } module.exports = CSite;