| | | 1 | | package repometa |
| | | 2 | | |
| | | 3 | | import ( |
| | | 4 | | "path/filepath" |
| | | 5 | | "strings" |
| | | 6 | | ) |
| | | 7 | | |
| | | 8 | | type goDetector struct{} |
| | | 9 | | |
| | 84 | 10 | | func (goDetector) detect(dv dirVisit, cfg options) []finding { |
| | 84 | 11 | | var out []finding |
| | 84 | 12 | | |
| | 84 | 13 | | // go.work — the whole directory is a Go workspace. Members are the |
| | 84 | 14 | | // module paths listed in the `use` directive. |
| | 2 | 15 | | if hasFile(dv.files, "go.work") { |
| | 2 | 16 | | members := parseGoWorkUse(filepath.Join(dv.abs, "go.work"), cfg) |
| | 2 | 17 | | expanded := make([]string, 0, len(members)) |
| | 2 | 18 | | for _, m := range members { |
| | 5 | 19 | | expanded = append(expanded, joinRel(dv.rel, m)) |
| | 5 | 20 | | } |
| | 2 | 21 | | out = append(out, finding{ |
| | 2 | 22 | | Kind: KindGoModule, |
| | 2 | 23 | | Confidence: 1.0, |
| | 2 | 24 | | Evidence: []Evidence{ |
| | 2 | 25 | | {Path: relJoin(dv.rel, "go.work"), Reason: "go.work at directory root"}, |
| | 2 | 26 | | }, |
| | 2 | 27 | | Workspaces: []Workspace{{Kind: WorkspaceGo, Members: expanded}}, |
| | 2 | 28 | | }) |
| | 2 | 29 | | return out |
| | | 30 | | } |
| | | 31 | | |
| | 9 | 32 | | if hasFile(dv.files, "go.mod") { |
| | 9 | 33 | | out = append(out, finding{ |
| | 9 | 34 | | Kind: KindGoModule, |
| | 9 | 35 | | Confidence: 1.0, |
| | 9 | 36 | | Evidence: []Evidence{ |
| | 9 | 37 | | {Path: relJoin(dv.rel, "go.mod"), Reason: "go.mod at directory root"}, |
| | 9 | 38 | | }, |
| | 9 | 39 | | }) |
| | 9 | 40 | | } |
| | 82 | 41 | | return out |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | // parseGoWorkUse returns the module directories listed in a go.work file. |
| | | 45 | | // It handles both `use ./mod` and `use ( ... )` block forms. On any read |
| | | 46 | | // error it returns nil. |
| | | 47 | | func parseGoWorkUse(path string, cfg options) []string { |
| | | 48 | | data := readManifestOrNil(path, cfg) |
| | | 49 | | if data == nil { |
| | | 50 | | return nil |
| | | 51 | | } |
| | | 52 | | var members []string |
| | | 53 | | inBlock := false |
| | | 54 | | for line := range strings.SplitSeq(string(data), "\n") { |
| | | 55 | | trim := strings.TrimSpace(line) |
| | | 56 | | if trim == "" || strings.HasPrefix(trim, "//") { |
| | | 57 | | continue |
| | | 58 | | } |
| | | 59 | | if idx := strings.Index(trim, "//"); idx >= 0 { |
| | | 60 | | trim = strings.TrimSpace(trim[:idx]) |
| | | 61 | | } |
| | | 62 | | switch { |
| | | 63 | | case strings.HasPrefix(trim, "use ("): |
| | | 64 | | inBlock = true |
| | | 65 | | case inBlock && trim == ")": |
| | | 66 | | inBlock = false |
| | | 67 | | case inBlock: |
| | | 68 | | m := strings.Trim(trim, "\"'") |
| | | 69 | | m = strings.TrimPrefix(m, "./") |
| | | 70 | | if m != "" { |
| | | 71 | | members = append(members, m) |
| | | 72 | | } |
| | | 73 | | case strings.HasPrefix(trim, "use "): |
| | | 74 | | m := strings.TrimSpace(strings.TrimPrefix(trim, "use ")) |
| | | 75 | | m = strings.Trim(m, "\"'") |
| | | 76 | | m = strings.TrimPrefix(m, "./") |
| | | 77 | | if m != "" { |
| | | 78 | | members = append(members, m) |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | return members |
| | | 83 | | } |