@controlplane/cli
Version:
Control Plane Corporation CLI
271 lines • 8.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.expressionToTerm = exports.withQueryOptions = exports.fromQuerySpecOptions = exports.fromQueryOptions = exports.withQuerySpecOptions = exports.defaultProps = exports.ops = void 0;
const objects_1 = require("../util/objects");
exports.ops = ['~', '=', '>', '>=', '<', '<=', '!=', '!', ''];
exports.defaultProps = [
//
'name',
'id',
'version',
'created',
'lastModified',
];
function withQuerySpecOptions(schema) {
var _a, _b;
const group = 'Query spec options:';
let props = (_b = (0, objects_1.cartesian)((_a = schema === null || schema === void 0 ? void 0 : schema.props) !== null && _a !== void 0 ? _a : exports.defaultProps, exports.ops, (s1, s2) => s1.concat(s2))) !== null && _b !== void 0 ? _b : [];
const fuzzy = ['~', '*~'];
let rels = (0, objects_1.cartesian)(schema === null || schema === void 0 ? void 0 : schema.rels, exports.ops, (s1, s2) => s1.concat(s2));
return (yargs) => {
yargs.options({
'query-match': {
group,
description: 'Query filter type',
default: 'all',
requiresArg: true,
choices: ['all', 'none', 'any'],
},
'query-property': {
group,
description: 'Property to query',
completions: [...props, ...fuzzy],
requiresArg: true,
multiple: true,
},
'query-tag': {
group,
description: 'Tag to query',
requiresArg: true,
completions: fuzzy,
multiple: true,
},
'query-rel': {
group,
description: 'Relation to query',
requiresArg: true,
completions: rels,
multiple: true,
},
});
if (schema === null || schema === void 0 ? void 0 : schema.kinds) {
yargs.option({
'query-kind': {
group,
description: 'Kind to query',
requiresArg: true,
choices: schema.kinds,
},
});
}
return yargs;
};
}
exports.withQuerySpecOptions = withQuerySpecOptions;
function fromQueryOptions(opts) {
const query = {
spec: {
match: opts.match,
terms: [],
},
};
for (let prop of (0, objects_1.toArray)(opts.property)) {
query.spec.terms.push(expressionToTerm('property', prop));
}
for (let tag of (0, objects_1.toArray)(opts.tag)) {
query.spec.terms.push(expressionToTerm('tag', tag));
}
// Handle rel
let isGvcRelSpecified = false;
for (let rel of (0, objects_1.toArray)(opts.rel)) {
const term = expressionToTerm('rel', rel);
if (term.rel == 'gvc') {
isGvcRelSpecified = true;
}
query.spec.terms.push(term);
}
if (!isGvcRelSpecified && opts.gvc) {
query.spec.terms.push({ op: '=', rel: 'gvc', value: opts.gvc });
}
if (query.spec.terms.length < 1) {
return undefined;
}
return query;
}
exports.fromQueryOptions = fromQueryOptions;
function fromQuerySpecOptions(opts) {
const query = {
kind: opts.queryKind,
spec: {
match: opts.queryMatch,
terms: [],
},
};
for (let prop of (0, objects_1.toArray)(opts.queryProperty)) {
query.spec.terms.push(expressionToTerm('property', prop));
}
for (let tag of (0, objects_1.toArray)(opts.queryTag)) {
query.spec.terms.push(expressionToTerm('tag', tag));
}
for (let rel of (0, objects_1.toArray)(opts.queryRel)) {
query.spec.terms.push(expressionToTerm('rel', rel));
}
if (query.spec.terms.length < 1) {
return undefined;
}
return query;
}
exports.fromQuerySpecOptions = fromQuerySpecOptions;
function withQueryOptions(schema) {
var _a, _b;
const group = 'Query options:';
const props = (_b = (0, objects_1.cartesian)((_a = schema === null || schema === void 0 ? void 0 : schema.props) !== null && _a !== void 0 ? _a : exports.defaultProps, exports.ops, (s1, s2) => s1.concat(s2))) !== null && _b !== void 0 ? _b : [];
const fuzzy = ['~', '*~'];
let rels = (0, objects_1.cartesian)(schema === null || schema === void 0 ? void 0 : schema.rels, exports.ops, (s1, s2) => s1.concat(s2));
return (yargs) => {
return yargs.options({
match: {
group,
description: 'Query filter type',
default: 'all',
requiresArg: true,
choices: ['all', 'none', 'any'],
},
property: {
group,
alias: 'prop',
description: 'Property to query',
completions: [...props, ...fuzzy],
requiresArg: true,
multiple: true,
},
tag: {
group,
description: 'Tag to query',
requiresArg: true,
completions: fuzzy,
multiple: true,
},
rel: {
group,
description: 'Tag to query',
requiresArg: true,
completions: rels,
multiple: true,
},
});
};
}
exports.withQueryOptions = withQueryOptions;
function expressionToTerm(type, expr) {
let i;
let key, op = '';
let value;
let parsed = false;
if (!parsed) {
i = expr.indexOf('>=');
if (i > 0) {
parsed = true;
op = '>=';
key = expr.substring(0, i);
value = expr.substring(i + 2);
}
}
if (!parsed) {
i = expr.indexOf('!=');
if (i > 0) {
parsed = true;
op = '!=';
key = expr.substring(0, i);
value = expr.substring(i + 2);
}
}
if (!parsed) {
i = expr.indexOf('=');
if (i > 0) {
parsed = true;
op = '=';
key = expr.substring(0, i);
value = expr.substring(i + 1);
}
}
if (!parsed) {
i = expr.indexOf('>');
if (i > 0) {
parsed = true;
op = '>';
key = expr.substring(0, i);
value = expr.substring(i + 1);
}
}
if (!parsed) {
i = expr.indexOf('<');
if (i > 0) {
parsed = true;
op = '<';
key = expr.substring(0, i);
value = expr.substring(i + 1);
}
}
if (!parsed) {
i = expr.indexOf('~');
if (i > 0) {
parsed = true;
op = '~';
key = expr.substring(0, i);
value = expr.substring(i + 1);
}
}
if (!parsed) {
i = expr.indexOf('<=');
if (i > 0) {
parsed = true;
op = '<=';
key = expr.substring(0, i);
value = expr.substring(i + 2);
}
}
if (!parsed) {
if (expr.endsWith('!')) {
parsed = true;
op = '!exists';
key = expr.substring(0, expr.length - 1);
}
}
if (!parsed) {
if (expr.startsWith('~')) {
parsed = true;
op = '~';
key = '*';
value = expr.substring(1);
}
}
if (!parsed) {
parsed = true;
op = 'exists';
key = expr.substring(0, expr.length);
}
if (!parsed) {
throw new Error(`Cannot understand the expression --${type} ${expr}`);
}
let asNumber = parseFloat(value);
if (!isNaN(asNumber)) {
// in JS the string 11asd would parse as the number 11
if ('' + asNumber == value) {
value = asNumber;
}
}
if (value === 'true') {
value = true;
}
if (value === 'false') {
value = false;
}
return {
op: op,
[type]: key,
value: value,
};
}
exports.expressionToTerm = expressionToTerm;
//# sourceMappingURL=query.js.map