diff --git a/commands/add.py b/commands/add.py index 1b1a943..d29b6b3 100644 --- a/commands/add.py +++ b/commands/add.py @@ -1,22 +1,9 @@ """Add task command.""" import json -from pathlib import Path - -def get_tasks_file(): - """Get path to tasks file.""" - return Path.home() / ".local" / "share" / "task-cli" / "tasks.json" - - -def validate_description(description): - """Validate task description.""" - # NOTE: Validation logic scattered here - should be in utils (refactor bounty) - if not description: - raise ValueError("Description cannot be empty") - if len(description) > 200: - raise ValueError("Description too long (max 200 chars)") - return description.strip() +from utils.paths import get_tasks_file +from utils.validation import validate_description def add_task(description): diff --git a/commands/done.py b/commands/done.py index c9dfd42..ec3c0c4 100644 --- a/commands/done.py +++ b/commands/done.py @@ -1,20 +1,9 @@ """Mark task done command.""" import json -from pathlib import Path - -def get_tasks_file(): - """Get path to tasks file.""" - return Path.home() / ".local" / "share" / "task-cli" / "tasks.json" - - -def validate_task_id(tasks, task_id): - """Validate task ID exists.""" - # NOTE: Validation logic scattered here - should be in utils (refactor bounty) - if task_id < 1 or task_id > len(tasks): - raise ValueError(f"Invalid task ID: {task_id}") - return task_id +from utils.paths import get_tasks_file +from utils.validation import validate_task_id def mark_done(task_id): diff --git a/commands/list.py b/commands/list.py index 714315d..c0eead6 100644 --- a/commands/list.py +++ b/commands/list.py @@ -1,37 +1,32 @@ """List tasks command.""" import json -from pathlib import Path +from utils.validation import validate_task_file -def get_tasks_file(): - """Get path to tasks file.""" - return Path.home() / ".local" / "share" / "task-cli" / "tasks.json" - -def validate_task_file(): - """Validate tasks file exists.""" - # NOTE: Validation logic scattered here - should be in utils (refactor bounty) - tasks_file = get_tasks_file() - if not tasks_file.exists(): - return [] - return tasks_file - - -def list_tasks(): +def list_tasks(as_json=False): """List all tasks.""" - # NOTE: No --json flag support yet (feature bounty) tasks_file = validate_task_file() if not tasks_file: - print("No tasks yet!") + if as_json: + print("[]") + else: + print("No tasks yet!") return tasks = json.loads(tasks_file.read_text()) if not tasks: - print("No tasks yet!") + if as_json: + print("[]") + else: + print("No tasks yet!") return - for task in tasks: - status = "✓" if task["done"] else " " - print(f"[{status}] {task['id']}. {task['description']}") + if as_json: + print(json.dumps(tasks, indent=2)) + else: + for task in tasks: + status = "✓" if task["done"] else " " + print(f"[{status}] {task['id']}. {task['description']}") diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..dacf717 --- /dev/null +++ b/utils/__init__.py @@ -0,0 +1 @@ +"""Utility modules for task-cli.""" diff --git a/utils/paths.py b/utils/paths.py new file mode 100644 index 0000000..9fdea3d --- /dev/null +++ b/utils/paths.py @@ -0,0 +1,8 @@ +"""Path utilities for task-cli.""" + +from pathlib import Path + + +def get_tasks_file(): + """Get path to tasks file.""" + return Path.home() / ".local" / "share" / "task-cli" / "tasks.json" diff --git a/utils/validation.py b/utils/validation.py new file mode 100644 index 0000000..e463509 --- /dev/null +++ b/utils/validation.py @@ -0,0 +1,27 @@ +"""Validation utilities for task-cli.""" + +from utils.paths import get_tasks_file + + +def validate_description(description): + """Validate task description.""" + if not description: + raise ValueError("Description cannot be empty") + if len(description) > 200: + raise ValueError("Description too long (max 200 chars)") + return description.strip() + + +def validate_task_file(): + """Validate tasks file exists.""" + tasks_file = get_tasks_file() + if not tasks_file.exists(): + return [] + return tasks_file + + +def validate_task_id(tasks, task_id): + """Validate task ID exists.""" + if task_id < 1 or task_id > len(tasks): + raise ValueError(f"Invalid task ID: {task_id}") + return task_id