aboutsummaryrefslogtreecommitdiff
path: root/pkg/cloudflare/iplist_generate.go
blob: a7bc8b5189180eee55e0f495a28a7fd42659c206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//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)
	}
}