//go:build generate // +build generate package main import ( "bytes" "context" "fmt" "go/format" "net/netip" "os" "time" _ "unsafe" "github.com/r2northstar/atlas/pkg/cloudflare" ) //go:linkname iplist github.com/r2northstar/atlas/pkg/cloudflare.iplist var iplist []netip.Prefix func main() { if err := cloudflare.UpdateIPs(context.Background()); err != nil { fmt.Fprintf(os.Stderr, "error: fetch ip list: %v\n", err) os.Exit(1) } var b bytes.Buffer fmt.Fprintf(&b, "// Code generated by iplist_generate on %s; DO NOT EDIT.\n", time.Now().UTC()) fmt.Fprintf(&b, "\n\n") fmt.Fprintf(&b, "package cloudflare\n") fmt.Fprintf(&b, "\n\n") fmt.Fprintf(&b, "import %#v\n", "net/netip") fmt.Fprintf(&b, "\n\n") fmt.Fprintf(&b, "//go:generate go run iplist_generate.go\n") fmt.Fprintf(&b, "\n\n") fmt.Fprintf(&b, "func init() {\n") fmt.Fprintf(&b, "iplist = append(iplist,\n") for _, p := range iplist { fmt.Fprintf(&b, "netip.MustParsePrefix(%#v),\n", p.String()) } fmt.Fprintf(&b, ")\n") fmt.Fprintf(&b, "}\n") if src, err := format.Source(b.Bytes()); err != nil { fmt.Fprintf(os.Stderr, "error: format generated code: %v\n", err) os.Exit(1) } else if err = os.WriteFile("iplist_generated.go", src, 0666); err != nil { fmt.Fprintf(os.Stderr, "error: write generated code: %v\n", err) os.Exit(1) } }