Add new line if missing

This commit is contained in:
twangboy 2016-07-01 12:02:45 -06:00
parent 0b7821c8db
commit 3e2fe12e5e
2 changed files with 19 additions and 3 deletions

View file

@ -2456,6 +2456,23 @@ def append(path, *args, **kwargs):
else:
args = [kwargs['args']]
# Make sure we have a newline at the end of the file. Do this in binary
# mode so SEEK_END with nonzero offset will work.
with salt.utils.fopen(path, 'rb+') as ofile:
linesep = salt.utils.to_bytes(os.linesep)
try:
ofile.seek(-len(linesep), os.SEEK_END)
except IOError as exc:
if exc.errno in (errno.EINVAL, errno.ESPIPE):
# Empty file, simply append lines at the beginning of the file
pass
else:
raise
else:
if ofile.read(len(linesep)) != linesep:
ofile.seek(0, os.SEEK_END)
ofile.write(linesep)
# Append lines in text mode
with salt.utils.fopen(path, 'a') as ofile:
for line in args:

View file

@ -3536,7 +3536,7 @@ def append(name,
continue
for line_item in chunk.splitlines():
append_lines.append('{0}\n'.format(line_item))
append_lines.append('{0}'.format(line_item))
except TypeError:
return _error(ret, 'No text found to append. Nothing appended')
@ -3552,7 +3552,7 @@ def append(name,
else:
# Changes happened, add them
ret['changes']['diff'] = (
''.join(difflib.unified_diff(slines, nlines))
'\n'.join(difflib.unified_diff(slines, nlines))
)
else:
ret['comment'] = 'File {0} is in correct state'.format(name)
@ -3560,7 +3560,6 @@ def append(name,
return ret
if len(append_lines):
append_lines = [item.strip() for item in append_lines]
__salt__['file.append'](name, args=append_lines)
ret['comment'] = 'Appended {0} lines'.format(len(append_lines))
else: