summaryrefslogtreecommitdiff
path: root/scripts/jsonmodify.py
diff options
context:
space:
mode:
author Alexei Nicoara <ancr@google.com> 2022-07-11 12:38:50 +0100
committer Alexei Nicoara <ancr@google.com> 2022-07-12 11:53:07 +0100
commit7d69b1d8e664e15bb891149384a18a5c715cc0f3 (patch)
treebf6f0b652987ae6254d034ededab25fbfc3e2c65 /scripts/jsonmodify.py
parent2971a94e4c6128bb147f38c25f82b3e6159a516f (diff)
Adding support to conditionally replace a value
Bug: 231691643 Test: presubmit Change-Id: I786ab152a94126ebc8a9d7dea0bb68e07d789a0b
Diffstat (limited to 'scripts/jsonmodify.py')
-rwxr-xr-xscripts/jsonmodify.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/scripts/jsonmodify.py b/scripts/jsonmodify.py
index ba1109e7a..96ff8cacd 100755
--- a/scripts/jsonmodify.py
+++ b/scripts/jsonmodify.py
@@ -59,6 +59,13 @@ class Replace(str):
cur[key] = val
+class ReplaceIfEqual(str):
+ def apply(self, obj, old_val, new_val):
+ cur, key = follow_path(obj, self)
+ if cur and cur[key] == int(old_val):
+ cur[key] = new_val
+
+
class Remove(str):
def apply(self, obj):
cur, key = follow_path(obj, self)
@@ -91,6 +98,11 @@ def main():
help='replace value of the key specified by path. If path doesn\'t exist, no op.',
metavar=('path', 'value'),
nargs=2, dest='patch', action='append')
+ parser.add_argument("-se", "--replace-if-equal", type=ReplaceIfEqual,
+ help='replace value of the key specified by path to new_value if it\'s equal to old_value.' +
+ 'If path doesn\'t exist or the value is not equal to old_value, no op.',
+ metavar=('path', 'old_value', 'new_value'),
+ nargs=3, dest='patch', action='append')
parser.add_argument("-r", "--remove", type=Remove,
help='remove the key specified by path. If path doesn\'t exist, no op.',
metavar='path',