Skip to content

Commit

Permalink
Merge pull request #1362 from dmach/meta-attribute-add-deduplicate
Browse files Browse the repository at this point in the history
Change '--add' option in 'meta attribute' command to skip duplicate values
  • Loading branch information
dmach authored Jul 18, 2023
2 parents 2d92772 + ac23ed0 commit 6f58d4f
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,7 +1741,7 @@ def do_token(self, subcmd, opts, *args):
@cmdln.option('-s', '--set', metavar='ATTRIBUTE_VALUES',
help='set attribute values')
@cmdln.option('--add', metavar='ATTRIBUTE_VALUES',
help='add to the existing attribute values')
help='add to the existing attribute values, skip duplicates')
@cmdln.option('--delete', action='store_true',
help='delete a pattern or attribute')
def do_meta(self, subcmd, opts, *args):
Expand Down Expand Up @@ -1963,26 +1963,36 @@ def do_meta(self, subcmd, opts, *args):
if len(aname) != 2:
raise oscerr.WrongOptions('Given attribute is not in "NAMESPACE:NAME" style')

values = ''
values = []

if opts.add:
# read the existing values from server
root = _private.api.get(apiurl, attributepath)
nodes = _private.api.find_nodes(root, "attributes", "attribute", {"namespace": aname[0], "name": aname[1]}, "value")
for node in nodes:
# append the existing values
value = _private.api.xml_escape(node.text)
values += f"<value>{value}</value>"
values.append(node.text)

# pretend we're setting values in order to append the values we have specified on the command-line,
# because OBS API doesn't support extending the value list directly
opts.set = opts.add

if opts.set:
for i in opts.set.split(','):
values += '<value>%s</value>' % _private.api.xml_escape(i)
# append the new values
# we skip duplicates during --add
if opts.add and i in values:
continue
values.append(i)

values_str = ""
for value in values:
value = _private.api.xml_escape(value)
values_str += f"<value>{value}</value>"

d = '<attributes><attribute namespace=\'%s\' name=\'%s\' >%s</attribute></attributes>' % (aname[0], aname[1], values)
ns = _private.api.xml_escape(aname[0])
name = _private.api.xml_escape(aname[1])
d = f"<attributes><attribute namespace='{ns}' name='{name}' >{values_str}</attribute></attributes>"
url = makeurl(apiurl, attributepath)
for data in streamfile(url, http_POST, data=d):
sys.stdout.buffer.write(data)
Expand Down

0 comments on commit 6f58d4f

Please sign in to comment.