Skip to content

Commit

Permalink
if the alias already exist don't write it again into the .bashrc file
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLoLf committed Jun 24, 2024
1 parent 3e71545 commit 8aacc53
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ def create_alias(alias_name :str , app_id: str):
Returns:
None
"""
home_dir = os.path.expanduser('~') # Get the home directory
bashrc_path = os.path.join(home_dir, '.bashrc') # Path to .bashrc
command = f'alias {alias_name}="flatpak run {app_id}"\n' # Alias command
with open(bashrc_path, 'a') as bashrc: # Open .bashrc in append mode
bashrc.write(command) # Append the alias command to .bashrc
home_dir: str = os.path.expanduser('~') # Get the home directory
bashrc_path: str = os.path.join(home_dir, '.bashrc') # Path to .bashrc
command: str = f'alias {alias_name}="flatpak run {app_id}"\n' # Alias command
with open(bashrc_path, 'r') as bashrc: # Open .bashrc in read mode
existing_aliases: list[str] = bashrc.readlines() # Read all existing aliases

if command in existing_aliases: # Check if the alias command already exists
print(f"Alias '{alias_name}' for '{app_id}' already exists in .bashrc.")
return

with open(bashrc_path, 'a') as bashrc: # Open .bashrc in append mode
bashrc.write(command) # Append the alias command to .bashrc
print(f"Alias '{alias_name}' for '{app_id}' added to .bashrc.")

def main():
Expand Down

0 comments on commit 8aacc53

Please sign in to comment.