|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// RunIneffassign detects ineffectual assignments. |
| 11 | +func RunIneffassign(ctx *CheckContext) (CheckResult, error) { |
| 12 | + scriptsDir := filepath.Join(ctx.RootDir, "scripts") |
| 13 | + |
| 14 | + // Ensure ineffassign is installed |
| 15 | + if !CommandExists("ineffassign") { |
| 16 | + installCmd := exec.Command("go", "install", "github.com/gordonklaus/ineffassign@latest") |
| 17 | + if _, err := RunCommand(installCmd, true); err != nil { |
| 18 | + return CheckResult{}, fmt.Errorf("failed to install ineffassign: %w", err) |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + modules, err := FindGoModules(scriptsDir) |
| 23 | + if err != nil { |
| 24 | + return CheckResult{}, fmt.Errorf("failed to find Go modules: %w", err) |
| 25 | + } |
| 26 | + |
| 27 | + var allIssues []string |
| 28 | + fileCount := 0 |
| 29 | + |
| 30 | + for _, mod := range modules { |
| 31 | + modDir := filepath.Join(scriptsDir, mod) |
| 32 | + |
| 33 | + // Count Go files in this module |
| 34 | + findCmd := exec.Command("find", ".", "-name", "*.go", "-type", "f") |
| 35 | + findCmd.Dir = modDir |
| 36 | + findOutput, _ := RunCommand(findCmd, true) |
| 37 | + if strings.TrimSpace(findOutput) != "" { |
| 38 | + fileCount += len(strings.Split(strings.TrimSpace(findOutput), "\n")) |
| 39 | + } |
| 40 | + |
| 41 | + cmd := exec.Command("ineffassign", "./...") |
| 42 | + cmd.Dir = modDir |
| 43 | + output, err := RunCommand(cmd, true) |
| 44 | + if err != nil { |
| 45 | + allIssues = append(allIssues, fmt.Sprintf("[%s]\n%s", mod, output)) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if len(allIssues) > 0 { |
| 50 | + return CheckResult{}, fmt.Errorf("ineffectual assignments found\n%s", indentOutput(strings.Join(allIssues, "\n"))) |
| 51 | + } |
| 52 | + |
| 53 | + if fileCount > 0 { |
| 54 | + return Success(fmt.Sprintf("%d %s checked, no ineffectual assignments", fileCount, Pluralize(fileCount, "file", "files"))), nil |
| 55 | + } |
| 56 | + return Success("No ineffectual assignments"), nil |
| 57 | +} |
0 commit comments