cloudcms-cli
Version:
Cloud CMS Command-Line client
206 lines (169 loc) • 7.13 kB
JavaScript
var helper = require("../../helper");
var request = require("../../util/request");
var path = require("path");
var fs = require("fs");
var doInit = function(uiUrl, apiUrl, username, password, callback)
{
var retrieveCredentialsFromOneTeam = function(uiUrl, apiUrl, username, password, callback)
{
var url = uiUrl + "/_cli_config";
var payload = {
"username": username,
"password": password
};
var headers = {};
if (process.authenticator_code)
{
if (!Gitana.OAUTH2_TOKEN_REQUEST_HEADERS)
{
Gitana.OAUTH2_TOKEN_REQUEST_HEADERS = {};
}
headers["authenticator_code"] = process.authenticator_code;
}
request({
"url": url,
"method": "POST",
"json": payload,
"headers": headers
}, function (error, response, body) {
if (error) {
return callback(error);
}
if (response.status !== 200)
{
return callback({
"message": body.message
});
}
body.baseURL = apiUrl;
body._uiURL = uiUrl;
body._apiURL = apiUrl;
var credentials = JSON.parse(JSON.stringify(body));
callback(null, credentials);
});
};
// if the UI url ends with ".cloudcms.net" and it also does not start with "http://" and it does not start with "https://"
// then assume it is a SaaS hosted UI and put "https://" in fronotn
if (uiUrl.toLowerCase().indexOf(".cloudcms.net") > -1 && uiUrl.toLowerCase().indexOf("http://") !== 0 && uiUrl.toLowerCase().indexOf("https://") !== 0)
{
uiUrl = "https://" + uiUrl.substring(7);
}
// if UI url doesn't start with "http://" or "https://", then we assume "http://"
if (uiUrl.toLowerCase().indexOf("http://") === -1 && uiUrl.toLowerCase().indexOf("https://") === -1)
{
uiUrl = "http://" + uiUrl;
}
// if API url doesn't start with "http://" or "https://", then we assume "http://"
if (apiUrl.toLowerCase().indexOf("http://") === -1 && apiUrl.toLowerCase().indexOf("https://") === -1)
{
apiUrl = "http://" + apiUrl;
}
// retrieve credentials from Cloud CMS
retrieveCredentialsFromOneTeam(uiUrl, apiUrl, username, password, function(err, credentialsProperties) {
if (err) {
return callback(err);
}
if (!credentialsProperties)
{
return callback({
"message": "No credentials defined"
});
}
fs.mkdir(path.join(helper.homeDirectory(), ".cloudcms"), { recursive: true },function(err) {
if (err) {
return callback(err);
}
fs.mkdir(path.join(helper.homeDirectory(), ".cloudcms", "credentials"), { recursive: true },function(err) {
var credentialsFilePath = path.join(helper.homeDirectory(), ".cloudcms", "credentials.json");
if (process.cloudcms_profile)
{
credentialsFilePath = path.join(helper.homeDirectory(), ".cloudcms", "credentials", process.cloudcms_profile + ".json");
}
// write the credentials.json config file to the user's home directory (.cloudcms subpath)
fs.writeFileSync(credentialsFilePath, JSON.stringify(credentialsProperties, null, " "));
console.log("");
console.log("Testing connectivity...");
// test the credentials
helper.connect(function(err) {
if (err) {
fs.unlinkSync(credentialsFilePath);
var message = "Unable to connect to Cloud CMS. Please check your credentials.";
if (err.response && err.response.text)
{
var responseObject = JSON.parse("" + err.response.text);
if (responseObject && responseObject.authenticatorRequired)
{
message += " " + responseObject.message;
}
}
return callback({
"message": message
});
}
// ALL DONE
console.log("Successfully connected to Cloud CMS!");
console.log("");
console.log("Cloud CMS platform connection credentials were saved to:");
console.log(" " + credentialsFilePath);
console.log("");
});
});
});
});
};
var AbstractCommand = require("../abstract");
class InitCommand extends AbstractCommand
{
constructor()
{
super({
"name": "init",
"description": "Initializes your command line client's connection to Cloud CMS",
"schema": {
"properties": [{
"name": "ui",
"type": "string",
"description": "Enter the URL to your Cloud CMS UI endpoint:",
"helper": "The URL to your Cloud CMS UI endpoint (example: https://mytenant.cloudcms.net or http://localhost)",
"required": true,
"args": ["ui"]
}, {
"name": "api",
"type": "string",
"description": "Enter the URL to your Cloud CMS API endpoint:",
"helper": "The URL to your Cloud CMS API endpoint (example: https://api.cloudcms.com or http://localhost:8080)",
"required": true,
"default": "https://api.cloudcms.com",
"args": ["api", "a"]
}, {
"name": "username",
"type": "string",
"description": "Enter your username:",
"helper": "Your Cloud CMS user name",
"required": true,
"args": ["username", "user", "name", "u"]
}, {
"name": "password",
"type": "string",
"description": "Enter your password:",
"helper": "Your Cloud CMS password",
"required": true,
"hidden": true,
"args": ["password", "pass", "pw", "p"]
}]
},
});
}
handle(options, callback)
{
console.log("");
console.log("To get your Cloud CMS client keys and login credentials,");
console.log(" visit \"https://gitana.io\".");
console.log("");
// call workhorse function
doInit(options.ui, options.api, options.username, options.password, function(err) {
callback(err);
});
}
}
module.exports = InitCommand;