Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

printing ipblock as list of ranges #55

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/ipblock/ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (b *IPBlock) Copy() *IPBlock {
return &IPBlock{ipRange: b.ipRange.Copy()}
}

func (b *IPBlock) ipCount() int {
func (b *IPBlock) IPCount() int {
return int(b.ipRange.CalculateSize())
}

Expand Down Expand Up @@ -170,7 +170,7 @@ func DisjointIPBlocks(set1, set2 []*IPBlock) []*IPBlock {
}
// sort ipbList by ip_count per ipblock
sort.Slice(ipbList, func(i, j int) bool {
return ipbList[i].ipCount() < ipbList[j].ipCount()
return ipbList[i].IPCount() < ipbList[j].IPCount()
})
// making sure the resulting list does not contain overlapping ipBlocks
res := []*IPBlock{}
Expand Down Expand Up @@ -303,6 +303,7 @@ func (b *IPBlock) ToCidrListString() string {
}

// ListToPrint: returns a uniform to print list s.t. each element contains either a single cidr or an ip range
// todo - remove this func, and put its code under ToRangesList()
func (b *IPBlock) ListToPrint() []string {
cidrsIPRangesList := []string{}
for _, ipRange := range b.ipRange.Intervals() {
Expand All @@ -316,6 +317,14 @@ func (b *IPBlock) ListToPrint() []string {
return cidrsIPRangesList
}

func (b *IPBlock) ToRangesList() []string {
return b.ListToPrint()
}

func (b *IPBlock) ToRangesListString() string {
return strings.Join(b.ToRangesList(), commaSeparator)
}

// ToIPAdressString returns the IP Address string for this IPBlock
func (b *IPBlock) ToIPAddressString() string {
if b.ipRange.IsSingleNumber() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/ipblock/ipblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestConversions(t *testing.T) {
require.Nil(t, err)
require.Equal(t, ipb1.ToCidrListString(), ipb2.ToCidrListString())

toPrint := ipb1.ListToPrint()
toPrint := ipb1.ToRangesList()
require.Len(t, toPrint, 1)
require.Equal(t, ipRange, toPrint[0])

Expand Down Expand Up @@ -90,12 +90,12 @@ func TestDisjointIPBlocks(t *testing.T) {
func TestPairCIDRsToIPBlocks(t *testing.T) {
first, second, err := ipblock.PairCIDRsToIPBlocks("5.6.7.8/24", "4.9.2.1/32")
require.Nil(t, err)
require.Equal(t, "5.6.7.0/24", first.ListToPrint()[0])
require.Equal(t, "4.9.2.1/32", second.ListToPrint()[0])
require.Equal(t, "5.6.7.0/24", first.ToRangesList()[0])
require.Equal(t, "4.9.2.1/32", second.ToRangesList()[0])

intersect := first.Intersect(second)
require.Equal(t, "", intersect.ToIPAddressString())
require.Empty(t, intersect.ListToPrint())
require.Empty(t, intersect.ToRangesList())
require.Empty(t, intersect.ToCidrListString())
}

Expand Down