|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "go/token" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/dgageot/rubocop-go/cop" |
| 13 | +) |
| 14 | + |
| 15 | +// ConfigVersionImport enforces that config version packages (pkg/config/vN) |
| 16 | +// only import their immediate predecessor (pkg/config/v{N-1}) and the shared |
| 17 | +// types package (pkg/config/types). This preserves the strict migration chain: |
| 18 | +// v0 → v1 → v2 → … → latest. |
| 19 | +type ConfigVersionImport struct{} |
| 20 | + |
| 21 | +func (*ConfigVersionImport) Name() string { return "Lint/ConfigVersionImport" } |
| 22 | +func (*ConfigVersionImport) Description() string { |
| 23 | + return "Config version packages must only import their immediate predecessor" |
| 24 | +} |
| 25 | +func (*ConfigVersionImport) Severity() cop.Severity { return cop.Error } |
| 26 | + |
| 27 | +// configVersionRe matches "pkg/config/vN" at the end of an import path. |
| 28 | +var configVersionRe = regexp.MustCompile(`pkg/config/v(\d+)$`) |
| 29 | + |
| 30 | +// Check inspects import declarations in config version packages. |
| 31 | +func (c *ConfigVersionImport) Check(fset *token.FileSet, file *ast.File) []cop.Offense { |
| 32 | + if len(file.Imports) == 0 { |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + // Determine which config version package this file belongs to. |
| 37 | + filename := fset.Position(file.Package).Filename |
| 38 | + dirVersion, isVersioned := extractDirVersion(filename) |
| 39 | + dirIsLatest := isLatestDir(filename) |
| 40 | + |
| 41 | + if !isVersioned && !dirIsLatest { |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + var offenses []cop.Offense |
| 46 | + |
| 47 | + for _, imp := range file.Imports { |
| 48 | + importPath := strings.Trim(imp.Path.Value, `"`) |
| 49 | + |
| 50 | + if !strings.Contains(importPath, "pkg/config/") { |
| 51 | + continue |
| 52 | + } |
| 53 | + |
| 54 | + if strings.HasSuffix(importPath, "pkg/config/types") { |
| 55 | + continue |
| 56 | + } |
| 57 | + |
| 58 | + if isVersioned { |
| 59 | + offenses = append(offenses, c.checkVersionedImport(fset, imp, importPath, dirVersion)...) |
| 60 | + } else if dirIsLatest { |
| 61 | + offenses = append(offenses, c.checkLatestImport(fset, imp, importPath)...) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return offenses |
| 66 | +} |
| 67 | + |
| 68 | +func (c *ConfigVersionImport) checkVersionedImport(fset *token.FileSet, imp *ast.ImportSpec, importPath string, dirVersion int) []cop.Offense { |
| 69 | + if strings.HasSuffix(importPath, "pkg/config/latest") { |
| 70 | + return []cop.Offense{cop.NewOffense(c, fset, imp.Path.Pos(), imp.Path.End(), |
| 71 | + fmt.Sprintf("config v%d must not import pkg/config/latest", dirVersion))} |
| 72 | + } |
| 73 | + |
| 74 | + m := configVersionRe.FindStringSubmatch(importPath) |
| 75 | + if m == nil { |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + importedVersion, _ := strconv.Atoi(m[1]) |
| 80 | + expected := dirVersion - 1 |
| 81 | + |
| 82 | + if expected < 0 { |
| 83 | + return []cop.Offense{cop.NewOffense(c, fset, imp.Path.Pos(), imp.Path.End(), |
| 84 | + "config v0 must not import other config version packages")} |
| 85 | + } |
| 86 | + |
| 87 | + if importedVersion != expected { |
| 88 | + return []cop.Offense{cop.NewOffense(c, fset, imp.Path.Pos(), imp.Path.End(), |
| 89 | + fmt.Sprintf("config v%d must import v%d (its predecessor), not v%d", dirVersion, expected, importedVersion))} |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func (c *ConfigVersionImport) checkLatestImport(fset *token.FileSet, imp *ast.ImportSpec, importPath string) []cop.Offense { |
| 96 | + if configVersionRe.MatchString(importPath) { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + return []cop.Offense{cop.NewOffense(c, fset, imp.Path.Pos(), imp.Path.End(), |
| 101 | + "pkg/config/latest should only import config version or types packages, not "+importPath)} |
| 102 | +} |
| 103 | + |
| 104 | +func extractDirVersion(filename string) (int, bool) { |
| 105 | + normalized := filepath.ToSlash(filename) |
| 106 | + |
| 107 | + re := regexp.MustCompile(`/pkg/config/v(\d+)/`) |
| 108 | + m := re.FindStringSubmatch(normalized) |
| 109 | + if m == nil { |
| 110 | + return 0, false |
| 111 | + } |
| 112 | + |
| 113 | + v, err := strconv.Atoi(m[1]) |
| 114 | + if err != nil { |
| 115 | + return 0, false |
| 116 | + } |
| 117 | + return v, true |
| 118 | +} |
| 119 | + |
| 120 | +func isLatestDir(filename string) bool { |
| 121 | + normalized := filepath.ToSlash(filename) |
| 122 | + return strings.Contains(normalized, "/pkg/config/latest/") |
| 123 | +} |
0 commit comments