Coverage for src/moai_adk/cli/commands/validate_links.py: 0.00%

49 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-11-20 14:35 +0900

1""" 

2Link Validation CLI Command 

3 

4Validate online documentation links 

5""" 

6 

7import argparse 

8import asyncio 

9from pathlib import Path 

10 

11from moai_adk.utils.link_validator import LinkValidator 

12 

13 

14def create_parser(subparsers) -> argparse.ArgumentParser: 

15 """Create link validation parser""" 

16 parser = subparsers.add_parser( 

17 "validate-links", 

18 help="Validate online documentation links", 

19 description="Automatically validate all online documentation links in README.ko.md", 

20 ) 

21 

22 parser.add_argument( 

23 "--file", 

24 "-f", 

25 type=str, 

26 default="README.ko.md", 

27 help="File path to validate (default: README.ko.md)", 

28 ) 

29 

30 parser.add_argument( 

31 "--max-concurrent", 

32 "-c", 

33 type=int, 

34 default=3, 

35 help="Maximum number of links to validate concurrently (default: 3)", 

36 ) 

37 

38 parser.add_argument( 

39 "--timeout", 

40 "-t", 

41 type=int, 

42 default=8, 

43 help="Request timeout in seconds (default: 8)", 

44 ) 

45 

46 parser.add_argument("--output", "-o", type=str, help="File path to save results") 

47 

48 parser.add_argument( 

49 "--verbose", 

50 "-v", 

51 action="store_true", 

52 help="Display detailed progress information", 

53 ) 

54 

55 return parser 

56 

57 

58def run_command(args) -> int: 

59 """Execute link validation command""" 

60 try: 

61 # Configure file path 

62 file_path = Path(args.file) 

63 if not file_path.exists(): 

64 print(f"Error: File does not exist: {file_path}") 

65 return 1 

66 

67 # Create validator 

68 validator = LinkValidator( 

69 max_concurrent=args.max_concurrent, timeout=args.timeout 

70 ) 

71 

72 if args.verbose: 

73 print(f"Extracting links from file: {file_path}") 

74 

75 # Extract links 

76 links = validator.extract_links_from_file(file_path) 

77 

78 if not links: 

79 print("No links to validate.") 

80 return 0 

81 

82 if args.verbose: 

83 print(f"Validating a total of {len(links)} links...") 

84 

85 # Execute async validation 

86 async def validate_links(): 

87 async with validator: 

88 result = await validator.validate_all_links(links) 

89 return result 

90 

91 result = asyncio.run(validate_links()) 

92 

93 # Generate report 

94 report = validator.generate_report(result) 

95 

96 # Output 

97 print(report) 

98 

99 # Save to file 

100 if args.output: 

101 output_path = Path(args.output) 

102 output_path.write_text(report, encoding="utf-8") 

103 print(f"\nResults saved to: {output_path}") 

104 

105 # Return exit code 

106 if result.invalid_links > 0: 

107 print(f"\n⚠️ {result.invalid_links} link(s) failed validation.") 

108 return 1 

109 else: 

110 print("\n✅ All links validated successfully.") 

111 return 0 

112 

113 except KeyboardInterrupt: 

114 print("\nValidation cancelled by user.") 

115 return 1 

116 except Exception as e: 

117 print(f"Error occurred: {e}") 

118 return 1