Skip to content

dev: clean up #4502

New issue

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

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

Already on GitHub? # to your account

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ website_expand_templates:

website_dump_info:
go run ./scripts/website/dump_info/
.PHONY: dump_info
.PHONY: website_dump_info

update_contributors_list:
cd .github/contributors && npm run all
Expand Down
51 changes: 32 additions & 19 deletions scripts/website/expand_templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/golangci/golangci-lint/internal/renameio"
"github.com/golangci/golangci-lint/scripts/website/types"
Expand All @@ -29,6 +30,7 @@ func main() {

func rewriteDocs(replacements map[string]string) error {
madeReplacements := map[string]bool{}

err := filepath.Walk(filepath.Join("docs", "src", "docs"),
func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand All @@ -40,7 +42,7 @@ func rewriteDocs(replacements map[string]string) error {
return processDoc(path, replacements, madeReplacements)
})
if err != nil {
return fmt.Errorf("failed to walk dir: %w", err)
return fmt.Errorf("walk dir: %w", err)
}

if len(madeReplacements) != len(replacements) {
Expand All @@ -57,7 +59,7 @@ func rewriteDocs(replacements map[string]string) error {
func processDoc(path string, replacements map[string]string, madeReplacements map[string]bool) error {
contentBytes, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read %s: %w", path, err)
return fmt.Errorf("read %s: %w", path, err)
}

content := string(contentBytes)
Expand All @@ -81,7 +83,7 @@ func processDoc(path string, replacements map[string]string, madeReplacements ma

log.Printf("Expanded template in %s, saving it", path)
if err = renameio.WriteFile(path, []byte(content), os.ModePerm); err != nil {
return fmt.Errorf("failed to write changes to file %s: %w", path, err)
return fmt.Errorf("write changes to file %s: %w", path, err)
}

return nil
Expand All @@ -92,29 +94,38 @@ type latestRelease struct {
}

func getLatestVersion() (string, error) {
req, err := http.NewRequest( //nolint:noctx
http.MethodGet,
"https://api.github.com/repos/golangci/golangci-lint/releases/latest",
http.NoBody,
)
endpoint := "https://api.github.com/repos/golangci/golangci-lint/releases/latest"

//nolint:noctx
req, err := http.NewRequest(http.MethodGet, endpoint, http.NoBody)
if err != nil {
return "", fmt.Errorf("failed to prepare a http request: %w", err)
return "", fmt.Errorf("prepare a HTTP request: %w", err)
}
req.Header.Add("Accept", "application/vnd.github.v3+json")
resp, err := http.DefaultClient.Do(req)

req.Header.Set("Accept", "application/vnd.github.v3+json")

client := &http.Client{Timeout: 2 * time.Second}

resp, err := client.Do(req)

if err != nil {
return "", fmt.Errorf("failed to get http response for the latest tag: %w", err)
return "", fmt.Errorf("get HTTP response for the latest tag: %w", err)
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read a body for the latest tag: %w", err)
return "", fmt.Errorf("read a body for the latest tag: %w", err)
}

release := latestRelease{}

err = json.Unmarshal(body, &release)
if err != nil {
return "", fmt.Errorf("failed to unmarshal the body for the latest tag: %w", err)
return "", fmt.Errorf("unmarshal the body for the latest tag: %w", err)
}

return release.TagName, nil
}

Expand All @@ -126,7 +137,7 @@ func buildTemplateContext() (map[string]string, error) {

pluginReference, err := getPluginReference()
if err != nil {
return nil, fmt.Errorf("failed to read plugin reference file: %w", err)
return nil, fmt.Errorf("read plugin reference file: %w", err)
}

helps, err := readJSONFile[types.CLIHelp](filepath.Join("assets", "cli-help.json"))
Expand All @@ -136,12 +147,12 @@ func buildTemplateContext() (map[string]string, error) {

changeLog, err := os.ReadFile("CHANGELOG.md")
if err != nil {
return nil, err
return nil, fmt.Errorf("read CHANGELOG.md: %w", err)
}

latestVersion, err := getLatestVersion()
if err != nil {
return nil, fmt.Errorf("failed to get the latest version: %w", err)
return nil, fmt.Errorf("get the latest version: %w", err)
}

exclusions, err := getDefaultExclusions()
Expand Down Expand Up @@ -169,14 +180,16 @@ func readJSONFile[T any](src string) (T, error) {
file, err := os.Open(src)
if err != nil {
var zero T
return zero, err
return zero, fmt.Errorf("open file %s: %w", src, err)
}

defer func() { _ = file.Close() }()

var result T
err = json.NewDecoder(file).Decode(&result)
if err != nil {
var zero T
return zero, err
return zero, fmt.Errorf("decode JSON file %s: %w", src, err)
}

return result, nil
Expand Down
Loading