diff --git a/kernelci/cli/base_api.py b/kernelci/cli/base_api.py index 8a066b6dd2..45f35a7b68 100644 --- a/kernelci/cli/base_api.py +++ b/kernelci/cli/base_api.py @@ -12,6 +12,7 @@ import abc import json +import re import kernelci.api from .base import Command, Args, catch_http_error @@ -63,12 +64,36 @@ class AttributesCommand(APICommand): { 'name': 'attributes', 'nargs': '*', - 'help': "Attributes in name=value format", + 'help': "Attributes in 'name=value' format, where = is " + "OPERATOR and can be one of: >, <, >=, <=, =", }, ] @classmethod def _split_attributes(cls, attributes): - return dict( - tuple(attr.split('=')) for attr in attributes - ) if attributes else {} + rval = {} + if not attributes: + return rval + for attribute in attributes: + pattern = r'^([\S]+)([!=<>]+)([\S]+)$' + match = re.match(pattern, attribute) + if match: + attribute, operator, value = match.groups() + # if operator is >= then append to attribute '__gte' suffix + # and so on + switch = { + '>': '__gt', + '<': '__lt', + '>=': '__gte', + '<=': '__lte', + } + if operator in switch: + attribute += switch[operator] + elif operator != '=': + # raise error + raise ValueError(f"Invalid operator {operator}") + rval[attribute] = value + else: + raise ValueError(f"Invalid attribute {attribute}") + + return rval