-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First stable release almost ready #68
Changes from 4 commits
92fed6f
37e1fa5
29f4c56
2a926df
b0ca3e0
8063186
eadbf11
a230151
3184632
027a440
5e59282
52f63f9
4972edd
cecc260
30e8570
0b265ce
3e89f8c
0e4027d
ca49110
1e06a18
4041eec
f830286
8b8a4d6
a7096c6
499d11d
308fd67
57853e2
7824871
b9a0d09
df26d81
707c9cc
7e1bb53
7b9eef3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,8 +96,4 @@ tags | |
.vars | ||
output/ | ||
|
||
demo/log | ||
|
||
tests.log | ||
|
||
.DS_Store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
from .sftp import sftp | ||
from .write import write | ||
|
||
|
||
__all__ = ( | ||
"sftp", | ||
"write", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import difflib | ||
import os | ||
|
||
from brigade.core.task import Result | ||
|
||
|
||
def read_file(file): | ||
if not os.path.exists(file): | ||
return [] | ||
with open(file, "r") as f: | ||
return f.read().splitlines() | ||
|
||
|
||
def generate_diff(filename, content, append): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should If we decide to allow multiple tasks within the same file we might want to collect all of the tasks dynamically, then we could just exclude _read_file and _generate_diff. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you are right, will mark them as private. |
||
original = read_file(filename) | ||
if append: | ||
c = list(original) | ||
c.extend(content.splitlines()) | ||
content = c | ||
else: | ||
content = content.splitlines() | ||
|
||
diff = difflib.unified_diff(original, content, fromfile=filename, tofile="new") | ||
|
||
return "\n".join(diff) | ||
|
||
|
||
def write(task, filename, content, append=False): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Changing to |
||
""" | ||
Write contents to a file (locally) | ||
|
||
Arguments: | ||
filename (``str``): file you want to write into | ||
conteint (``str``): content you want to write | ||
append (``bool``): whether you want to replace the contents or append to it | ||
|
||
Returns: | ||
* changed (``bool``): | ||
* diff (``str``): unified diff | ||
""" | ||
diff = generate_diff(filename, content, append) | ||
|
||
if not task.dry_run: | ||
mode = "a+" if append else "w+" | ||
with open(filename, mode=mode) as f: | ||
f.write(content) | ||
|
||
return Result(host=task.host, diff=diff, changed=bool(diff)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, we shouldn't enable this by default. People can enable it if they want to, but shouldn't default to enabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recognize this changes in this commit 027a440, but still defaults to logging to brigade.log (level INFO).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#71