@@ -83,21 +83,12 @@ func (a *DefaultAnalyzer) loadGoMod() (*modfile.File, error) {
83
83
return nil , fmt .Errorf ("read go.mod: %w" , err )
84
84
}
85
85
86
- // Extract module name from go.mod
87
- var moduleName string
88
- lines := strings .Split (string (content ), "\n " )
89
- for _ , line := range lines {
90
- if strings .HasPrefix (strings .TrimSpace (line ), "module " ) {
91
- moduleName = strings .TrimSpace (strings .TrimPrefix (strings .TrimSpace (line ), "module " ))
92
- break
93
- }
94
- }
95
-
96
- if moduleName == "" {
97
- return nil , & AnalysisError {Op : "parse go.mod" , Path : goModPath , Wrapped : fmt .Errorf ("module name not found" )}
86
+ modFile , err := modfile .Parse ("go.mod" , content , nil )
87
+ if err != nil {
88
+ return nil , fmt .Errorf ("parse go.mod: %w" , err )
98
89
}
99
90
100
- return nil , nil
91
+ return modFile , nil
101
92
}
102
93
103
94
// loadPackage loads a package with basic configuration
@@ -368,8 +359,8 @@ func (a *DefaultAnalyzer) FindFunction(ctx context.Context, pkgPath, funcName st
368
359
369
360
// AnalyzeFile analyzes a specific Go source file
370
361
func (a * DefaultAnalyzer ) AnalyzeFile (ctx context.Context , filePath string ) (* AnalysisResult , error ) {
371
- if filePath == "" {
372
- return nil , ErrInvalidInput
362
+ if err := a . validatePath ( filePath ); err != nil {
363
+ return nil , fmt . Errorf ( "invalid file path: %w" , err )
373
364
}
374
365
375
366
// Read file content
@@ -464,28 +455,17 @@ func (a *DefaultAnalyzer) AnalyzeFile(ctx context.Context, filePath string) (*An
464
455
465
456
// AnalyzePackage analyzes a Go package
466
457
// It supports analyzing both local and third-party packages
467
- func (a * DefaultAnalyzer ) AnalyzePackage (ctx context.Context , pkgPath string ) (result * AnalysisResult , err error ) {
468
- if a .cache != nil {
469
- key := PackageCacheKey {
470
- Path : pkgPath ,
471
- Mode : "full" ,
472
- }
473
- if cached , ok := a .cache .GetPackage (key ); ok {
474
- return cached , nil
475
- }
476
- defer func () {
477
- if err == nil && result != nil {
478
- a .cache .SetPackage (key , result )
479
- }
480
- }()
458
+ func (a * DefaultAnalyzer ) AnalyzePackage (ctx context.Context , pkgPath string ) (* AnalysisResult , error ) {
459
+ if err := a .validatePath (pkgPath ); err != nil {
460
+ return nil , fmt .Errorf ("invalid package path: %w" , err )
481
461
}
482
462
483
463
pkg , err := a .loadPackage (pkgPath )
484
464
if err != nil {
485
465
return nil , err
486
466
}
487
467
488
- result = & AnalysisResult {
468
+ result : = & AnalysisResult {
489
469
Name : pkg .Name ,
490
470
Path : pkgPath ,
491
471
StartTime : time .Now ().Format (time .RFC3339 ),
@@ -545,29 +525,22 @@ func (a *DefaultAnalyzer) AnalyzePackage(ctx context.Context, pkgPath string) (r
545
525
546
526
// AnalyzeProject analyzes a Go project at the specified path
547
527
func (a * DefaultAnalyzer ) AnalyzeProject (ctx context.Context , projectPath string ) (* AnalysisResult , error ) {
548
- if projectPath == "" {
549
- return nil , ErrInvalidInput
528
+ if err := a . validatePath ( projectPath ); err != nil {
529
+ return nil , fmt . Errorf ( "invalid project path: %w" , err )
550
530
}
551
531
552
- // Read go.mod to get project name
553
- goModPath := filepath .Join (a .workDir , "go.mod" )
554
- content , err := os .ReadFile (goModPath )
532
+ // Load go.mod to get module information
533
+ modFile , err := a .loadGoMod ()
555
534
if err != nil {
556
- return nil , & AnalysisError {Op : "read go.mod" , Path : goModPath , Wrapped : err }
557
- }
558
-
559
- // Extract module name from go.mod
560
- var moduleName string
561
- lines := strings .Split (string (content ), "\n " )
562
- for _ , line := range lines {
563
- if strings .HasPrefix (strings .TrimSpace (line ), "module " ) {
564
- moduleName = strings .TrimSpace (strings .TrimPrefix (strings .TrimSpace (line ), "module " ))
565
- break
566
- }
535
+ return nil , fmt .Errorf ("load go.mod: %w" , err )
567
536
}
568
537
569
- if moduleName == "" {
570
- return nil , & AnalysisError {Op : "parse go.mod" , Path : goModPath , Wrapped : fmt .Errorf ("module name not found" )}
538
+ // Use module information for analysis
539
+ result := & AnalysisResult {
540
+ Name : modFile .Module .Mod .Path ,
541
+ Path : projectPath ,
542
+ StartTime : time .Now ().Format (time .RFC3339 ),
543
+ AnalyzedAt : time .Now (),
571
544
}
572
545
573
546
// Get all Go files in the project
@@ -601,15 +574,11 @@ func (a *DefaultAnalyzer) AnalyzeProject(ctx context.Context, projectPath string
601
574
}
602
575
sort .Strings (importsList )
603
576
604
- return & AnalysisResult {
605
- Name : moduleName ,
606
- Path : a .workDir ,
607
- StartTime : time .Now ().Format (time .RFC3339 ),
608
- AnalyzedAt : time .Now (),
609
- Types : types ,
610
- Functions : functions ,
611
- Imports : importsList ,
612
- }, nil
577
+ result .Types = types
578
+ result .Functions = functions
579
+ result .Imports = importsList
580
+
581
+ return result , nil
613
582
}
614
583
615
584
// GetCacheStats returns cache statistics if caching is enabled
0 commit comments