diff options
author | 2022-07-12 13:07:28 +0000 | |
---|---|---|
committer | 2022-07-12 13:07:28 +0000 | |
commit | 1e3d27821b5be4a018814b0edfee7ffce5499686 (patch) | |
tree | fd3022255fa0efe2eace763d6b2eeb20e36a0a34 /scripts/jsonmodify.py | |
parent | 348b777b86a07945004148e1beb84fdc9a892770 (diff) | |
parent | d887e242604d99cc87047ca524a3dc73d7f439a3 (diff) |
Merge changes Ibcf908d9,I786ab152
* changes:
Cleaning up the version sed rule
Adding support to conditionally replace a value
Diffstat (limited to 'scripts/jsonmodify.py')
-rwxr-xr-x | scripts/jsonmodify.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/scripts/jsonmodify.py b/scripts/jsonmodify.py index 5132aa8f6..8bd8d4556 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) @@ -99,6 +106,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', |