UNPKG

hana-cli

Version:
121 lines (113 loc) 4.2 kB
// @ts-check import * as baseLite from '../utils/base-lite.js' import * as cf from '../utils/cf.js' import * as btp from '../utils/btp.js' import { buildDocEpilogue } from '../utils/doc-linker.js' export const command = 'hcStart [name]' export const aliases = ['hcstart', 'hc_start', 'start'] export const describe = baseLite.bundle.getText("hcStart") export const builder = (yargs) => yargs.options(baseLite.getBuilder({ name: { alias: ['n'], type: 'string', default: `**default**`, desc: baseLite.bundle.getText("hc_instance_name") } }, false)).wrap(160).example('hana-cli hcStart --name myInstance', baseLite.bundle.getText('hcStartExample')).wrap(160).epilog(buildDocEpilogue('hanaCloudStart', 'hana-cloud', ['hanaCloudStop', 'hanaCloudInstances'])) /** * Command handler function * @param {object} argv - Command line arguments from yargs * @returns {Promise<void>} */ export async function handler(argv) { const base = await import('../utils/base.js') base.promptHandler(argv, hcStart, { name: { description: base.bundle.getText("hc_instance_name"), type: 'string', required: true } }, false) } /** * Suggest using hana-cli commands in output messages * @param {string} stdout - Standard output from CF command * @param {string} instanceName - HANA Cloud instance name * @returns {string} - Modified output message */ function suggestHanaCli(stdout, instanceName) { let out = stdout.split("\n") // Replace message 'Update in progress. Use 'cf services' or 'cf service dbhana-hana' to check operation status.' if (out[3]) { out[3] = `${baseLite.bundle.getText("hc.updateProgress")}. ${baseLite.bundle.getText("hc.progress", [instanceName])}` } return out.join("\n") } /** * Suggest using BTP hana-cli commands in output messages * @param {string} stdout - Standard output from BTP command * @param {string} instanceName - HANA Cloud instance name * @returns {string} - Modified output message */ function suggestBTPHanaCli(stdout, instanceName) { let out = stdout // Replace message 'Update in progress. Use 'cf services' or 'cf service dbhana-hana' to check operation status.' if (out) { out = `${baseLite.bundle.getText("hc.updateProgress")}. ${baseLite.bundle.getText("hc.progress", [instanceName])}` } return out } /** * Start HANA Cloud instance * @param {object} prompts - Input prompts with instance name * @returns {Promise<void>} */ export async function hcStart(prompts) { const base = await import('../utils/base.js') base.debug(`hcStart`) try { //BTP Multi-Environment Instances let results = [] try { if (prompts.name === '**default**') { results = await btp.getHANAServiceInstances() } else { results = await btp.getHANAInstanceByName(prompts.name) } } catch (error) { base.debug(error) // console.log(error) } base.debug(results) if (results && results.length > 0) { // @ts-ignore for (let item of results) { const stdout = await btp.startHana(item.name) console.log(suggestBTPHanaCli(stdout, item.name)) } } else { try { if (prompts.name === '**default**') { results = await cf.getHANAInstances() } else { results = await cf.getHANAInstanceByName(prompts.name) } // @ts-ignore if (!results || !results.resources) { return base.error(new Error(baseLite.bundle.getText("hc.error"))) } // @ts-ignore for (let item of results.resources) { const stdout = await cf.startHana(item.name) console.log(suggestHanaCli(stdout, item.name)) } } catch (error) { base.debug(error) } } return base.end() } catch (error) { base.error(error) } }