Skip to content
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
4 changes: 4 additions & 0 deletions .ci.gofmt.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/usr/bin/env bash

set -euo pipefail

if [ -n "$(gofmt -l .)" ]; then
echo "Go code is not formatted:"
gofmt -d .
exit 1
fi

go run ./_readme-gofmt/main.go

go generate ./...
if [ -n "$(git status -s -uno)" ]; then
echo "Go generate output does not match commit."
Expand Down
12 changes: 0 additions & 12 deletions .ci.readme.fmt.sh

This file was deleted.

2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ jobs:
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
with:
go-version: ${{ matrix.go_version }}
- run: npm install -g mdsf-cli
- run: ./.ci.gogenerate.sh
- run: ./.ci.gofmt.sh
- run: ./.ci.readme.fmt.sh
- run: ./.ci.govet.sh
- run: go test -v -race ./...
test:
Expand Down
12 changes: 0 additions & 12 deletions .mdsf.json

This file was deleted.

91 changes: 91 additions & 0 deletions _readme-gofmt/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//go:build go1.21

/*
MIT License
Copyright (c) 2026 Olivier Mengué and contributors.
Comment thread
ccoVeille marked this conversation as resolved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// Command readme-gofmt applies 'gofmt -s' to all Go block in README.md.
package main

import (
"bytes"
"log"
"os"
"os/exec"
"regexp"
"slices"
)

func main() {
log.SetFlags(0)
log.SetPrefix("readme-gofmt: ")

buf, err := os.ReadFile("README.md")
if err != nil {
log.Fatal(err)
}

gofmt, err := exec.LookPath("gofmt")
if err != nil {
log.Fatal(err)
}

buf = bytes.ReplaceAll(buf, []byte("\r"), nil) // CRLF -> LF

reBlock := regexp.MustCompile("(?s)\n```go\n(.*?\n)```")

matches := reBlock.FindAllSubmatchIndex(buf, -1)

changes := 0

for i := len(matches) - 1; i >= 0; i-- {
match := matches[i]
block := buf[match[2]:match[3]]

cmd := exec.Command(gofmt, "-s")
cmd.Stdin = bytes.NewReader(block)
var output bytes.Buffer
cmd.Stdout = &output
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}

outputBytes := bytes.ReplaceAll(output.Bytes(), []byte("\r"), []byte("")) // CRLF -> LF
if !bytes.Equal(outputBytes, block) {
changes++
buf = slices.Replace(buf, match[2], match[3], outputBytes...)
}
}

if changes > 0 {
cmd := exec.Command("diff", "-a", "-u", "README.md", "-")
cmd.Stdin = bytes.NewReader(buf)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()

os.Exit(2)
}
}