Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 85e2f17

Browse files
committed
feat: add isLesserThan and isGreaterThan filters
Usage: "1.0.0" is getstackhead.stackhead.isLesserThan("1.2.0") "2.0.0" is getstackhead.stackhead.isGreaterThan("1.2.0")
1 parent 153f9e9 commit 85e2f17

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

ansible/plugins/test/version.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,50 @@ def within_major(currentVersion, targetVersion):
4242
return int(major) == int(majorTarget)
4343

4444

45+
def is_gt(currentVersion, targetVersion):
46+
matchCurrent = SEMVER_RE.match(currentVersion)
47+
if not matchCurrent:
48+
raise ValueError("invalid semantic version '%s'" % currentVersion)
49+
matchTarget = SEMVER_RE.match(targetVersion)
50+
if not matchTarget:
51+
raise ValueError("invalid semantic version '%s'" % targetVersion)
52+
53+
(major, minor, patch, _, _) = matchCurrent.group(1, 2, 3, 4, 5)
54+
(majorTarget, minorTarget, patchTarget, _, _) = matchTarget.group(1, 2, 3, 4, 5)
55+
56+
return (
57+
int(major) < int(majorTarget)
58+
or int(minor) < int(minorTarget)
59+
or int(patch) < int(patchTarget)
60+
)
61+
62+
63+
def is_lt(currentVersion, targetVersion):
64+
matchCurrent = SEMVER_RE.match(currentVersion)
65+
if not matchCurrent:
66+
raise ValueError("invalid semantic version '%s'" % currentVersion)
67+
matchTarget = SEMVER_RE.match(targetVersion)
68+
if not matchTarget:
69+
raise ValueError("invalid semantic version '%s'" % targetVersion)
70+
71+
(major, minor, patch, _, _) = matchCurrent.group(1, 2, 3, 4, 5)
72+
(majorTarget, minorTarget, patchTarget, _, _) = matchTarget.group(1, 2, 3, 4, 5)
73+
74+
return (
75+
int(major) > int(majorTarget)
76+
or int(minor) > int(minorTarget)
77+
or int(patch) > int(patchTarget)
78+
)
79+
80+
4581
class TestModule(object):
4682
"""
4783
Jinja2 tests for version compare
4884
"""
4985

5086
def tests(self):
51-
return {"withinMajorVersion": within_major}
87+
return {
88+
"withinMajorVersion": within_major,
89+
"isLesserThan": is_lt,
90+
"isGreaterThan": is_gt,
91+
}

0 commit comments

Comments
 (0)