Replace uses of code which requires 1.24+

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan
2025-07-24 16:43:50 -07:00
parent 900a0516de
commit 58c95cde9b
2 changed files with 23 additions and 2 deletions

View File

@@ -889,7 +889,9 @@ func TestFirewallBackendSwitch(t *testing.T) {
dump = icmd.RunCommand("iptables-save").Combined()
dump += icmd.RunCommand("ip6tables-save").Combined()
})
for line := range strings.Lines(dump) {
// TODO: (When Go 1.24 is min version) Replace with `strings.Lines(dump)`.
for _, line := range strings.Split(dump, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue

View File

@@ -20,6 +20,7 @@ package nftablesdoc
import (
"context"
"fmt"
"iter"
"net/netip"
"os"
"path/filepath"
@@ -413,7 +414,7 @@ func runNftables(t *testing.T, host networking.Host) map[string]string {
// {{index . "map filter-forward-out-jumps"}}
var sb strings.Builder
for line := range strings.Lines(out) {
for line := range lines(out) {
if line == "\n" || (line != "" && line[0] == '}') {
block := sb.String()
sb.Reset()
@@ -428,6 +429,24 @@ func runNftables(t *testing.T, host networking.Host) map[string]string {
return res
}
// lines produces a line iterator
// TODO: (When Go 1.24 is min version) Replace with `strings.Lines(out)`.
func lines(s string) iter.Seq[string] {
return func(yield func(string) bool) {
for s != "" {
var line string
if i := strings.IndexByte(s, '\n'); i >= 0 {
line, s = s[:i+1], s[i+1:]
} else {
line, s = s, ""
}
if !yield(line) {
return
}
}
}
}
func generate(t *testing.T, name string, data map[string]string) string {
t.Helper()
templ, err := template.New(name).ParseFiles(filepath.Join("templates", name))