feat: Complete Issue #39 - Database CLI Reorganization with Comprehensive Legacy Compatibility System

## Database Command Reorganization
- Add new db-prefixed commands: db-query, db-schema, db-delete, db-status
- Maintain backward compatibility with deprecation warnings for query/schema commands
- Implement lazy database initialization to reduce CLI coupling
- Add command-specific --database options for flexibility

## Legacy Compatibility Framework
- Create comprehensive legacy compatibility system in markitect/legacy_compat.py
- Support versioned legacy switches (--legacy-v39-pre) for smooth transitions
- Implement git commit binding for version tracking (Issue #39: v39-pre → 3168de4)
- Add environment-based legacy mode detection for test environments
- Create graduated deprecation warning system (DEPRECATED → LEGACY → SUNSET)

## Legacy Agent System
- Implement intelligent legacy lifecycle management agent
- Add 8 CLI commands for legacy interface management (status, analyze, migrate, cleanup, etc.)
- Create automated maintenance with usage analytics and data-driven decisions
- Provide comprehensive safety features with backup and rollback capabilities

## Test Architecture Enhancement
- Add 18 comprehensive tests for Issue #39 functionality (16 passing, 2 skipped by design)
- Configure pytest.ini with MARKITECT_LEGACY_MODE=39-pre for automatic legacy support
- Update test count to 466 total tests across 7 architectural layers
- Identify 5 legacy interface tests for future recreation without legacy dependencies

## Documentation & Roadmap Updates
- Update NEXT.md with completed Issues #39 and #40
- Document failing tests requiring recreation with pure db- commands
- Add comprehensive legacy agent documentation
- Update development priorities and capability descriptions

## Architecture Achievements
- Simplified CLI architecture with reduced coupling between commands and global state
- Created reusable legacy compatibility framework for future breaking changes
- Established systematic approach to interface deprecation and migration
- Maintained 461/466 tests passing (5 legacy interface tests flagged for recreation)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-30 17:28:39 +02:00
parent 3168de49ac
commit a367628cab
16 changed files with 5745 additions and 27 deletions

View File

@@ -26,6 +26,16 @@ from tabulate import tabulate
import builtins
from .database import DatabaseManager
from .legacy_compat import LegacyMode, emit_deprecation_warning, legacy_switch_option
# Import legacy system components for advanced management
try:
from .legacy import (
LegacyRegistry, LegacyAgent, LegacyStatus, AgentConfig
)
LEGACY_SYSTEM_AVAILABLE = True
except ImportError:
LEGACY_SYSTEM_AVAILABLE = False
def detect_execution_mode():
@@ -542,15 +552,78 @@ def query(config, sql, format):
"""
Execute SQL query against the database.
DEPRECATED: Use 'db-query' instead. This command will be removed in a future version.
Execute read-only SQL queries to explore and analyze document metadata.
Only SELECT and WITH statements are allowed for security.
SQL: SQL query to execute (SELECT statements only)
Examples:
markitect query "SELECT filename, created_at FROM markdown_files"
markitect query "SELECT COUNT(*) as total FROM markdown_files" --format json
markitect query "SELECT * FROM markdown_files WHERE filename LIKE '%.md'" --format yaml
markitect db-query "SELECT filename, created_at FROM markdown_files"
markitect db-query "SELECT COUNT(*) as total FROM markdown_files" --format json
markitect db-query "SELECT * FROM markdown_files WHERE filename LIKE '%.md'" --format yaml
"""
# Show deprecation warning (unless in legacy mode)
if not LegacyMode.should_suppress_warnings():
emit_deprecation_warning(
"The 'query' command is deprecated. Please use 'db-query' instead. "
"This command will be removed in a future version."
)
try:
if config['verbose']:
click.echo(f"Executing query: {sql}", err=True)
db_manager = config['db_manager']
# Execute the query
results = db_manager.execute_query(sql)
if not results:
if format == 'json':
click.echo('[]')
elif format == 'yaml':
click.echo('[]')
else:
click.echo("No results found.")
return
# Format and display results
formatted_output = format_output(results, format)
click.echo(formatted_output)
if config['verbose']:
click.echo(f"Query returned {len(results)} result(s)", err=True)
except ValueError as e:
click.echo(f"Query error: {e}", err=True)
sys.exit(1)
except Exception as e:
click.echo(f"Database error: {e}", err=True)
if config['verbose']:
import traceback
click.echo(traceback.format_exc(), err=True)
sys.exit(1)
@cli.command('db-query')
@click.argument('sql', type=str)
@click.option('--format', '-f', type=click.Choice(['table', 'json', 'yaml', 'simple']), default=lambda: get_default_format(['table', 'json', 'yaml', 'simple']), help='Output format')
@pass_config
def db_query(config, sql, format):
"""
Execute SQL query against the database.
Execute read-only SQL queries to explore and analyze document metadata.
Only SELECT and WITH statements are allowed for security.
SQL: SQL query to execute (SELECT statements only)
Examples:
markitect db-query "SELECT filename, created_at FROM markdown_files"
markitect db-query "SELECT COUNT(*) as total FROM markdown_files" --format json
markitect db-query "SELECT * FROM markdown_files WHERE filename LIKE '%.md'" --format yaml
"""
try:
if config['verbose']:
@@ -595,13 +668,66 @@ def schema(config, format):
"""
Show database schema and table structure.
DEPRECATED: Use 'db-schema' instead. This command will be removed in a future version.
Display the structure of all tables in the database, including
column names, types, and constraints.
Examples:
markitect schema
markitect schema --format json
markitect schema --format yaml
markitect db-schema
markitect db-schema --format json
markitect db-schema --format yaml
"""
# Show deprecation warning (unless in legacy mode)
if not LegacyMode.should_suppress_warnings():
emit_deprecation_warning(
"The 'schema' command is deprecated. Please use 'db-schema' instead. "
"This command will be removed in a future version."
)
try:
if config['verbose']:
click.echo("Retrieving database schema...", err=True)
db_manager = config['db_manager']
# Get schema information
schema_info = db_manager.get_schema()
if not schema_info:
click.echo("No tables found in database.")
return
# Format and display schema
formatted_output = format_output(schema_info, format)
click.echo(formatted_output)
if config['verbose']:
table_count = len(schema_info)
click.echo(f"Schema contains {table_count} table(s)", err=True)
except Exception as e:
click.echo(f"Schema error: {e}", err=True)
if config['verbose']:
import traceback
click.echo(traceback.format_exc(), err=True)
sys.exit(1)
@cli.command('db-schema')
@click.option('--format', '-f', type=click.Choice(['table', 'json', 'yaml', 'simple']), default=lambda: get_default_format(['table', 'json', 'yaml', 'simple']), help='Output format')
@pass_config
def db_schema(config, format):
"""
Show database schema and table structure.
Display the structure of all tables in the database, including
column names, types, and constraints.
Examples:
markitect db-schema
markitect db-schema --format json
markitect db-schema --format yaml
"""
try:
if config['verbose']:
@@ -1907,6 +2033,736 @@ def create_associated_stub(config, schema_file, style, title):
sys.exit(1)
@cli.command('db-delete')
@click.option('--force', is_flag=True, help='Delete without confirmation prompt')
@click.option('--database', type=click.Path(), help='Database file path (overrides global setting)')
@pass_config
def db_delete(config, force, database):
"""
Delete the database file.
WARNING: This operation cannot be undone. All stored data will be lost.
Examples:
markitect db-delete
markitect db-delete --force
markitect db-delete --database /path/to/db.sqlite --force
"""
try:
# Use command-specific database option or fall back to global config
if database:
db_path = Path(database)
else:
db_path = Path(config.get('database_path', os.path.expanduser('~/.markitect/markitect.db')))
if not db_path.exists():
click.echo(f"Database file not found: {db_path}")
return
if not force:
if not click.confirm(f"⚠️ Are you sure you want to delete the database at {db_path}?\nThis action cannot be undone."):
click.echo("Operation cancelled.")
return
# Delete the database file
db_path.unlink()
click.echo(f"✅ Database deleted: {db_path}")
if config.get('verbose'):
click.echo("All stored data has been permanently removed.", err=True)
except Exception as e:
click.echo(f"Error deleting database: {e}", err=True)
sys.exit(1)
@cli.command('db-status')
@click.option('--format', '-f', type=click.Choice(['table', 'json', 'yaml', 'simple']),
default=lambda: get_default_format(['table', 'json', 'yaml', 'simple']), help='Output format')
@click.option('--database', type=click.Path(), help='Database file path (overrides global setting)')
@pass_config
def db_status(config, format, database):
"""
Show database statistics and information.
Display database size and basic information. For detailed table analysis,
use existing database commands after ensuring the database is accessible.
Examples:
markitect db-status
markitect db-status --format json
markitect db-status --database /path/to/db.sqlite
"""
try:
# Use command-specific database option or fall back to global config
if database:
db_path = Path(database)
else:
db_path = Path(config.get('database_path', os.path.expanduser('~/.markitect/markitect.db')))
if not db_path.exists():
if format == 'json':
click.echo('{"error": "Database not found", "path": "' + str(db_path) + '"}')
elif format == 'yaml':
click.echo(f'error: Database not found\npath: {db_path}')
else:
click.echo(f"Database file not found: {db_path}")
return
# Basic file information (no database connection needed)
file_size = db_path.stat().st_size
stats = {
'database_path': str(db_path),
'exists': True,
'size_bytes': file_size,
'size_human': format_file_size(file_size),
'status': 'accessible' if db_path.is_file() else 'inaccessible'
}
# Format and display statistics
formatted_output = format_output(stats, format)
click.echo(formatted_output)
if config.get('verbose'):
click.echo(f"Database status retrieved successfully", err=True)
except Exception as e:
click.echo(f"Error getting database status: {e}", err=True)
if config.get('verbose'):
import traceback
click.echo(traceback.format_exc(), err=True)
sys.exit(1)
def format_file_size(size_bytes):
"""Format file size in human-readable format."""
if size_bytes < 1024:
return f"{size_bytes} B"
elif size_bytes < 1024 * 1024:
return f"{size_bytes / 1024:.1f} KB"
elif size_bytes < 1024 * 1024 * 1024:
return f"{size_bytes / (1024 * 1024):.1f} MB"
else:
return f"{size_bytes / (1024 * 1024 * 1024):.1f} GB"
# Legacy Agent Management Commands
# =================================
# Comprehensive CLI interface for managing legacy interface lifecycle
@cli.group('legacy')
def legacy_management():
"""
Manage legacy interface compatibility and lifecycle.
Provides comprehensive tools for analyzing, managing, and cleaning up
legacy interfaces including deprecation progression, migration assistance,
and automated maintenance.
"""
if not LEGACY_SYSTEM_AVAILABLE:
click.echo("Error: Legacy management system not available", err=True)
click.echo("Install with: pip install markitect[legacy]", err=True)
sys.exit(1)
@legacy_management.command('status')
@click.option('--format', '-f', type=click.Choice(['table', 'json', 'yaml', 'simple']),
default='table', help='Output format')
@click.option('--include-removed', is_flag=True, help='Include removed interfaces')
@pass_config
def legacy_status(config, format, include_removed):
"""
Show status of all legacy interfaces.
Displays comprehensive information about all registered legacy interfaces
including their current status, deprecation dates, and removal schedules.
Examples:
markitect legacy status
markitect legacy status --format json
markitect legacy status --include-removed
"""
try:
registry = LegacyRegistry()
# Get all legacy interfaces
interfaces = []
for command in registry._interfaces:
for version, interface in registry._interfaces[command].items():
if not include_removed and interface.status == LegacyStatus.REMOVED:
continue
interfaces.append({
'command': interface.command,
'version': interface.version,
'status': interface.status.value,
'deprecated_date': interface.deprecated_date,
'removal_date': interface.removal_date,
'git_commit': interface.git_commit[:8] if interface.git_commit else 'N/A',
'description': interface.description or 'No description'
})
if format == 'json':
click.echo(json.dumps(interfaces, indent=2))
elif format == 'yaml':
import yaml
click.echo(yaml.dump(interfaces, default_flow_style=False))
elif format == 'simple':
for interface in interfaces:
status_icon = {
'current': '',
'deprecated': '⚠️',
'legacy': '🔄',
'sunset': '🌅',
'removed': ''
}.get(interface['status'], '')
click.echo(f"{status_icon} {interface['command']} {interface['version']} ({interface['status']})")
else:
# Table format
if interfaces:
headers = ['Command', 'Version', 'Status', 'Deprecated', 'Removal', 'Commit', 'Description']
rows = [[
i['command'], i['version'], i['status'],
i['deprecated_date'][:10] if i['deprecated_date'] else 'N/A',
i['removal_date'][:10] if i['removal_date'] else 'N/A',
i['git_commit'],
i['description'][:30] + '...' if len(i['description']) > 30 else i['description']
] for i in interfaces]
click.echo(tabulate(rows, headers=headers, tablefmt='grid'))
else:
click.echo("No legacy interfaces found.")
if config.get('verbose'):
total = len(interfaces)
by_status = {}
for interface in interfaces:
status = interface['status']
by_status[status] = by_status.get(status, 0) + 1
click.echo(f"\nSummary: {total} interfaces", err=True)
for status, count in by_status.items():
click.echo(f" {status}: {count}", err=True)
except Exception as e:
click.echo(f"Error getting legacy status: {e}", err=True)
if config.get('verbose'):
import traceback
click.echo(traceback.format_exc(), err=True)
sys.exit(1)
@legacy_management.command('analyze')
@click.argument('command', required=False)
@click.argument('version', required=False)
@click.option('--format', '-f', type=click.Choice(['table', 'json', 'yaml', 'detailed']),
default='detailed', help='Output format')
@pass_config
def legacy_analyze(config, command, version, format):
"""
Analyze legacy interfaces for needed actions.
Performs comprehensive analysis of legacy interfaces to identify
deprecation candidates, migration opportunities, and cleanup needs.
Examples:
markitect legacy analyze
markitect legacy analyze query
markitect legacy analyze query v1.0
"""
try:
registry = LegacyRegistry()
agent = LegacyAgent(registry=registry)
if command and version:
# Analyze specific interface
interface = registry.get_legacy_interface(command, version)
if not interface:
click.echo(f"Legacy interface {command} {version} not found", err=True)
sys.exit(1)
analysis = {
'command': interface.command,
'version': interface.version,
'current_status': interface.status.value,
'deprecated_date': interface.deprecated_date,
'removal_date': interface.removal_date,
'git_commit': interface.git_commit,
'breaking_changes': interface.breaking_changes,
'migration_guide_available': bool(interface.migration_guide),
'recommendations': []
}
# Add recommendations based on status
if interface.status == LegacyStatus.DEPRECATED:
analysis['recommendations'].append("Consider progressing to LEGACY status")
elif interface.status == LegacyStatus.LEGACY:
analysis['recommendations'].append("Monitor usage and prepare for SUNSET")
elif interface.status == LegacyStatus.SUNSET:
analysis['recommendations'].append("Schedule final removal")
if not interface.migration_guide:
analysis['recommendations'].append("Generate migration guide")
if format == 'json':
click.echo(json.dumps(analysis, indent=2))
elif format == 'yaml':
import yaml
click.echo(yaml.dump(analysis, default_flow_style=False))
else:
click.echo(f"Analysis for {command} {version}")
click.echo("=" * 40)
click.echo(f"Status: {analysis['current_status']}")
click.echo(f"Deprecated: {analysis['deprecated_date'] or 'N/A'}")
click.echo(f"Removal: {analysis['removal_date'] or 'N/A'}")
click.echo(f"Migration guide: {'Available' if analysis['migration_guide_available'] else 'Missing'}")
if analysis['breaking_changes']:
click.echo(f"\nBreaking changes ({len(analysis['breaking_changes'])}):")
for change in analysis['breaking_changes']:
click.echo(f"{change}")
if analysis['recommendations']:
click.echo(f"\nRecommendations:")
for rec in analysis['recommendations']:
click.echo(f"{rec}")
else:
# Analyze all interfaces
candidates = registry.get_deprecation_candidates(days_ahead=30)
usage_stats = registry.get_usage_statistics(days=30)
analysis = {
'total_interfaces': sum(len(versions) for versions in registry._interfaces.values()),
'deprecation_candidates': len(candidates),
'recent_usage': usage_stats['total_usage'],
'cleanup_opportunities': 0,
'migration_guides_needed': 0
}
# Count missing migration guides and cleanup opportunities
for command_versions in registry._interfaces.values():
for interface in command_versions.values():
if not interface.migration_guide and interface.status in [LegacyStatus.LEGACY, LegacyStatus.SUNSET]:
analysis['migration_guides_needed'] += 1
if interface.status == LegacyStatus.SUNSET:
analysis['cleanup_opportunities'] += 1
if format == 'json':
click.echo(json.dumps(analysis, indent=2))
elif format == 'yaml':
import yaml
click.echo(yaml.dump(analysis, default_flow_style=False))
else:
click.echo("Legacy Interface Analysis")
click.echo("=" * 30)
click.echo(f"Total interfaces: {analysis['total_interfaces']}")
click.echo(f"Deprecation candidates: {analysis['deprecation_candidates']}")
click.echo(f"Recent usage events: {analysis['recent_usage']}")
click.echo(f"Migration guides needed: {analysis['migration_guides_needed']}")
click.echo(f"Cleanup opportunities: {analysis['cleanup_opportunities']}")
if candidates:
click.echo(f"\nUpcoming removals:")
for candidate in candidates[:5]: # Show first 5
click.echo(f"{candidate.command} {candidate.version} (removal: {candidate.removal_date})")
except Exception as e:
click.echo(f"Error analyzing legacy interfaces: {e}", err=True)
if config.get('verbose'):
import traceback
click.echo(traceback.format_exc(), err=True)
sys.exit(1)
@legacy_management.command('migrate')
@click.argument('command')
@click.argument('version')
@click.option('--to-version', default='current', help='Target version for migration')
@pass_config
def legacy_migrate(config, command, version, to_version):
"""
Get migration guidance for a legacy version.
Provides detailed migration instructions and breaking change information
for upgrading from a legacy interface version to current or another version.
Examples:
markitect legacy migrate query v1.0
markitect legacy migrate query v1.0 --to-version v2.0
"""
try:
registry = LegacyRegistry()
interface = registry.get_legacy_interface(command, version)
if not interface:
click.echo(f"Legacy version {command} {version} not found", err=True)
sys.exit(1)
migration = registry.get_migration_path(command, version, to_version)
click.echo(f"Migration Guide: {command} {version}{to_version}")
click.echo("=" * 60)
if interface.migration_guide:
click.echo(interface.migration_guide)
else:
click.echo("No specific migration guide available.")
click.echo("Consider generating one with: markitect legacy generate-guide")
if migration['breaking_changes']:
click.echo("\nBreaking Changes:")
for i, change in enumerate(migration['breaking_changes'], 1):
click.echo(f"{i}. {change}")
if migration['steps']:
click.echo("\nMigration Steps:")
for i, step in enumerate(migration['steps'], 1):
click.echo(f"{i}. {step}")
# Show additional context
click.echo(f"\nInterface Details:")
click.echo(f" Current status: {interface.status.value}")
if interface.deprecated_date:
click.echo(f" Deprecated: {interface.deprecated_date}")
if interface.removal_date:
click.echo(f" Removal scheduled: {interface.removal_date}")
except Exception as e:
click.echo(f"Error getting migration guide: {e}", err=True)
sys.exit(1)
@legacy_management.command('cleanup')
@click.argument('command')
@click.argument('version')
@click.option('--force', is_flag=True, help='Force cleanup without confirmation')
@click.option('--backup', is_flag=True, default=True, help='Create backup before cleanup')
@pass_config
def legacy_cleanup(config, command, version, force, backup):
"""
Clean up a specific legacy version.
Permanently removes a legacy interface from the registry and optionally
creates a backup for restoration if needed.
Examples:
markitect legacy cleanup query v1.0
markitect legacy cleanup query v1.0 --force
markitect legacy cleanup query v1.0 --no-backup
"""
try:
agent = LegacyAgent()
if not force:
interface = agent.registry.get_legacy_interface(command, version)
if interface:
click.echo(f"About to clean up {command} {version}")
click.echo(f"Status: {interface.status.value}")
if interface.removal_date:
click.echo(f"Scheduled removal: {interface.removal_date}")
if interface.status not in [LegacyStatus.SUNSET, LegacyStatus.REMOVED]:
click.echo("Warning: Interface is not in SUNSET status")
if not click.confirm("Are you sure you want to proceed?"):
click.echo("Cleanup cancelled.")
return
# Configure backup behavior
original_backup_config = agent.config.backup_before_cleanup
agent.config.backup_before_cleanup = backup
success = agent.force_cleanup(command, version)
# Restore original config
agent.config.backup_before_cleanup = original_backup_config
if success:
click.echo(f"✅ Successfully cleaned up {command} {version}")
if backup:
click.echo("📦 Backup created in agent data directory")
else:
click.echo(f"❌ Failed to clean up {command} {version}", err=True)
sys.exit(1)
except Exception as e:
click.echo(f"Error during cleanup: {e}", err=True)
sys.exit(1)
@legacy_management.command('agent-run')
@click.option('--dry-run', is_flag=True, help='Show what would be done without executing')
@pass_config
def legacy_agent_run(config, dry_run):
"""
Run legacy agent maintenance cycle.
Executes automated maintenance including deprecation progression,
cleanup scheduling, migration guide generation, and user notifications.
Examples:
markitect legacy agent-run
markitect legacy agent-run --dry-run
"""
try:
agent = LegacyAgent()
if dry_run:
click.echo("DRY RUN: Legacy agent maintenance preview")
click.echo("=" * 50)
# Show what would be done
agent_config = AgentConfig(
auto_progression=False, # Disable actual changes
cleanup_unused_days=agent.config.cleanup_unused_days,
migration_guide_auto_generation=False,
notification_threshold_days=agent.config.notification_threshold_days,
max_concurrent_migrations=agent.config.max_concurrent_migrations,
backup_before_cleanup=agent.config.backup_before_cleanup
)
# Create a preview agent
preview_agent = LegacyAgent(config=agent_config)
# Analyze what would be done
preview_agent._analyze_legacy_interfaces()
pending_tasks = [task for task in preview_agent._tasks if not task.completed]
if pending_tasks:
click.echo(f"Would schedule {len(pending_tasks)} tasks:")
for task in pending_tasks:
click.echo(f"{task.action.value}: {task.command}:{task.version}")
else:
click.echo("No maintenance tasks needed")
else:
click.echo("Running legacy agent maintenance...")
summary = agent.run_maintenance()
click.echo("Maintenance Summary")
click.echo("=" * 20)
click.echo(f"Tasks executed: {summary['tasks_executed']}")
click.echo(f"Progressions: {summary['progressions']}")
click.echo(f"Cleanups: {summary['cleanups']}")
click.echo(f"Notifications: {summary['notifications']}")
if summary['errors']:
click.echo(f"\nErrors ({len(summary['errors'])}):")
for error in summary['errors']:
click.echo(f"{error}")
click.echo(f"\nStarted: {summary['started_at']}")
click.echo(f"Completed: {summary['completed_at']}")
except Exception as e:
click.echo(f"Error running agent maintenance: {e}", err=True)
if config.get('verbose'):
import traceback
click.echo(traceback.format_exc(), err=True)
sys.exit(1)
@legacy_management.command('agent-status')
@click.option('--format', '-f', type=click.Choice(['table', 'json', 'yaml']),
default='table', help='Output format')
@pass_config
def legacy_agent_status(config, format):
"""
Show legacy agent status and statistics.
Displays comprehensive information about the legacy agent including
task queue status, configuration, and registry statistics.
Examples:
markitect legacy agent-status
markitect legacy agent-status --format json
"""
try:
agent = LegacyAgent()
status = agent.get_agent_status()
if format == 'json':
click.echo(json.dumps(status, indent=2))
elif format == 'yaml':
import yaml
click.echo(yaml.dump(status, default_flow_style=False))
else:
click.echo("Legacy Agent Status")
click.echo("=" * 30)
click.echo(f"Data Directory: {status['data_directory']}")
click.echo(f"Auto Progression: {'Enabled' if status['config']['auto_progression'] else 'Disabled'}")
click.echo(f"Cleanup After: {status['config']['cleanup_unused_days']} days")
click.echo(f"\nTask Queue:")
click.echo(f" Total: {status['tasks']['total']}")
click.echo(f" Pending: {status['tasks']['pending']}")
click.echo(f" Completed: {status['tasks']['completed']}")
if status['next_maintenance']:
click.echo(f"\nNext Maintenance: {status['next_maintenance']}")
click.echo(f"\nRegistry Statistics:")
for stat_name, stat_value in status['registry_stats'].items():
if stat_name == 'commands':
click.echo(f" Commands: {', '.join(stat_value) if stat_value else 'none'}")
else:
click.echo(f" {stat_name}: {stat_value}")
except Exception as e:
click.echo(f"Error getting agent status: {e}", err=True)
sys.exit(1)
@legacy_management.command('usage-stats')
@click.option('--command', help='Filter by specific command')
@click.option('--days', type=int, default=30, help='Number of days to analyze')
@click.option('--format', '-f', type=click.Choice(['table', 'json', 'yaml']),
default='table', help='Output format')
@pass_config
def legacy_usage_stats(config, command, days, format):
"""
Show usage statistics for legacy interfaces.
Displays usage patterns to help make informed decisions about
deprecation timelines and cleanup priorities.
Examples:
markitect legacy usage-stats
markitect legacy usage-stats --command query
markitect legacy usage-stats --days 90 --format json
"""
try:
registry = LegacyRegistry()
stats = registry.get_usage_statistics(command=command, days=days)
if format == 'json':
click.echo(json.dumps(stats, indent=2))
elif format == 'yaml':
import yaml
click.echo(yaml.dump(stats, default_flow_style=False))
else:
click.echo(f"Legacy Interface Usage ({days} days)")
click.echo("=" * 40)
click.echo(f"Total usage events: {stats['total_usage']}")
if stats['by_command']:
click.echo(f"\nBy Command:")
for cmd, versions in stats['by_command'].items():
total_cmd_usage = sum(v['usage_count'] for v in versions.values())
click.echo(f" {cmd}: {total_cmd_usage} uses")
for version, data in versions.items():
click.echo(f" {version}: {data['usage_count']} (last: {data['last_used'][:10]})")
if stats['by_version']:
click.echo(f"\nMost Used Versions:")
sorted_versions = sorted(stats['by_version'].items(),
key=lambda x: x[1], reverse=True)
for version_key, count in sorted_versions[:10]:
click.echo(f" {version_key}: {count} uses")
if config.get('verbose'):
click.echo(f"\nAnalysis period: {days} days", err=True)
if command:
click.echo(f"Filtered to command: {command}", err=True)
except Exception as e:
click.echo(f"Error getting usage statistics: {e}", err=True)
sys.exit(1)
@legacy_management.command('generate-guide')
@click.argument('command')
@click.argument('version')
@click.option('--output', '-o', type=click.Path(), help='Output file (default: stdout)')
@pass_config
def legacy_generate_guide(config, command, version, output):
"""
Generate migration guide for a legacy interface.
Creates detailed migration documentation for upgrading from
a legacy interface version to the current implementation.
Examples:
markitect legacy generate-guide query v1.0
markitect legacy generate-guide query v1.0 --output migration_guide.md
"""
try:
registry = LegacyRegistry()
interface = registry.get_legacy_interface(command, version)
if not interface:
click.echo(f"Legacy interface {command} {version} not found", err=True)
sys.exit(1)
# Generate guide content
guide_content = f"""# Migration Guide: {command} {version} → Current
## Overview
This guide helps you migrate from the legacy `{command}` {version} interface to the current implementation.
**Status**: {interface.status.value}
**Deprecated**: {interface.deprecated_date or 'Not specified'}
**Removal Date**: {interface.removal_date or 'Not scheduled'}
## Breaking Changes
"""
if interface.breaking_changes:
for i, change in enumerate(interface.breaking_changes, 1):
guide_content += f"{i}. {change}\n"
else:
guide_content += "No specific breaking changes documented.\n"
guide_content += f"""
## Migration Steps
1. **Remove the legacy flag**: Stop using `--legacy-{version.replace('.', '-')}`
2. **Update command syntax**: Review the current command documentation
3. **Test thoroughly**: Verify that your use cases work with the new interface
4. **Update automation**: Modify any scripts or tools that use the legacy interface
## Getting Help
- Run: `markitect help {command}`
- Check the documentation for current syntax
- Review the changelog for detailed changes
## Example Migration
```bash
# Old (legacy {version})
markitect {command} --legacy-{version.replace('.', '-')} [arguments]
# New (current)
markitect {command} [arguments]
```
For specific parameter changes, refer to the breaking changes section above.
"""
if interface.migration_guide:
guide_content += f"\n## Additional Notes\n\n{interface.migration_guide}\n"
# Output
if output:
with open(output, 'w', encoding='utf-8') as f:
f.write(guide_content)
click.echo(f"✅ Migration guide written to: {output}")
else:
click.echo(guide_content)
# Update interface with generated guide if it didn't have one
if not interface.migration_guide:
interface.migration_guide = guide_content
# Note: In a full implementation, this would save back to registry
except Exception as e:
click.echo(f"Error generating migration guide: {e}", err=True)
sys.exit(1)
def main():
"""
Main entry point for the CLI.