UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

68 lines (67 loc) 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.kubectl = exports.exec = void 0; var _child_process = require("child_process"); var _cliKit = require("cli-kit"); var _util = _interopRequireDefault(require("util")); var _utils = require("./utils"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const quotes = _utils.isWindows ? '"' : "'"; const { log } = (0, _cliKit.snooplogg)('central: kubectl'); const exec = exports.exec = _util.default.promisify(_child_process.exec); class Kubectl { async get(resource, args) { return await this.invoke('get', resource, args); } async create(resource, args) { return await this.invoke('create', resource, args); } async delete(resource, args) { return await this.invoke('delete', resource, args); } async isInstalled() { return await this.execKubectl('version', 'version'); } async invoke(action, resource, args) { const obj = { error: null, data: [] }; let logMsg = `kubectl ${action} ${resource}`; logMsg = args ? logMsg += ` ${args}` : logMsg; log(logMsg); const res = await this.execKubectl(`${action} ${resource} ${args || ''}`, resource); if (res.error) { obj.error = res.error; log(`command failed: ${res.error}`); return obj; } obj.data = this.cleanResponse(res.data); log('command success'); return obj; } async execKubectl(action, resource) { let { stdout, stderr } = await exec(`kubectl ${action} | awk ${quotes}{print $1}${quotes}`); if (stderr.includes('WARNING')) { stderr = ''; } return stderr ? { data: null, error: `K8S ${resource}: ${stderr}` } : { data: stdout, error: null }; } cleanResponse(res) { return res.split('\n').filter(str => str !== 'NAME' && str != '').sort(); } } const kubectl = exports.kubectl = new Kubectl();