"""
Command management functionality for the ModularOllamaAgent.
This module provides functions for managing the command allowlist,
including listing, adding, and removing commands.
"""
from typing import List, Set
from .utils import print_info, print_error, print_success, print_warning
def list_allowed_commands(command_allowlist: Set[str]) -> List[str]:
"""
List all currently allowed commands.
Args:
command_allowlist: Set of currently allowed commands
Returns:
List[str]: List of allowed commands
"""
# Get the current allowlist
all_commands = sorted(list(command_allowlist))
# Print the commands
print_info(f"Currently allowed commands ({len(all_commands)}):")
# Format into categories for better readability
categories = {
"File Operations": ["ls", "cat", "find", "grep", "head", "tail"],
"Directory Operations": ["pwd", "cd", "mkdir", "rmdir"],
"System Information": ["uname", "hostname", "whoami", "ps", "top", "df", "du", "free"],
"Network Utilities": ["ping", "curl", "wget", "netstat", "dig", "nslookup"],
"Text Processing": ["echo", "printf", "sed", "awk", "cut", "sort", "uniq", "wc"],
"File Manipulation": ["cp", "mv", "rm", "touch", "chmod", "chown", "chgrp", "ln"],
"Other": []
}
# Categorize commands
for cmd in all_commands:
categorized = False
for category, commands in categories.items():
if cmd in commands:
categorized = True
break
if not categorized:
categories["Other"].append(cmd)
# Print by category
for category, commands in categories.items():
if commands:
print(f"\n{category}:")
for i, cmd in enumerate(sorted(commands)):
print(f" {cmd}", end="\t")
# New line every 5 commands for readability
if (i + 1) % 5 == 0:
print()
print() # Ensure we end with a newline
return all_commands
def add_allowed_command(command: str, command_allowlist: Set[str]) -> bool:
"""
Add a command to the allowed list.
Args:
command: Command to add
command_allowlist: Set of currently allowed commands
Returns:
bool: True if added successfully
"""
if not command or not isinstance(command, str):
print_error("Invalid command name")
return False
# Clean up the command name
command = command.strip().lower()
# Check if already in allowlist
if command in command_allowlist:
print_info(f"Command '{command}' is already allowed")
return True
# Add to custom allowlist
command_allowlist.add(command)
print_success(f"Command '{command}' added to allowed list")
# Show warning for potentially dangerous commands
dangerous_commands = {"rm", "mv", "chmod", "chown", "dd", "mkfs", "iptables"}
if command in dangerous_commands:
print_warning(f"'{command}' is a potentially dangerous command. Use with caution!")
return True
def remove_allowed_command(command: str, command_allowlist: Set[str]) -> bool:
"""
Remove a command from the allowed list.
Args:
command: Command to remove
command_allowlist: Set of currently allowed commands
Returns:
bool: True if removed successfully
"""
if not command or not isinstance(command, str):
print_error("Invalid command name")
return False
# Clean up the command name
command = command.strip().lower()
# Check if in allowlist
if command not in command_allowlist:
print_error(f"Command '{command}' is not in the allowed list")
return False
# Remove from custom allowlist
command_allowlist.remove(command)
print_success(f"Command '{command}' removed from allowed list")
return True