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

Support range, cidrs in grepip #188

Conversation

talhahwahla
Copy link
Contributor

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
@@ -13,6 +13,12 @@ var completionsGrepIP = &complete.Command{
Flags: map[string]complete.Predictor{
"-o": predict.Nothing,
"--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.

@@ -13,6 +13,12 @@ var completionsGrepIP = &complete.Command{
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

@@ -35,6 +41,14 @@ Options:
General:
--only-matching, -o
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

@@ -35,6 +41,14 @@ Options:
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

@@ -37,6 +41,26 @@ func (f *CmdGrepIPFlags) Init() {
"only-matching", "o", false,
"print only matched IPs in result line.",
)
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

ipV4Regex = regexp.MustCompilePOSIX(IPv4RegexPattern)
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

@@ -121,10 +145,46 @@ func CmdGrepIP(

// 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 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.

2 participants