Skip to content

Commit ade5790

Browse files
committed
modified
1 parent f152d96 commit ade5790

File tree

4 files changed

+124
-27
lines changed

4 files changed

+124
-27
lines changed

Main.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
from file_system import (
2-
create_folder, create_file, list_contents, read_file, delete_file,
3-
delete_folder, change_directory, go_back, print_working_directory
4-
)
1+
from file_system import *
52

63
def display_help():
7-
"""Displays available commands."""
84
commands = """
95
Available commands:
106
- mkdir <folder_name> : Create a folder
@@ -13,8 +9,9 @@ def display_help():
139
- read <file_name> : Read a file
1410
- rm <file_name> : Delete a file
1511
- rmdir <folder_name> : Delete a folder
12+
- mv <old_name> <new_name> : Rename a file or folder
1613
- cd <folder_name> : Change directory
17-
- back : Move to the parent directory
14+
- cd .. : Move to the parent directory
1815
- pwd : Print the current working directory
1916
- help : Show this help menu
2017
- exit : Exit the program
@@ -23,48 +20,60 @@ def display_help():
2320

2421

2522
def main():
26-
"""Command interpreter for the file system."""
27-
print("Welcome to the Python Terminal Virtual File System!")
28-
display_help()
29-
3023
while True:
31-
command = input("\n>>> ").strip()
24+
command = input("Admin@Python-Terminal ~ % ").strip()
3225
if not command:
3326
continue
3427

35-
parts = command.split(" ", 1)
28+
parts = command.split(" ", 2)
3629
cmd = parts[0].lower()
37-
arg = parts[1] if len(parts) > 1 else None
30+
arg1 = parts[1] if len(parts) > 1 else None
31+
arg2 = parts[2] if len(parts) > 2 else None
3832

39-
if cmd == "mkdir" and arg:
40-
print(create_folder(arg))
41-
elif cmd == "touch" and arg:
42-
print(create_file(arg))
33+
if cmd == "mkdir" and arg1:
34+
print(create_folder(arg1))
35+
elif cmd == "touch" and arg1:
36+
print(create_file(arg1))
4337
elif cmd == "ls":
4438
contents = list_contents()
4539
if isinstance(contents, list):
4640
print("\n".join(contents) if contents else "Directory is empty.")
4741
else:
4842
print(contents)
49-
elif cmd == "read" and arg:
50-
print(read_file(arg))
51-
elif cmd == "rm" and arg:
52-
print(delete_file(arg))
53-
elif cmd == "rmdir" and arg:
54-
print(delete_folder(arg))
55-
elif cmd == "cd" and arg:
56-
print(change_directory(arg))
43+
elif cmd == "read" and arg1:
44+
print(read_file(arg1))
45+
elif cmd == "rm" and arg1:
46+
print(delete_file(arg1))
47+
elif cmd == "rmdir" and arg1:
48+
print(delete_folder(arg1))
49+
elif cmd == "mv" and arg1 and arg2:
50+
print(rename_item(arg1, arg2))
51+
elif cmd == "cd" and arg1:
52+
print(change_directory(arg1))
53+
elif cmd == "cd":
54+
print("zsh: cd must have 1 argument")
5755
elif cmd == "back":
5856
print(go_back())
5957
elif cmd == "pwd":
6058
print(print_working_directory())
6159
elif cmd == "help":
6260
display_help()
6361
elif cmd == "exit":
64-
print("Exiting the program. Goodbye!")
62+
print("[process Completed]")
6563
break
64+
elif cmd == "alias" and arg1 and arg2:
65+
alias(arg1, arg2)
66+
elif cmd:
67+
if arg1:
68+
arg2 = ""
69+
search_alias(cmd, arg1, arg2)
70+
else:
71+
arg1 = ""
72+
arg2 = ""
73+
search_alias(cmd, arg1, arg2)
6674
else:
67-
print("Unknown command. Type 'help' for a list of commands.")
75+
if arg1 and arg2:
76+
search_alias(arg1, arg2)
6877

6978
if __name__ == "__main__":
7079
main()

SECURITY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
| Version | Supported |
66
| ------- | ------------------ |
77
| 1.0.0 | :white_check_mark: |
8+
| 2.3.0 | :white_check_mark: |
89

910

1011
## Reporting a Vulnerability
4.98 KB
Binary file not shown.

file_system.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,60 @@
11
import os
22

3+
4+
def display_help():
5+
commands = """
6+
Available commands:
7+
- mkdir <folder_name> : Create a folder
8+
- touch <file_name> : Create a file
9+
- ls : List contents of the current directory
10+
- read <file_name> : Read a file
11+
- rm <file_name> : Delete a file
12+
- rmdir <folder_name> : Delete a folder
13+
- mv <old_name> <new_name> : Rename a file or folder
14+
- cd <folder_name> : Change directory
15+
- cd .. : Move to the parent directory
16+
- pwd : Print the current working directory
17+
- help : Show this help menu
18+
- exit : Exit the program
19+
"""
20+
print(commands)
21+
22+
aliases = {}
23+
def find_alias(cmd, arg1, arg2):
24+
cmd = ""
25+
if cmd == "mkdir" and arg1:
26+
print(create_folder(arg1))
27+
elif cmd == "touch" and arg1:
28+
print(create_file(arg1))
29+
elif cmd == "ls":
30+
contents = list_contents()
31+
if isinstance(contents, list):
32+
print("\n".join(contents) if contents else "Directory is empty.")
33+
else:
34+
print(contents)
35+
elif cmd == "read" and arg1:
36+
print(read_file(arg1))
37+
elif cmd == "rm" and arg1:
38+
print(delete_file(arg1))
39+
elif cmd == "rmdir" and arg1:
40+
print(delete_folder(arg1))
41+
elif cmd == "mv" and arg1 and arg2:
42+
print(rename_item(arg1, arg2))
43+
elif cmd == "cd" and arg1:
44+
print(change_directory(arg1))
45+
elif cmd == "cd":
46+
print("zsh: cd must have 1 argument")
47+
elif cmd == "back":
48+
print(go_back())
49+
elif cmd == "pwd":
50+
print(print_working_directory())
51+
elif cmd == "help":
52+
display_help()
53+
elif cmd == "alias" and arg1 and arg2:
54+
alias(arg1, arg2)
55+
else:
56+
print(f"zsh: command not found: {cmd}")
57+
358
# Base directory for the virtual file system
459
BASE_DIRECTORY = os.path.join(os.getcwd(), "virtual_fs")
560

@@ -79,6 +134,20 @@ def delete_folder(folder_name):
79134
return f"Folder '{folder_name}' does not exist."
80135

81136

137+
def rename_item(old_name, new_name):
138+
"""Renames a file or folder in the current directory."""
139+
old_path = os.path.join(current_directory, old_name)
140+
new_path = os.path.join(current_directory, new_name)
141+
if os.path.exists(old_path):
142+
try:
143+
os.rename(old_path, new_path)
144+
return f"'{old_name}' has been renamed to '{new_name}'."
145+
except Exception as e:
146+
return f"Error renaming '{old_name}': {e}"
147+
else:
148+
return f"'{old_name}' does not exist."
149+
150+
82151
def change_directory(new_directory):
83152
"""Changes the current directory."""
84153
global current_directory
@@ -103,3 +172,21 @@ def go_back():
103172
def print_working_directory():
104173
"""Returns the current working directory, relative to BASE_DIRECTORY."""
105174
return "/" + os.path.relpath(current_directory, BASE_DIRECTORY).replace("\\", "/")
175+
176+
def alias(alias, command):
177+
aliases[alias] = command
178+
print(f"Alias added: {alias} -> {command}")
179+
180+
def search_alias(alias, arg1, arg2):
181+
if alias in aliases:
182+
if arg1 and arg2:
183+
find_alias(alias, arg1, arg2)
184+
elif arg1:
185+
arg2 = ""
186+
find_alias(alias, arg1, arg2)
187+
else:
188+
arg1 = ""
189+
arg2 = ""
190+
find_alias(alias, arg1, arg2)
191+
else:
192+
print(f"zsh: command not found: {alias}")

0 commit comments

Comments
 (0)