UNPKG

@sap/cli-core

Version:

Command-Line Interface (CLI) Core Module

134 lines (133 loc) 4.84 kB
"use strict"; /* eslint-disable */ var exec = require("./exec.js"); var getResults = exec.getResults; module.exports.searchAllApps = function (names, targetOrg, targetSpace) { var nextUrl = "/v3/apps?names=" + names.join(","); var fnFetchPage = function () { return exec.cf(["curl", nextUrl], getResults); }; var res = []; var fnSearchPage = function () { return fnFetchPage().then((apps) => { try { apps = JSON.parse(apps); } catch (e) { console.error("Failed to parse the following body:"); console.error(apps); throw e; } if (apps.errors) { console.error(apps.errors[0]); throw apps.errors[0]; } for (const curResource of apps.resources) { var resIndex = names.indexOf(curResource.name); if (resIndex > -1) { res[resIndex] = curResource; for (var x = 0; x < names.length; x++) { if (!res[x]) { break; } if (x === names.length - 1) { return Promise.resolve(); } } } } if (apps.pagination && apps.pagination.next && apps.pagination.next.href) { nextUrl = "/v3/" + apps.pagination.next.href.split("/v3/")[1]; } else { return Promise.resolve(); } return fnSearchPage(); }); }; return exec .cf(["curl", "/v3/organizations?names=" + targetOrg], getResults) .then((orgs) => { orgs = JSON.parse(orgs); var orgGuid = orgs.resources && orgs.resources[0] && orgs.resources[0].guid; nextUrl = nextUrl + "&organization_guids=" + orgGuid; return exec.cf([ "curl", "/v3/spaces?organization_guids=" + orgGuid + "&names=" + targetSpace, ], getResults); }) .then((spaces) => { spaces = JSON.parse(spaces); var spaceGuid = spaces.resources && spaces.resources[0] && spaces.resources[0].guid; nextUrl = nextUrl + "&space_guids=" + spaceGuid; }) .then(() => fnSearchPage()) .then(() => res); }; module.exports.getServiceKey = function (serviceName, serviceKeyName) { function resourceFromPagination(cfResult) { if (!cfResult || cfResult.length === 0) { return []; } const oResult = JSON.parse(cfResult); if (oResult.pagination["total_results"] > 0) { return oResult.resources; } return []; } return Promise.all([ exec .cf(["curl", `/v3/service_instances?names=${serviceName}`], getResults) .then((result) => resourceFromPagination(result)) .catch((err) => console.error(err.message)), exec .cf(["curl", `/v3/service_credential_bindings?names=${serviceKeyName}`], getResults) .then((result) => resourceFromPagination(result)) .catch((err) => console.error(err.message)), ]) .then((result) => { var serviceGuid = result[0][0].guid; var credKeys = result[1]; if (!serviceGuid || credKeys.length === 0) { return null; } var serviceKey = credKeys.filter((key) => key.relationships["service_instance"].data.guid === serviceGuid); if (serviceKey.length === 0) { return null; } return exec .cf([ "curl", `/v3/service_credential_bindings/${serviceKey[0].guid}/details`, ], getResults) .then((details) => JSON.parse(details)); }) .catch((err) => console.error(`Failed to fetch service key ${serviceKeyName}. ${err}`)); }; module.exports.getEnv = function (names, targetOrg, targetSpace) { if (!Array.isArray(names)) { names = [names]; } return module.exports .searchAllApps(names, targetOrg, targetSpace) .then((apps) => Promise.all(apps.map((a) => exec.cf(["curl", `/v3/apps/${a.guid}/env`], getResults)))); }; module.exports.getCurrentTarget = function () { return exec.cf(["t"], getResults).then((result) => { var json = result .replace(/ /g, "") .split("\n") .map((l) => { var i = l.indexOf(":"); if (i < 0) { return l; } return '"' + l.substring(0, i) + '":"' + l.substring(i + 1) + '"'; }); json.pop(); json = "{" + json.join(",\n") + "}"; return JSON.parse(json); }); };