mirror of
https://github.com/valitydev/salt.git
synced 2024-11-09 01:36:48 +00:00
Refactor insert/ensure when after/before is known
This commit is contained in:
parent
c2d9d8f8ea
commit
e0d05e5347
@ -1936,25 +1936,19 @@ def line(path, content=None, match=None, mode=None, location=None,
|
|||||||
_assert_occurrence(body, before, 'before')
|
_assert_occurrence(body, before, 'before')
|
||||||
_assert_occurrence(body, after, 'after')
|
_assert_occurrence(body, after, 'after')
|
||||||
|
|
||||||
a_idx = b_idx = -1
|
is_there = False
|
||||||
idx = 0
|
out = []
|
||||||
body = body.split(os.linesep)
|
body = body.split(os.linesep)
|
||||||
for _line in body:
|
for idx, line in enumerate(body):
|
||||||
idx += 1
|
out.append(line)
|
||||||
if _line.find(before) > -1 and b_idx < 0:
|
if line.find(content) > -1:
|
||||||
b_idx = idx
|
is_there = True
|
||||||
if _line.find(after) > -1 and a_idx < 0:
|
if not is_there:
|
||||||
a_idx = idx
|
if idx < (len(body) - 1) and line.find(after) > -1 and body[idx + 1].find(before) > -1:
|
||||||
|
out.append(content)
|
||||||
# Add
|
elif line.find(after):
|
||||||
if not b_idx - a_idx - 1:
|
|
||||||
body = body[:a_idx] + [content] + body[b_idx - 1:]
|
|
||||||
elif b_idx - a_idx - 1 == 1:
|
|
||||||
if _starts_till(body[a_idx:b_idx - 1][0], content) > -1:
|
|
||||||
body[a_idx] = _get_line_indent(body[a_idx - 1], content, indent)
|
|
||||||
else:
|
|
||||||
raise CommandExecutionError('Found more than one line between boundaries "before" and "after".')
|
raise CommandExecutionError('Found more than one line between boundaries "before" and "after".')
|
||||||
body = os.linesep.join(body)
|
body = os.linesep.join(out)
|
||||||
|
|
||||||
elif before and not after:
|
elif before and not after:
|
||||||
_assert_occurrence(body, before, 'before')
|
_assert_occurrence(body, before, 'before')
|
||||||
|
Loading…
Reference in New Issue
Block a user