Skip to content

Commit 0f30669

Browse files
committed
fix: integrate validatePath and loadGoMod functions into analyzer operations
1 parent df1e26a commit 0f30669

File tree

1 file changed

+26
-57
lines changed

1 file changed

+26
-57
lines changed

analyzer.go

+26-57
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,12 @@ func (a *DefaultAnalyzer) loadGoMod() (*modfile.File, error) {
8383
return nil, fmt.Errorf("read go.mod: %w", err)
8484
}
8585

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)
9889
}
9990

100-
return nil, nil
91+
return modFile, nil
10192
}
10293

10394
// loadPackage loads a package with basic configuration
@@ -368,8 +359,8 @@ func (a *DefaultAnalyzer) FindFunction(ctx context.Context, pkgPath, funcName st
368359

369360
// AnalyzeFile analyzes a specific Go source file
370361
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)
373364
}
374365

375366
// Read file content
@@ -464,28 +455,17 @@ func (a *DefaultAnalyzer) AnalyzeFile(ctx context.Context, filePath string) (*An
464455

465456
// AnalyzePackage analyzes a Go package
466457
// 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)
481461
}
482462

483463
pkg, err := a.loadPackage(pkgPath)
484464
if err != nil {
485465
return nil, err
486466
}
487467

488-
result = &AnalysisResult{
468+
result := &AnalysisResult{
489469
Name: pkg.Name,
490470
Path: pkgPath,
491471
StartTime: time.Now().Format(time.RFC3339),
@@ -545,29 +525,22 @@ func (a *DefaultAnalyzer) AnalyzePackage(ctx context.Context, pkgPath string) (r
545525

546526
// AnalyzeProject analyzes a Go project at the specified path
547527
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)
550530
}
551531

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()
555534
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)
567536
}
568537

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(),
571544
}
572545

573546
// Get all Go files in the project
@@ -601,15 +574,11 @@ func (a *DefaultAnalyzer) AnalyzeProject(ctx context.Context, projectPath string
601574
}
602575
sort.Strings(importsList)
603576

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
613582
}
614583

615584
// GetCacheStats returns cache statistics if caching is enabled

0 commit comments

Comments
 (0)