Skip to content

Commit 6241ea2

Browse files
authored
Change file creating and writing methods (#115)
2 parents 9ac9b86 + 332c63c commit 6241ea2

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

sshgen/generators/sshconfig.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,13 @@ def _save_config(self, templates: list[str]) -> None:
7575
condition=len(templates) > 0, message="No templates found to generate. Exiting...", exit_code=0
7676
)
7777

78-
self._create_output_file()
79-
self.output_file.write_text("\n".join(templates))
78+
FileUtils.create_file(self.output_file)
79+
FileUtils.write_text(file_path=self.output_file, data="\n".join(templates))
80+
8081
log.info("Generated SSH config file was saved to %s", self.output_file)
8182
log.debug("Skipped hosts list: %s", self._parse_skipped())
8283
log.info("Total processed hosts: %d, total skipped hosts: %d", len(templates), len(self._skipped))
8384

84-
def _create_output_file(self) -> None:
85-
self.output_file.touch(exist_ok=True)
86-
8785
def _parse_skipped(self) -> str:
8886
if len(self._skipped) > 0:
8987
return ", ".join([model.ansible_host for model in self._skipped])

sshgen/utils/file.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ def get_hosts_path(file_path: str) -> Path:
2828

2929
@staticmethod
3030
def get_output_path(file_path: str) -> Path:
31-
resolved_path = FileUtils.resolve_path(file_path)
32-
33-
if not resolved_path.exists():
34-
log.debug("Path %s is not exists, creating required directories", resolved_path)
35-
FileUtils.create_file(resolved_path)
36-
37-
return resolved_path
31+
return FileUtils.resolve_path(file_path)
3832

3933
@staticmethod
4034
def as_package_file(file_path: str) -> Path:
@@ -58,10 +52,20 @@ def is_empty(file_path: Path) -> bool:
5852

5953
@staticmethod
6054
def create_file(file_path: Path) -> None:
61-
if not file_path.is_file():
62-
try:
63-
file_path.parent.mkdir(parents=True, exist_ok=True)
64-
file_path.touch(exist_ok=True)
65-
except OSError as e:
66-
log.exception("Failed to create file or directory: %s, reason: %s", file_path, e.strerror)
67-
sys.exit(1)
55+
if not file_path.exists():
56+
log.debug("Path %s is not exists, trying to create required folders and file", file_path)
57+
58+
try:
59+
file_path.parent.mkdir(parents=True, exist_ok=True)
60+
file_path.touch(exist_ok=True)
61+
except OSError as e:
62+
log.exception("Failed to create file or directory: %s, reason: %s", file_path, e.strerror)
63+
sys.exit(1)
64+
65+
@staticmethod
66+
def write_text(file_path: Path, data: str) -> None:
67+
try:
68+
file_path.write_text(data)
69+
except OSError as e:
70+
log.exception("Failed to create file or directory: %s, reason: %s", file_path, e.strerror)
71+
sys.exit(1)

0 commit comments

Comments
 (0)