Skip to content

Commit

Permalink
- ensure to set the permission first before write the content
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielcocenza committed Oct 16, 2024
1 parent 05bdef0 commit dd904dc
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,23 @@ def validate_exporter_configs(self) -> Tuple[bool, str]:


def write_to_file(path: Path, content: str, mode: Optional[int] = None) -> bool:
"""Write to file with provided content."""
success = True
"""Write to file with provided content.
It's important to first set the permissions to then write the content because it might have
sensitive information like password.
"""
path.touch()
if mode is not None:
os.chmod(path, mode)
try:
with open(path, "w", encoding="utf-8") as file:
file.write(content)

if mode:
os.chmod(path, mode)
except (NotADirectoryError, PermissionError, OSError) as err:
except (NotADirectoryError, PermissionError) as err:
logger.error(err)
logger.info("Writing file to %s - Failed.", path)
success = False
else:
logger.info("Writing file to %s - Done.", path)
return success
return False

logger.info("Writing file to %s - Done.", path)
return True


def remove_file(path: Path) -> bool:
Expand Down

0 comments on commit dd904dc

Please sign in to comment.