Debian Codegraph @

internal/tools/verifyreadmes/main.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Package verifyreadmes is used to verify that all go modules in the repository
5 // have a README.md file.
6 package main
7
8 import (
9 "fmt"
10 "io/fs"
11 "os"
12 "path/filepath"
13 "strings"
14 )
15
16 // excludedDirs is a list of directories to exclude from the README check if the full path contains any of these strings.
17 var excludedDirs = []string{
18 "internal",
19 "test",
20 "example",
21 "/.",
22 }
23
24 const readmeFilename = "README.md"
25
26 // verifyReadme is a [os.WalkFunc] that checks if a README.md exists in the same directory as the go.mod file.
27 func verifyReadme(path string, info os.FileInfo, err error) error {
28 if err != nil {
29 return err
30 }
31
32 if !info.Mode().IsRegular() || info.Name() != "go.mod" {
33 return nil
34 }
35
36 for _, dir := range excludedDirs {
37 if strings.Contains(path, dir) {
38 return nil
39 }
40 }
41
42 // Check that a README.md exists in the same directory as the go.mod file.
43 readme := filepath.Join(filepath.Dir(path), readmeFilename)
44 _, err = os.Stat(readme)
45 if os.IsNotExist(err) {
46 err = fmt.Errorf("couldn't find %s for %q", readmeFilename, filepath.Dir(path))
47 }
48
49 return err
50 }
51
52 func main() {
53 root, err := os.Getwd()
54 if len(os.Args) == 2 {
55 root, err = filepath.Abs(os.Args[1])
56 }
57
58 if err != nil {
59 fmt.Println("Error: ", err)
60 os.Exit(1)
61 }
62
63 fmt.Println("Verifying READMEs in", root)
64
65 var errs []string
66 filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
67 if err := verifyReadme(path, info, err); err != nil {
68 errs = append(errs, err.Error())
69 }
70 return nil // continue walking
71 })
72
73 if len(errs) > 0 {
74 fmt.Println("Some readme files couldn't be found.")
75 fmt.Println(strings.Join(errs, "\n"))
76 os.Exit(1)
77 }
78 }