|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "compress/zlib" |
| 7 | + "encoding/base64" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/rhysd/actionlint" |
| 15 | +) |
| 16 | + |
| 17 | +func generatePermalink(src []byte) (string, error) { |
| 18 | + var out bytes.Buffer |
| 19 | + |
| 20 | + b64 := base64.NewEncoder(base64.StdEncoding, &out) |
| 21 | + comp, _ := zlib.NewWriterLevel(b64, zlib.BestCompression) |
| 22 | + |
| 23 | + scan := bufio.NewScanner(bytes.NewReader(src)) |
| 24 | + first := true |
| 25 | + for scan.Scan() { |
| 26 | + l := scan.Bytes() |
| 27 | + if bytes.HasPrefix(bytes.TrimSpace(l), []byte("#")) { |
| 28 | + continue |
| 29 | + } |
| 30 | + if first { |
| 31 | + first = false |
| 32 | + } else { |
| 33 | + comp.Write([]byte{'\n'}) |
| 34 | + } |
| 35 | + comp.Write(l) |
| 36 | + } |
| 37 | + if err := scan.Err(); err != nil { |
| 38 | + return "", err |
| 39 | + } |
| 40 | + |
| 41 | + comp.Close() |
| 42 | + b64.Close() |
| 43 | + |
| 44 | + return fmt.Sprintf("[Playground](https://rhysd.github.io/actionlint#%s)", out.Bytes()), nil |
| 45 | +} |
| 46 | + |
| 47 | +func runActionlint(src []byte) ([]byte, error) { |
| 48 | + var out bytes.Buffer |
| 49 | + |
| 50 | + opts := &actionlint.LinterOptions{ |
| 51 | + StdinFileName: "test.yaml", |
| 52 | + Shellcheck: "shellcheck", |
| 53 | + Pyflakes: "pyflakes", |
| 54 | + Color: actionlint.ColorOptionKindNever, |
| 55 | + } |
| 56 | + |
| 57 | + l, err := actionlint.NewLinter(&out, opts) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + if _, err := l.Lint("test.yaml", src, nil); err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return out.Bytes(), nil |
| 67 | +} |
| 68 | + |
| 69 | +func update(in []byte) ([]byte, error) { |
| 70 | + var buf bytes.Buffer |
| 71 | + |
| 72 | + var input bytes.Buffer |
| 73 | + var anchor string |
| 74 | + var section string |
| 75 | + var inputHeader bool |
| 76 | + var outputHeader bool |
| 77 | + var inInput bool |
| 78 | + var count int |
| 79 | + lnum := 0 |
| 80 | + scan := bufio.NewScanner(bytes.NewReader(in)) |
| 81 | + for scan.Scan() { |
| 82 | + lnum++ |
| 83 | + l := scan.Text() |
| 84 | + if strings.HasPrefix(l, "## ") && anchor != "" { |
| 85 | + if input.Len() > 0 { |
| 86 | + return nil, fmt.Errorf("example input for %q was not consumed. playground link may not exist", section) |
| 87 | + } |
| 88 | + if section != "" && count == 0 { |
| 89 | + log.Printf("Section %q contains NO example", section) |
| 90 | + } |
| 91 | + section = l[3:] |
| 92 | + log.Printf("Entering new section %q (%s) at line %d", section, anchor, lnum) |
| 93 | + anchor = "" |
| 94 | + inputHeader = false |
| 95 | + outputHeader = false |
| 96 | + inInput = false |
| 97 | + count = 0 |
| 98 | + } |
| 99 | + if strings.HasPrefix(l, `<a name="`) && strings.HasSuffix(l, `"></a>`) { |
| 100 | + anchor = strings.TrimSuffix(strings.TrimPrefix(l, `<a name="`), `"></a>`) |
| 101 | + } |
| 102 | + if l == "Example input:" { |
| 103 | + log.Printf("Found example input header for %q at line %d", section, lnum) |
| 104 | + inputHeader = true |
| 105 | + } |
| 106 | + if l == "```yaml" && inputHeader { |
| 107 | + inputHeader = false |
| 108 | + inInput = true |
| 109 | + } else if inInput && l != "```" { |
| 110 | + input.WriteString(l) |
| 111 | + input.WriteByte('\n') |
| 112 | + } |
| 113 | + if l == "```" { |
| 114 | + if inInput { |
| 115 | + log.Printf("Found an input example (%d bytes) for %q at line %d", input.Len(), section, lnum) |
| 116 | + inInput = false |
| 117 | + } else if outputHeader { |
| 118 | + buf.WriteString("```\n") |
| 119 | + for { |
| 120 | + if !scan.Scan() { |
| 121 | + return nil, fmt.Errorf("code block for output of %q does not close", section) |
| 122 | + } |
| 123 | + lnum++ |
| 124 | + if scan.Text() == "```" { |
| 125 | + break |
| 126 | + } |
| 127 | + } |
| 128 | + if input.Len() == 0 { |
| 129 | + return nil, fmt.Errorf("output cannot be generated because example input for %q does not exist", section) |
| 130 | + } |
| 131 | + log.Printf("Generating output for the input example for %q at line %d", section, lnum) |
| 132 | + out, err := runActionlint(input.Bytes()) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + buf.Write(out) |
| 137 | + } |
| 138 | + } |
| 139 | + if l == "Output:" { |
| 140 | + log.Printf("Found example output header for %q at line %d", section, lnum) |
| 141 | + outputHeader = true |
| 142 | + } |
| 143 | + if l == "<!-- Skip update output -->" { |
| 144 | + log.Printf("Skip updating output for %q due to the comment at line %d", section, lnum) |
| 145 | + outputHeader = false |
| 146 | + } |
| 147 | + if strings.HasPrefix(l, "[Playground](https://rhysd.github.io/actionlint#") && strings.HasSuffix(l, ")") { |
| 148 | + if input.Len() == 0 { |
| 149 | + return nil, fmt.Errorf("playground link cannot be generated because example input for %q does not exist", section) |
| 150 | + } |
| 151 | + if !outputHeader { |
| 152 | + return nil, fmt.Errorf("output code block is missing for %q", section) |
| 153 | + } |
| 154 | + link, err := generatePermalink(input.Bytes()) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + buf.WriteString(link) |
| 159 | + buf.WriteByte('\n') |
| 160 | + log.Printf("Generate playground link for %q at line %d: %s", section, lnum, link) |
| 161 | + outputHeader = false |
| 162 | + input.Reset() |
| 163 | + count++ |
| 164 | + continue |
| 165 | + } |
| 166 | + if l == "<!-- Skip playground link -->" { |
| 167 | + log.Printf("Skip generating playground link for %q due to the comment at line %d", section, lnum) |
| 168 | + outputHeader = false |
| 169 | + input.Reset() |
| 170 | + count++ |
| 171 | + } |
| 172 | + buf.WriteString(l) |
| 173 | + buf.WriteByte('\n') |
| 174 | + } |
| 175 | + return buf.Bytes(), scan.Err() |
| 176 | +} |
| 177 | + |
| 178 | +func Main(args []string) error { |
| 179 | + if len(os.Args) < 2 { |
| 180 | + return errors.New("usage: update-checks-doc FILE") |
| 181 | + } |
| 182 | + p := os.Args[1] |
| 183 | + in, err := os.ReadFile(p) |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + log.Printf("Read %d bytes from %q", len(in), p) |
| 188 | + |
| 189 | + out, err := update(in) |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + |
| 194 | + if bytes.Equal(in, out) { |
| 195 | + log.Printf("Do nothing because there is no update in %q", p) |
| 196 | + return nil |
| 197 | + } |
| 198 | + |
| 199 | + log.Printf("Generate the updated content (%d bytes) for %q", len(out), p) |
| 200 | + if err := os.WriteFile(p, out, 0666); err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +func main() { |
| 208 | + if err := Main(os.Args); err != nil { |
| 209 | + fmt.Fprintln(os.Stderr, err) |
| 210 | + os.Exit(1) |
| 211 | + } |
| 212 | +} |
0 commit comments