Skip to content

Commit

Permalink
chore: fix typo cidr cdir (#7)
Browse files Browse the repository at this point in the history
- replaces occurrences of cdir to cidr
  • Loading branch information
nohehf authored Oct 1, 2024
1 parent 2ffa9f2 commit 1583f6c
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions internal/source/source_alibaba.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (a Alibaba) GetIPRanges() []*IPRange {
if err != nil {
log.Fatal("Failed to load text url to range for Alibaba", err)
}
for _, cdir := range alibabaRanges {
network, cat := ParseCIDR(cdir)
for _, cidr := range alibabaRanges {
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
4 changes: 2 additions & 2 deletions internal/source/source_digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func (a Digitalocean) GetIPRanges() []*IPRange {

ranges := make([]*IPRange, 0)
for _, line := range data {
cdir := line[0]
network, cat := ParseCIDR(cdir)
cidr := line[0]
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
4 changes: 2 additions & 2 deletions internal/source/source_fastly.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func (a Fastly) GetIPRanges() []*IPRange {

fastlyRanges.Addresses = append(fastlyRanges.Addresses, fastlyRanges.IPv6Addresses...)
ranges := make([]*IPRange, 0)
for _, cdir := range fastlyRanges.Addresses {
network, cat := ParseCIDR(cdir)
for _, cidr := range fastlyRanges.Addresses {
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
10 changes: 5 additions & 5 deletions internal/source/source_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func (a Gcp) GetIPRanges() []*IPRange {
}

for _, prefix := range gcpJSON.Prefixes {
cdir := prefix.IPv4Prefix
if cdir == "" {
cdir = prefix.IPv6Prefix
cidr := prefix.IPv4Prefix
if cidr == "" {
cidr = prefix.IPv6Prefix
}

if cdir == "" {
if cidr == "" {
log.Fatal("both ipv4 and ipv6 prefixes are empty", errors.New("must have IP"))
}

network, cat := ParseCIDR(cdir)
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
8 changes: 4 additions & 4 deletions internal/source/source_ibm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Ibm struct{}
const ibmFileURL = "https://raw.githubusercontent.com/dprosper/cidr-calculator/main/data/datacenters.json"

type cat []*struct {
CdirBlocks []string `json:"cidr_blocks"`
CidrBlocks []string `json:"cidr_blocks"`
}

type ibmJSON struct {
Expand Down Expand Up @@ -54,9 +54,9 @@ func (a Ibm) GetIPRanges() []*IPRange {
}

for _, c := range m {
for _, cdirs := range c {
for _, cdir := range cdirs.CdirBlocks {
network, cat := ParseCIDR(cdir)
for _, cidrs := range c {
for _, cidr := range cidrs.CidrBlocks {
network, cat := ParseCIDR(cidr)
if isPrivateNetwork(network) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions internal/source/source_linode.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (a Linode) GetIPRanges() []*IPRange {
if err != nil {
log.Fatal("Failed to load text url to range for Linode", err)
}
for _, cdir := range linodeRanges {
network, cat := ParseCIDR(cdir)
for _, cidr := range linodeRanges {
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
4 changes: 2 additions & 2 deletions internal/source/source_ovh.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (a Ovh) GetIPRanges() []*IPRange {
log.Info("Using static ovh ip ranges")

ranges := make([]*IPRange, 0)
for _, cdir := range ovhRanges {
network, cat := ParseCIDR(cdir)
for _, cidr := range ovhRanges {
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
4 changes: 2 additions & 2 deletions internal/source/source_scaleway.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func (a Scaleway) GetIPRanges() []*IPRange {
log.Info("Using static Scaleway ip ranges")

ranges := make([]*IPRange, 0)
for _, cdir := range scalewayRanges {
network, cat := ParseCIDR(cdir)
for _, cidr := range scalewayRanges {
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
4 changes: 2 additions & 2 deletions internal/source/source_tencent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (a Tencent) GetIPRanges() []*IPRange {
if err != nil {
log.Fatal("Failed to load text url to range for Tencent", err)
}
for _, cdir := range tencentRanges {
network, cat := ParseCIDR(cdir)
for _, cidr := range tencentRanges {
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
4 changes: 2 additions & 2 deletions internal/source/source_ucloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func (a Ucloud) GetIPRanges() []*IPRange {
if err != nil {
log.Fatal("Failed to load text url to range for ucloud", err)
}
for _, cdir := range ucloudRanges {
network, cat := ParseCIDR(cdir)
for _, cidr := range ucloudRanges {
network, cat := ParseCIDR(cidr)
ranges = append(ranges, &IPRange{
Network: network,
Cat: cat,
Expand Down
4 changes: 2 additions & 2 deletions internal/source/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ func LoadTextURLToRange(url string) ([]string, error) {

func isPrivateNetwork(n *net.IPNet) bool {
// Source: https://en.wikipedia.org/wiki/Private_network
for _, cdir := range []string{
for _, cidr := range []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
"100.64.0.0/10",
"fc00::/7",
} {
_, pn, err := net.ParseCIDR(cdir)
_, pn, err := net.ParseCIDR(cidr)
if err != nil {
panic(err)
}
Expand Down
28 changes: 14 additions & 14 deletions internal/tree/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func makeTreeHelper() *tree {
Root: newNode(),
Cat: source.CatIPv4,
}
networksCdirs := []string{
networksCidrs := []string{
"8.8.4.0/24",
"3.5.140.0/22",
}

// build the tree
for _, networkCdir := range networksCdirs {
_, network, err := net.ParseCIDR(networkCdir)
for _, networkCidr := range networksCidrs {
_, network, err := net.ParseCIDR(networkCidr)
if err != nil {
log.Error("Failed to parse CIDR", err)
}
Expand Down Expand Up @@ -128,9 +128,9 @@ func TestSerializeAndLoad(t *testing.T) {
}
}

func cdirsToRanges(cdirs []string) []*source.IPRange {
func cidrsToRanges(cidrs []string) []*source.IPRange {
ranges := make([]*source.IPRange, 0)
for _, c := range cdirs {
for _, c := range cidrs {
net, cat := source.ParseCIDR(c)
ranges = append(ranges, &source.IPRange{
Network: net,
Expand All @@ -140,12 +140,12 @@ func cdirsToRanges(cdirs []string) []*source.IPRange {
return ranges
}

func rangesToCdirs(ranges []*source.IPRange) []string {
cdirs := make([]string, 0)
func rangesToCidrs(ranges []*source.IPRange) []string {
cidrs := make([]string, 0)
for _, r := range ranges {
cdirs = append(cdirs, r.Network.String())
cidrs = append(cidrs, r.Network.String())
}
return cdirs
return cidrs
}

func TestOverlapCases(t *testing.T) {
Expand Down Expand Up @@ -182,18 +182,18 @@ func TestOverlapCases(t *testing.T) {
}

for _, tt := range tests {
ranges := cdirsToRanges(tt.ranges)
ranges := cidrsToRanges(tt.ranges)
tree := NewIPv4Tree()
for _, r := range ranges {
tree.Add(r)
}

gotRanges := tree.GetAllRanges()
gotCdirs := rangesToCdirs(gotRanges)
slices.Sort(gotCdirs)
gotCidrs := rangesToCidrs(gotRanges)
slices.Sort(gotCidrs)
slices.Sort(tt.expected)
if !slices.Equal(gotCdirs, tt.expected) {
t.Errorf("[%s] Got %+v, Expected: %+v", tt.name, gotCdirs, tt.expected)
if !slices.Equal(gotCidrs, tt.expected) {
t.Errorf("[%s] Got %+v, Expected: %+v", tt.name, gotCidrs, tt.expected)
}
}
}

0 comments on commit 1583f6c

Please sign in to comment.