UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

76 lines 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Convert = void 0; const command_1 = require("../cli/command"); const functions_1 = require("../util/functions"); const generic_1 = require("./generic"); const options_1 = require("./options"); const js_yaml_1 = require("js-yaml"); const converter_1 = require("../k8s-converter/converter"); const io_1 = require("../util/io"); const resolver_1 = require("./resolver"); const format_1 = require("../format/format"); function withConvertOptions(yargs) { return yargs.options({ protocol: { requiresArg: true, choices: ['http', 'http2', 'grpc', 'tcp'], description: 'Protocol to use for all container ports', }, }); } class Convert extends command_1.Command { constructor() { super(...arguments); this.command = 'convert'; this.describe = 'Convert k8s files to cpln files'; } builder(yargs) { return (0, functions_1.pipe)( // generic_1.withFileInputOptions, withConvertOptions, options_1.withAllOptions)(yargs); } async handle(args) { const resources = []; // Accumulate the path of every file const filePaths = (0, io_1.readFileInputsOption)(args.file); // Convert K8s to cpln resources const k8sConverter = new converter_1.K8sConverter(filePaths, this.session.context, this.session, args.protocol); try { const convertedK8s = await k8sConverter.convert(); resources.push(...convertedK8s); // If a GVC is specified and there were pull secrets found, add them to the GVC if (this.session.context.gvc && k8sConverter.pullSecrets.length != 0) { // Fetch the GVC const gvcLink = (0, resolver_1.resolveToLink)('gvc', this.session.context.gvc, this.session.context); const gvc = (0, format_1.slimObject)(await this.client.get(gvcLink)); // Add the pull secrets to the GVC if (!gvc.spec) { gvc.spec = { pullSecretLinks: [], }; } if (!gvc.spec.pullSecretLinks) { gvc.spec.pullSecretLinks = []; } gvc.spec.pullSecretLinks.push(...k8sConverter.pullSecrets); // Add GVC to the resource list that will be sent to the user resources.push(gvc); } } catch (e) { this.session.abort({ error: e }); } if (this.session.format.output == 'yaml' || this.session.format.output == 'yaml-slim') { const resourcesAsStrings = []; for (const resource of resources) { resourcesAsStrings.push((0, js_yaml_1.safeDump)(JSON.parse(JSON.stringify(resource)))); } this.session.out(resourcesAsStrings.join('\n---\n')); return; } await this.session.outFormat(resources); } } exports.Convert = Convert; //# sourceMappingURL=convert.js.map