mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Add new line if missing
This commit is contained in:
parent
0b7821c8db
commit
3e2fe12e5e
2 changed files with 19 additions and 3 deletions
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Add table
Reference in a new issue