Skip to content

Commit

Permalink
icm rm: ask for confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
Obijuan committed Jun 19, 2024
1 parent 8467cd8 commit 318ef41
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"type": "debugpy",
"request": "launch",
"program": "icm-run.py",
"args": ["rm","iceK-0.1.3"],
"args": ["rm","iceK"],
//"console": "internalConsole",
"console": "integratedTerminal",
"justMyCode": true,
Expand Down
59 changes: 51 additions & 8 deletions icm/commands/cmd_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,67 @@
import shutil
from icm.commons import commons

import click

def main(name: str) -> None:

def main(coltag: str) -> None:
"""ENTRY POINT: Remove collections
* name: Name of the collection to remove
* coltag: Name+version of the collection to remove
(Ex. iceK-0.1.4)
"""

# -- Get context information
# ctx = commons.Context()
folders = commons.Folders()
collection = commons.Collection(folders)
print()

# -- Build the Path to the collection
abs_collection = folders.collections / name
abs_collection = folders.collections / coltag

# -- Check if the collection exists, as it was typed bye the user
if abs_collection.exists():
# -- Remove it!
shutil.rmtree(abs_collection)
return

# -- Manage other cases
# -- Case 1: Remove the first collection that has the same name
# -- Ignore the version

# -- Parse the collection name: Get the name and version
parsed_coltag = collection.parse_coltag2(coltag)
name = parsed_coltag['name']
version = parsed_coltag['version']

# -- If there is name and version: The collection does not exists
if name and version:
print(f"rm: cannot remove {coltag}: No such collection")
return

# -- List all the collections that starts with "name-"
list_col = [
file.name for file in folders.collections.glob(f"{name}-*") if file.is_dir()
]

# -- No collection has a name that starts with "<name>-"
if not list_col:
print(f"rm: cannot remove {coltag}: No such collection")
return

# -- There are collection with that name
# -- TODO: Remove ALL the collections, not just the first

# -- Get the first collection name
coltag = list_col[0]

# -- Build the full path to the collection
abs_collection = folders.collections / coltag

# -- Check if the collection exists, as it was type byte the user
if not abs_collection.exists():
print(f"rm: cannot remove {name}: No such collection")
# -- Ask for confirmation!
if click.confirm(f"{coltag}: Remove?"):
# -- Remove the collection!
shutil.rmtree(abs_collection)
return

# -- Remove the collection folder, along with all its files
shutil.rmtree(abs_collection)
print("Aborted.")
22 changes: 22 additions & 0 deletions icm/commons/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,25 @@ def parse_coltag(self, coltag: str) -> dict: # or | None:

# -- No match. Incorrect collection tag
return None

def parse_coltag2(self, coltag: str) -> dict: # or | None:
"""Parse a collection name with optional tag version
Ex: "iceK-0.1.4"
Return:
None: There is an error
result: Dictionary with the calculated values
result['name']: Collection name
result['version']: Collection version (it could be "")
"""
# -- Pattern for parsing strings in the format <name>[@<version>]
pattern = r"^(?P<name>[a-zA-Z0-9]+)(-(?P<version>\d+\.\d+(\.\d+)?))?$"

# Busca coincidencias en la cadena de entrada
match = re.match(pattern, coltag)

# -- TODO: Raise an exception!
if match:
return match.groupdict()

# -- No match. Incorrect collection tag
return None

0 comments on commit 318ef41

Please sign in to comment.