Skip to content

Commit f33c095

Browse files
eliottnessdmathieupellared
authored
Rewrite verify_readmes.sh script in Go (#6598)
- [x] Rewrite `verify_readmes.sh` as the package `internal/tools/verifyreadmes` - [x] Tweak `Makefile` to call the new script instead of the old one - [x] Remove `verify_readmes.sh` Closes #6527 --------- Signed-off-by: Eliott Bouhana <[email protected]> Co-authored-by: Damien Mathieu <[email protected]> Co-authored-by: Robert Pająk <[email protected]>
1 parent 1ac6c6f commit f33c095

File tree

3 files changed

+86
-24
lines changed

3 files changed

+86
-24
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ $(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/build-tools/crosslink
4343
SEMCONVKIT = $(TOOLS)/semconvkit
4444
$(TOOLS)/semconvkit: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/semconvkit
4545

46+
VERIFYREADMES = $(TOOLS)/verifyreadmes
47+
$(TOOLS)/verifyreadmes: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/verifyreadmes
48+
4649
GOLANGCI_LINT = $(TOOLS)/golangci-lint
4750
$(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/v2/cmd/golangci-lint
4851

@@ -68,7 +71,7 @@ GOVULNCHECK = $(TOOLS)/govulncheck
6871
$(TOOLS)/govulncheck: PACKAGE=golang.org/x/vuln/cmd/govulncheck
6972

7073
.PHONY: tools
71-
tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(SEMCONVGEN) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
74+
tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(SEMCONVGEN) $(VERIFYREADMES) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
7275

7376
# Virtualized python tools via docker
7477

@@ -322,5 +325,5 @@ lint-markdown:
322325
docker run --rm -u $(DOCKER_USER) -v "$(CURDIR):$(WORKDIR)" $(MARKDOWNIMAGE) -c $(WORKDIR)/.markdownlint.yaml $(WORKDIR)/**/*.md
323326

324327
.PHONY: verify-readmes
325-
verify-readmes:
326-
./verify_readmes.sh
328+
verify-readmes: $(VERIFYREADMES)
329+
$(VERIFYREADMES)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
37+
for _, dir := range excludedDirs {
38+
if strings.Contains(path, dir) {
39+
return nil
40+
}
41+
}
42+
43+
44+
// Check that a README.md exists in the same directory as the go.mod file.
45+
readme := filepath.Join(filepath.Dir(path), readmeFilename)
46+
_, err = os.Stat(readme)
47+
if os.IsNotExist(err) {
48+
err = fmt.Errorf("couldn't find %s for %q", readmeFilename, filepath.Dir(path))
49+
}
50+
51+
return err
52+
}
53+
54+
func main() {
55+
root, err := os.Getwd()
56+
if len(os.Args) == 2 {
57+
root, err = filepath.Abs(os.Args[1])
58+
}
59+
60+
if err != nil {
61+
fmt.Println("Error: ", err)
62+
os.Exit(1)
63+
}
64+
65+
fmt.Println("Verifying READMEs in", root)
66+
67+
var errs []string
68+
filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
69+
if err := verifyReadme(path, info, err); err != nil {
70+
errs = append(errs, err.Error())
71+
}
72+
return nil // continue walking
73+
})
74+
75+
if len(errs) > 0 {
76+
fmt.Println("Some readme files couldn't be found.")
77+
fmt.Println(strings.Join(errs, "\n"))
78+
os.Exit(1)
79+
}
80+
}

verify_readmes.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)