UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

93 lines 3.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.linksOf = linksOf; exports.parentLink = parentLink; exports.getLastPartOfLink = getLastPartOfLink; exports.isCplnLink = isCplnLink; exports.parseLink = parseLink; exports.cplnUriOf = cplnUriOf; const format_1 = require("./format"); // A control plane link is <prefix><kind>/<name>, e.g. for a secret: // cpln://secret/<name>, //secret/<name>, or /org/<org>/secret/<name>. A self link // carries /gvc/<gvc>/ before a gvc-scoped kind, the way resolveToLink builds it. // A .<key> after the name selects one field, the way a workload env value does. // Anchored, so a nested path never resolves to a resource by accident. const CPLN_LINK_RE = /^(?:cpln:\/\/|\/\/|\/org\/([^/]+)\/(?:gvc\/([^/]+)\/)?)([a-z][a-z0-9-]*)\/([^/]+)$/i; // The prefix half of the same rule, for values that are link-shaped without being // valid links. The kind segment keeps //example.com/path from reading as one. const CPLN_LINK_PREFIX_RE = /^(?:cpln:\/\/|\/\/[a-z][a-z0-9-]*\/|\/org\/[^/]+\/)/i; function get(target, prop) { for (let lnk of target) { if (lnk.rel == prop) { return lnk.href; } } return undefined; } function linksOf(linked) { return new Proxy(linked.links, { get: get, }); } function parentLink(link) { let i = link.lastIndexOf('/'); return link.substring(0, i); } /** * @param link A string that contains slashes indicating that it is a link * @returns The last part of the link string after the last slash */ function getLastPartOfLink(link) { const parts = link.split('/'); return parts[parts.length - 1]; } /** * Reports whether a value is written like a control plane link, valid or not. * A link-shaped value never doubles as a local file path or a variable name. * * @param {string} value - The value as the user wrote it. * @returns {boolean} True for a cpln:// URI, a //<kind>/ short link, and an /org/<org>/ self link. */ function isCplnLink(value) { return CPLN_LINK_PREFIX_RE.test(value); } /** * Parses a control plane link into the resource it names. The link is a * cpln://<kind>/<name> URI, a //<kind>/<name> short link, or an * /org/<org>/<kind>/<name> self link, with an optional .<key> field selector * after the name. * * @param {string} link - The link as the user wrote it. * @returns {ParsedLink | null} The parts, or null when the value names no resource. */ function parseLink(link) { var _a, _b; const match = CPLN_LINK_RE.exec(link); if (match === null) { return null; } const org = (_a = match[1]) !== null && _a !== void 0 ? _a : ''; const gvc = (_b = match[2]) !== null && _b !== void 0 ? _b : ''; const kind = match[3].toLowerCase(); const segment = match[4]; // A dot selects one field of the resource's data, the way a workload env value // does; a CPLN name never contains one, so the split is unambiguous. const dot = segment.indexOf('.'); const name = dot === -1 ? segment : segment.slice(0, dot); const key = dot === -1 ? '' : segment.slice(dot + 1); // A valid name, and a field behind every dot; a trailing dot selects nothing if (!(0, format_1.isNameValid)(name) || (dot !== -1 && key === '')) { return null; } return { org, gvc, kind, name, key }; } /** * Builds the canonical cpln:// URI for a parsed link. * * @param {ParsedLink} parsed - The resource's kind, name, and selected field if any. * @returns {string} The cpln://<kind>/<name>[.<key>] URI. */ function cplnUriOf(parsed) { return `cpln://${parsed.kind}/${parsed.name}${parsed.key === '' ? '' : `.${parsed.key}`}`; } //# sourceMappingURL=links.js.map