|
| 1 | +/* |
| 2 | +findtaggedmodules finds modules that contain given go build tags. |
| 3 | +
|
| 4 | +Given a directory and a list of tags, the command looks at all .go files |
| 5 | +inside the directory to find any that contain the tags. Once found, it |
| 6 | +finds which module they belong to and returns all directories where any |
| 7 | +of the tags are found. |
| 8 | +
|
| 9 | +It always returns paths Unix-style with a forward slash ("/") as separator |
| 10 | +
|
| 11 | +Usage: |
| 12 | +
|
| 13 | + findtagmodules -tags [tag1,tag2] [-p path] |
| 14 | +
|
| 15 | +The flags are: |
| 16 | +
|
| 17 | + -tags |
| 18 | + List of tags to look for, passed as "tag1,tag2" |
| 19 | + -p |
| 20 | + Root path to search from. Default to the repo root |
| 21 | +*/ |
| 22 | +package main |
| 23 | + |
| 24 | +import ( |
| 25 | + "flag" |
| 26 | + "fmt" |
| 27 | + "io/fs" |
| 28 | + "log" |
| 29 | + "os" |
| 30 | + "path/filepath" |
| 31 | + "strings" |
| 32 | + |
| 33 | + repotools "github.com/awslabs/aws-go-multi-module-repository-tools" |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + tags string |
| 38 | + rootPath string |
| 39 | +) |
| 40 | + |
| 41 | +func init() { |
| 42 | + flag.StringVar(&tags, "tags", "", "Comma-separated list of build tags to search for") |
| 43 | + flag.StringVar(&rootPath, "p", "", "Root path to search from (defaults to repo root)") |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + flag.Parse() |
| 48 | + |
| 49 | + if tags == "" { |
| 50 | + log.Fatal("must specify -tags") |
| 51 | + } |
| 52 | + |
| 53 | + targetTags := strings.Split(tags, ",") |
| 54 | + for i := range targetTags { |
| 55 | + targetTags[i] = strings.TrimSpace(targetTags[i]) |
| 56 | + } |
| 57 | + |
| 58 | + repoRoot, err := repotools.FindRepoRoot(rootPath) |
| 59 | + if err != nil { |
| 60 | + log.Fatalf("failed to find repo root: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + if rootPath == "" { |
| 64 | + rootPath = repoRoot |
| 65 | + } else { |
| 66 | + rootPath = filepath.Join(repoRoot, rootPath) |
| 67 | + } |
| 68 | + |
| 69 | + var boots repotools.Boots |
| 70 | + |
| 71 | + if err := filepath.Walk(rootPath, boots.Walk); err != nil { |
| 72 | + log.Fatalf("failed to walk directory: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + for _, modPath := range boots.Modules() { |
| 76 | + if modPath == rootPath { |
| 77 | + continue |
| 78 | + } |
| 79 | + found, err := hasAnyTag(modPath, targetTags) |
| 80 | + if err != nil { |
| 81 | + log.Fatalf("found an error searching for tags: %v", err) |
| 82 | + } |
| 83 | + if found { |
| 84 | + relPath, err := filepath.Rel(repoRoot, modPath) |
| 85 | + // Use Unix style path |
| 86 | + relPath = filepath.ToSlash(relPath) |
| 87 | + if err != nil { |
| 88 | + fmt.Println(modPath) |
| 89 | + } else { |
| 90 | + fmt.Println(relPath) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func hasAnyTag(modPath string, targetTags []string) (bool, error) { |
| 97 | + found := false |
| 98 | + err := filepath.WalkDir(modPath, func(path string, info fs.DirEntry, err error) error { |
| 99 | + if err != nil || !strings.HasSuffix(path, ".go") { |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + file, err := os.Open(path) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + defer file.Close() |
| 108 | + |
| 109 | + // Read first two lines only |
| 110 | + buf := make([]byte, 200) |
| 111 | + n, _ := file.Read(buf) |
| 112 | + lines := strings.Split(string(buf[:n]), "\n") |
| 113 | + |
| 114 | + for i := 0; i < 2 && i < len(lines); i++ { |
| 115 | + line := strings.TrimSpace(lines[i]) |
| 116 | + if strings.HasPrefix(line, "//go:build") || strings.HasPrefix(line, "// +build") { |
| 117 | + for _, tag := range targetTags { |
| 118 | + if strings.Contains(line, tag) { |
| 119 | + found = true |
| 120 | + return nil |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return nil |
| 126 | + }) |
| 127 | + return found, err |
| 128 | +} |
0 commit comments