UNPKG

kui-shell

Version:

This is the monorepo for Kui, the hybrid command-line/GUI electron-based Kubernetes tool

52 lines 2.85 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const plugin_core_support_1 = require("@kui-shell/plugin-core-support"); function getMatchingStrings(cmd, spec) { return __awaiter(this, void 0, void 0, function* () { const { REPL } = yield Promise.resolve().then(() => require('@kui-shell/core/api/repl')); const completions = yield REPL.rexec(cmd); const list = completions.split(/[\n\r]/).map(_ => _.replace(/^\w+\//, '')); return list.filter(name => name.startsWith(spec.toBeCompleted)); }); } function optionals(commandLine, filter = () => true) { const options = commandLine.parsedOptions; return Object.keys(options) .filter(filter) .filter(_ => !/^(-o|--output)/.test(_)) .map(key => `${key.length === 1 ? `-${key}` : `--${key}`} ${options[key]}`) .join(' '); } function completeResourceNames(commandLine, spec) { return __awaiter(this, void 0, void 0, function* () { const { argvNoOptions, argv, parsedOptions } = commandLine; const previous = spec.toBeCompletedIdx === -1 ? commandLine.argv.length - 1 : spec.toBeCompletedIdx - 1; if (previous > 0 && (argv[previous] === '-n' || argv[previous] === '--namespace')) { const cmd = `kubectl get ns ${optionals(commandLine, _ => _ !== '-n' && _ !== '--namespace')} -o name`; return getMatchingStrings(cmd, spec); } else if ((argvNoOptions[0] === 'kubectl' || argvNoOptions[0] === 'k') && (argvNoOptions[1] === 'get' || argvNoOptions[1] === 'describe' || argvNoOptions[1] === 'annotate' || argvNoOptions[1] === 'label' || (argvNoOptions[1] === 'delete' && !parsedOptions.f && !parsedOptions.file))) { const entityType = argvNoOptions[2]; const cmd = `kubectl get ${entityType} ${optionals(commandLine)} -o name`; return getMatchingStrings(cmd, spec); } }); } exports.default = () => { plugin_core_support_1.registerTabCompletionEnumerator(completeResourceNames); }; //# sourceMappingURL=tab-completion.js.map