Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/pyinfra/facts/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def process(self, output) -> Optional[str]:
class Sha1File(HashFileFactBase, digits=40, cmds=["sha1sum", "shasum", "sha1"]):
"""
Returns a SHA1 hash of a file. Works with both sha1sum and sha1. Returns
``None`` if the file doest not exist.
``None`` if the file does not exist.
"""


Expand Down Expand Up @@ -664,14 +664,16 @@ def process(self, output):

class FileContents(FactBase):
"""
Returns the contents of a file as a list of lines. Works with both sha1sum and sha1. Returns
``None`` if the file doest not exist.
Returns the contents of a file as a list of lines. Returns
``None`` if the file does not exist.
"""

@override
def command(self, path):
return make_formatted_string_command("cat {0}", QuoteString(path))
return make_formatted_string_command("test -e {0} && cat {0} || true", QuoteString(path))

@override
def process(self, output):
if not output:
return self.default()
return output
2 changes: 1 addition & 1 deletion tests/facts/files.FileContents/file.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": "myfile",
"command": "cat myfile",
"command": "test -e myfile && cat myfile || true",
"output": ["line1", "line2"],
"fact": ["line1", "line2"]
}
4 changes: 2 additions & 2 deletions tests/facts/files.FileContents/no_file.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"arg": ["test"],
"command": "cat test",
"output": null,
"command": "test -e test && cat test || true",
"output": [],
"fact": null
}