Skip to content

Conversation

@talhahwahla
Copy link
Contributor

@linear
Copy link

linear bot commented Nov 14, 2023

BE-2716 Introduce flag in grepip to print subnets too

Ref: #185 (comment)

Current:

$ ipinfo grepip list2.txt --only-matching
33.0.112.175
100.0.0.0
8.8.8.8
10.0.0.0

Want:

$ ipinfo grepip list2.txt --only-matching --include-subnet
33.0.112.175/12
100.0.0.0
8.8.8.8-10.0.0.0

cc uman

@talhahwahla
Copy link
Contributor Author

$ cat list.txt 
lorem33.0.112.175/12
100.0.0.0ipsum
8.8.8.8-10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1


$ ./ipinfo grepip list.txt --only-matching 
33.0.112.175
100.0.0.0
8.8.8.8
10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85
2001:db8::1
2001:db8::3
::1
::2
10.0.0.0
101.1.1.1


$ ./ipinfo grepip list.txt --only-matching --include-cidrs
33.0.112.175/12
100.0.0.0
8.8.8.8
10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28
2001:db8::1
2001:db8::3
::1
::2
10.0.0.0
101.1.1.1


$ ./ipinfo grepip list.txt --only-matching --include-ranges
33.0.112.175
100.0.0.0
8.8.8.8-10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1


$ ./ipinfo grepip list.txt --only-matching --include-ranges --include-cidrs
33.0.112.175/12
100.0.0.0
8.8.8.8-10.0.0.0
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1


$ ./ipinfo grepip list.txt -o --cidrs-only
33.0.112.175/12
ed2e:6d1a:dfb:15bf:d7a0:fff9:4e58:bc85/28


$ ./ipinfo grepip list.txt -o --ranges-only
8.8.8.8-10.0.0.0
2001:db8::1-2001:db8::3
::1,::2
10.0.0.0,101.1.1.1

@talhahwahla talhahwahla force-pushed the talhahameed/be-2716-introduce-flag-in-grepip-to-print-subnets-too branch from b67dd12 to 6127d8d Compare November 14, 2023 18:51
"--only-matching": predict.Nothing,
"-c": predict.Nothing,
"--include-cidrs": predict.Nothing,
"-r": predict.Nothing,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use a short flag for this - we tried to copy some common flag naming conventions from normal grep (hence -o), and -r is too easily associated with recursive lookup.

Flags: map[string]complete.Predictor{
"-o": predict.Nothing,
"--only-matching": predict.Nothing,
"-c": predict.Nothing,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, this is too easily associated with count in grep

print only matched IP in result line, excluding surrounding content.
--include-cidrs, -c
prints the CIDRs too.
--include-ranges, -r
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rm -r

General:
--only-matching, -o
print only matched IP in result line, excluding surrounding content.
--include-cidrs, -c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rm -c

)
pflag.BoolVarP(
&f.IncludeCIDRs,
"include-cidrs", "c", false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rm short

ipV6Regex = regexp.MustCompilePOSIX(IPv6RegexPattern)
ipRegex = regexp.MustCompilePOSIX(IPv4RegexPattern + "|" + IPv6RegexPattern)

v4IpCidrRegex = regexp.MustCompilePOSIX(IPv4RegexPattern + "|" + IPv4CIDRRegexPattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment against each grouped code is warranted now

// prepare regexp
var rexp *regexp.Regexp
if ipv == 4 {
if ipv == 4 && f.CIDRsOnly && f.RangesOnly {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we find a grouping that makes this more readable? If nesting ifs helps then let's try that. Needs a judgement

lib/regex.go Outdated
var v6SubnetRegex *regexp.Regexp
var subnetRegex *regexp.Regexp

const octet = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a v4Octet

lib/regex.go Outdated
const IPv4CIDRRegexPattern = IPv4RegexPattern + `\/([1-2]?[0-9]|3[0-2])`

const IPv6RegexPattern = `(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))`
const IPv6RangeRegexPattern = IPv6RegexPattern + `[:space:]*[-,][:space:]*` + IPv6RegexPattern
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the infinitely allowed spaces. Our common definition of ranges has always been without any space separation allowed.

lib/regex.go Outdated

const octet = `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`
const IPv4RegexPattern = `(` + octet + `\.){3}` + octet
const IPv4RangeRegexPattern = IPv4RegexPattern + `[:space:]*[-,][:space:]*` + IPv4RegexPattern
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

@UmanShahzad UmanShahzad merged commit ead14a7 into master Nov 15, 2023
@UmanShahzad UmanShahzad deleted the talhahameed/be-2716-introduce-flag-in-grepip-to-print-subnets-too branch November 15, 2023 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants