updateAutoversionFile reads a markdown file and replaces version strings in lines following autoversion pragmas for the given channel.
(path, channel, newVer string)
| 312 | // version strings in lines following autoversion pragmas for |
| 313 | // the given channel. |
| 314 | func updateAutoversionFile(path, channel, newVer string) error { |
| 315 | content, err := os.ReadFile(path) |
| 316 | if err != nil { |
| 317 | return xerrors.Errorf("reading %s: %w", path, err) |
| 318 | } |
| 319 | |
| 320 | lines := strings.Split(string(content), "\n") |
| 321 | changed := false |
| 322 | |
| 323 | for i, line := range lines { |
| 324 | m := autoversionPragmaRe.FindStringSubmatch(line) |
| 325 | if len(m) < 3 { |
| 326 | continue |
| 327 | } |
| 328 | pragmaChannel := m[1] |
| 329 | pattern := m[2] |
| 330 | |
| 331 | if pragmaChannel != channel { |
| 332 | continue |
| 333 | } |
| 334 | |
| 335 | // Build regex from the pattern by replacing |
| 336 | // [version] with a capture group. |
| 337 | escaped := regexp.QuoteMeta(pattern) |
| 338 | reStr := strings.ReplaceAll( |
| 339 | escaped, |
| 340 | regexp.QuoteMeta("[version]"), |
| 341 | `(\d+\.\d+\.\d+)`, |
| 342 | ) |
| 343 | re, err := regexp.Compile(reStr) |
| 344 | if err != nil { |
| 345 | continue |
| 346 | } |
| 347 | |
| 348 | // Search the next few lines for a match. |
| 349 | for j := i + 1; j < len(lines) && j <= i+5; j++ { |
| 350 | if loc := re.FindStringSubmatchIndex(lines[j]); loc != nil { |
| 351 | // loc[2]:loc[3] is the version capture |
| 352 | // group. |
| 353 | lines[j] = lines[j][:loc[2]] + newVer + lines[j][loc[3]:] |
| 354 | changed = true |
| 355 | break |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if !changed { |
| 361 | return nil |
| 362 | } |
| 363 | |
| 364 | //nolint:gosec // File permissions match the original. |
| 365 | return os.WriteFile(path, []byte(strings.Join(lines, "\n")), 0o644) |
| 366 | } |
| 367 | |
| 368 | // updateRancherFile updates the version strings in rancher.md. |
| 369 | func updateRancherFile(path, channel, newVer string) error { |
no test coverage detected