|
| 1 | +//go:build go1.21 |
| 2 | + |
| 3 | +/* |
| 4 | +MIT License |
| 5 | +
|
| 6 | +Copyright (c) 2026 Olivier Mengué and contributors. |
| 7 | +
|
| 8 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +of this software and associated documentation files (the "Software"), to deal |
| 10 | +in the Software without restriction, including without limitation the rights |
| 11 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +copies of the Software, and to permit persons to whom the Software is |
| 13 | +furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | +The above copyright notice and this permission notice shall be included in all |
| 16 | +copies or substantial portions of the Software. |
| 17 | +
|
| 18 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +SOFTWARE. |
| 25 | +*/ |
| 26 | + |
| 27 | +// Command readme-gofmt applies 'gofmt -s' to all Go block in README.md. |
| 28 | +package main |
| 29 | + |
| 30 | +import ( |
| 31 | + "bytes" |
| 32 | + "log" |
| 33 | + "os" |
| 34 | + "os/exec" |
| 35 | + "regexp" |
| 36 | + "slices" |
| 37 | +) |
| 38 | + |
| 39 | +func main() { |
| 40 | + log.SetFlags(0) |
| 41 | + log.SetPrefix("readme-gofmt: ") |
| 42 | + |
| 43 | + buf, err := os.ReadFile("README.md") |
| 44 | + if err != nil { |
| 45 | + log.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + gofmt, err := exec.LookPath("gofmt") |
| 49 | + if err != nil { |
| 50 | + log.Fatal(err) |
| 51 | + } |
| 52 | + |
| 53 | + buf = bytes.ReplaceAll(buf, []byte("\r"), nil) // CRLF -> LF |
| 54 | + |
| 55 | + reBlock := regexp.MustCompile("(?s)\n```go\n(.*?\n)```") |
| 56 | + |
| 57 | + matches := reBlock.FindAllSubmatchIndex(buf, -1) |
| 58 | + |
| 59 | + changes := 0 |
| 60 | + |
| 61 | + for i := len(matches) - 1; i >= 0; i-- { |
| 62 | + match := matches[i] |
| 63 | + block := buf[match[2]:match[3]] |
| 64 | + |
| 65 | + cmd := exec.Command(gofmt, "-s") |
| 66 | + cmd.Stdin = bytes.NewReader(block) |
| 67 | + var output bytes.Buffer |
| 68 | + cmd.Stdout = &output |
| 69 | + cmd.Stderr = os.Stderr |
| 70 | + err := cmd.Run() |
| 71 | + if err != nil { |
| 72 | + log.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + outputBytes := bytes.ReplaceAll(output.Bytes(), []byte("\r"), []byte("")) // CRLF -> LF |
| 76 | + if !bytes.Equal(outputBytes, block) { |
| 77 | + changes++ |
| 78 | + buf = slices.Replace(buf, match[2], match[3], outputBytes...) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if changes > 0 { |
| 83 | + cmd := exec.Command("diff", "-a", "-u", "README.md", "-") |
| 84 | + cmd.Stdin = bytes.NewReader(buf) |
| 85 | + cmd.Stdout = os.Stdout |
| 86 | + cmd.Stderr = os.Stderr |
| 87 | + cmd.Run() |
| 88 | + |
| 89 | + os.Exit(2) |
| 90 | + } |
| 91 | +} |
0 commit comments