Skip to content

Rewrite verify_readmes.sh script in Go #6598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ $(TOOLS)/crosslink: PACKAGE=go.opentelemetry.io/build-tools/crosslink
SEMCONVKIT = $(TOOLS)/semconvkit
$(TOOLS)/semconvkit: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/semconvkit

VERIFYREADMES = $(TOOLS)/verifyreadmes
$(TOOLS)/verifyreadmes: PACKAGE=go.opentelemetry.io/otel/$(TOOLS_MOD_DIR)/verifyreadmes

GOLANGCI_LINT = $(TOOLS)/golangci-lint
$(TOOLS)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/v2/cmd/golangci-lint

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

.PHONY: tools
tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(SEMCONVGEN) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)
tools: $(CROSSLINK) $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(SEMCONVGEN) $(VERIFYREADMES) $(MULTIMOD) $(SEMCONVKIT) $(GOTMPL) $(GORELEASE)

# Virtualized python tools via docker

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

.PHONY: verify-readmes
verify-readmes:
./verify_readmes.sh
verify-readmes: $(VERIFYREADMES)
$(VERIFYREADMES)
81 changes: 81 additions & 0 deletions internal/tools/verifyreadmes/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

// Package verifyreadmes is used to verify that all go modules in the repository
// have a README.md file.
package main

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
)

// excludedDirs is a list of directories to exclude from the README check if the full path contains any of these strings.
var excludedDirs = []string{
"internal",
"test",
"example",
"/.",
}

// verifyReadme is a [os.WalkFunc] that checks if a README.md exists in the same directory as the go.mod file.
func verifyReadme(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if !info.Mode().IsRegular() || info.Name() != "go.mod" {
return nil

}

for _, dir := range excludedDirs {
if strings.Contains(path, dir) {
return nil
}
}

// Check that a README.md exists in the same directory as the go.mod file.
readme := filepath.Join(filepath.Dir(path), "README.md")
_, err = os.Stat(readme)
if os.IsNotExist(err) {
err = fmt.Errorf("couldn't find README.md for %q", filepath.Dir(path))
}

return err

}

func main() {
root, err := os.Getwd()
if len(os.Args) == 2 {
root, err = filepath.Abs(os.Args[1])
}

if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}

fmt.Println("Verifying READMEs in", root)

var errs []string
filepath.Walk(root, func(path string, info fs.FileInfo, err error) error {
err = verifyReadme(path, info, err)
if err != nil {
errs = append(errs, err.Error())
}
return nil // continue walking
})

if len(errs) == 0 {
return
}

fmt.Println("Error: some READMEs couldn't be found.")
fmt.Println(strings.Join(errs, "\n"))
os.Exit(1)
}
21 changes: 0 additions & 21 deletions verify_readmes.sh

This file was deleted.

Loading