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

Lower & upper IP tool #155

Merged
merged 11 commits into from
Aug 22, 2023
Merged

Conversation

Kashaf-Sajid-Yaseen
Copy link
Contributor

Updated cmd_tool.go file in ipinfo folder. Additionally created cmd_tool_lower.go & cmd_tool_upper.go files in ipinfo and lib folders. To get start and end IP addresses of input IP entered by the user.

Copy link
Contributor

@awaismslm awaismslm left a comment

Choose a reason for hiding this comment

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

There is a lot of useless code. Please optimize the code.

Copy link
Contributor

@awaismslm awaismslm left a comment

Choose a reason for hiding this comment

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

Auto complete is also not working. Please check it too

ipinfo/cmd_tool_lower.go Show resolved Hide resolved
ipinfo/cmd_tool_lower.go Show resolved Hide resolved
ipinfo/cmd_tool_upper.go Show resolved Hide resolved

import (
"fmt"

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

lib/cmd_tool_lower.go Show resolved Hide resolved
lib/common.go Outdated
@@ -0,0 +1,35 @@
package lib
Copy link
Contributor

Choose a reason for hiding this comment

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

Please rename this file. You might name it utils.go. we can add more utility functions there in future.

Copy link
Contributor

Choose a reason for hiding this comment

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

As these functions are tool related So its better to move them in cmd_tool.go
(Please ignore above)

lib/common.go Outdated
return err
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

lib/cmd_tool_lower.go Show resolved Hide resolved
lib/cmd_tool_upper.go Show resolved Hide resolved
lib/cmd_tool_lower.go Show resolved Hide resolved
@UmanShahzad UmanShahzad changed the title from newbranch Lower & upper IP tool Jul 28, 2023
Copy link
Contributor

@awaismslm awaismslm left a comment

Choose a reason for hiding this comment

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

Great work. We are almost done. Just a few things.
Also, please update the printHelp of cmd_tool to include the help message of upper and lower.

Copy link
Contributor

Choose a reason for hiding this comment

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

We do not commit the binaries. Please remove it.

lib/cmd_tool_lower.go Show resolved Hide resolved
printHelp()
return nil
}
stat, _ := os.Stdin.Stat()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
stat, _ := os.Stdin.Stat()
stat, _ := os.Stdin.Stat()

}
stat, _ := os.Stdin.Stat()
isStdin := (stat.Mode() & os.ModeCharDevice) == 0
if len(args) == 0 && !isStdin {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if len(args) == 0 && !isStdin {
if len(args) == 0 && !isStdin {

if !f.Quiet {
fmt.Printf("Error parsing CIDR: %v\n", err)
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we return the error instead of nil?

ipinfo/ipinfo Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

same, no binaries

Copy link
Contributor

@awaismslm awaismslm left a comment

Choose a reason for hiding this comment

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

Just a small change and lets do the final review.

@@ -26,7 +28,8 @@ func printHelpTool() {

Commands:
aggregate aggregate IPs, IP ranges, and CIDRs.

lower lower CIDRs(return the starting IP addresses of entered input).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
lower lower CIDRs(return the starting IP addresses of entered input).
lower get start IP of CIDR

@@ -26,7 +28,8 @@ func printHelpTool() {

Commands:
aggregate aggregate IPs, IP ranges, and CIDRs.

lower lower CIDRs(return the starting IP addresses of entered input).
upper upper CIDRs(return the ending IP addresses of entered input).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
upper upper CIDRs(return the ending IP addresses of entered input).
upper get end IP of CIDR

@awaismslm
Copy link
Contributor

@UmanShahzad Please do the final review. I am done from my side.

Copy link
Contributor

@UmanShahzad UmanShahzad left a comment

Choose a reason for hiding this comment

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

Why did we only accept CIDRs and not ranges too? #113 says ranges too.

Also this doesn't seem generic enough from the input side still. See lib/ip_list_write.go#IPListWriteFrom to understand the level of abstraction we need here. You can take that function and create an analogue that writes the lower/upper IPs. What's even better than copy/paste though which I would much more prefer, is creating a generic version of that function which accepts a function as input that will do some generic action on the IPs; then we could use this for all of our use cases here, even the original one in IPListWriteFrom.

ipinfo/cmd_tool.go Outdated Show resolved Hide resolved
lib/cmd_tool_lower.go Show resolved Hide resolved
lib/cmd_tool_upper.go Show resolved Hide resolved
@Kashaf-Sajid-Yaseen
Copy link
Contributor Author

@UmanShahzad Please check

Copy link
Contributor

@UmanShahzad UmanShahzad left a comment

Choose a reason for hiding this comment

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

This isn't using the generic algorithm as explained above

ipinfo/cmd_tool.go Outdated Show resolved Hide resolved
ipinfo/cmd_tool.go Outdated Show resolved Hide resolved
@@ -27,6 +29,8 @@ func printHelpTool() {
Commands:
aggregate aggregate IPs, IP ranges, and CIDRs.

lower get start IP of IP, IP ranges, and CIDRs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
lower get start IP of IP, IP ranges, and CIDRs.
lower get start IP of IPs, IP ranges, and CIDRs.

@@ -27,6 +29,8 @@ func printHelpTool() {
Commands:
aggregate aggregate IPs, IP ranges, and CIDRs.

lower get start IP of IP, IP ranges, and CIDRs.
upper get end IP of IP, IP ranges, and CIDRs.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
upper get end IP of IP, IP ranges, and CIDRs.
upper get end IP of IPs, IP ranges, and CIDRs.


func printHelpToolLower() {
fmt.Printf(
`Usage: %s tool lower [<opts>] <cidr>
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 not just <cidr> now

Copy link
Contributor

Choose a reason for hiding this comment

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

Also we allow files too now right?


func printHelpToolUpper() {
fmt.Printf(
`Usage: %s tool upper [<opts>] <cidr>
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 not just <cidr> now

Input can be IP, IP range or CIDR.

If input contains CIDRs, it calculates the lower IP address for each CIDR.
If input contains IP ranges, it calculates the lower IP address for each Ip range.
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove these above 2 description lines, they're redundant


Description:
Calculates the lower IP address (start address of a network) for the given inputs.
Input can be IP, IP range or CIDR.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Input can be IP, IP range or CIDR.
Inputs can be a mixture of IPs, IP ranges or CIDRs.

Input can be Ip, IP range or CIDR.

If input contains CIDRs, it calculates the upper IP address for each CIDR.
If input contains Ip ranges, it calculates the upper IP address for each IP range.
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove these above 2 description lines, they're redundant


Description:
Calculates the upper IP address (end address of a network) for the given inputs.
Input can be Ip, IP range or CIDR.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Input can be Ip, IP range or CIDR.
Inputs can be a mixture of IPs, IP ranges or CIDRs.

@Kashaf-Sajid-Yaseen
Copy link
Contributor Author

Please check @UmanShahzad, I have updated the above files.

Copy link
Contributor

@UmanShahzad UmanShahzad left a comment

Choose a reason for hiding this comment

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

This is still not right. @awaismslm @Kashaf-Sajid-Yaseen

@UmanShahzad
Copy link
Contributor

@Kashaf-Sajid-Yaseen lemme know when ready?

Copy link
Contributor

@awaismslm awaismslm left a comment

Choose a reason for hiding this comment

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

@UmanShahzad Please take a look if the approach is correct. I can see that we have to make the generic functions for actions: not specific to upper and lower. Other than that the approach seems fine. I'll check the other code.

@UmanShahzad
Copy link
Contributor

So @Harisabdullah is developing a generic solution for that which he'll use in #159 and which we can use here too. Let us hold out until that is done then we can use that solution here.

I just checked this one and it's not too bad either, but @Harisabdullah will have one with a better interface (the one used here is the one I wrote very rapidly in pseudocode).

@UmanShahzad
Copy link
Contributor

@awaismslm / @Kashaf-Sajid-Yaseen you can check the latest abstraction used in #159 for this, and use that for all tools now.

@awaismslm
Copy link
Contributor

Looks good to me. @Kashaf-Sajid-Yaseen Please resolve the conflicts.

@awaismslm
Copy link
Contributor

@UmanShahzad Please take a look. It looks good to me.

$ cat /path/to/file.txt | %[1]s tool lower

# Find lower IPs from file.
$ %[1]s tool lower /path/to/file1.txt
Copy link
Contributor

Choose a reason for hiding this comment

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

Need proper spacing for all of these examples (2 chars)

}

func ActionForIP(input string) {
ip := net.ParseIP(input)
Copy link
Contributor

Choose a reason for hiding this comment

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

It is already in a parsed state and valid - just print it, no need to parse this.

fmt.Println(err)
return
}
fmt.Println(ipnet.IP) // Print the start IP of the CIDR
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the trailing comment

lib/cmd_tool_lower.go Show resolved Hide resolved
lib/cmd_tool_lower.go Show resolved Hide resolved
lib/cmd_tool_upper.go Show resolved Hide resolved
fmt.Println(err)
return
}
fmt.Println(ipRange.End) // Print the end IP of the range
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the trailing comment

fmt.Println(err)
return
}
fmt.Println(ipRange.End) // Print the end IP of the CIDR
Copy link
Contributor

Choose a reason for hiding this comment

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

No need for the trailing comment

}

func ActionForCIDRUpper(input string) {
_, ipnet, err := net.ParseCIDR(input)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just pass the input directly into IPRangeStrFromCIDR, because at this point we know it's a valid CIDR.

}

func ActionForIPUpper(input string) {
ip := net.ParseIP(input)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to parse

@Kashaf-Sajid-Yaseen
Copy link
Contributor Author

@UmanShahzad

@UmanShahzad UmanShahzad merged commit dd5467b into ipinfo:master Aug 22, 2023
This was linked to issues Aug 28, 2023
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.

IP tool: CIDR/range end IP IP tool: CIDR/range start IP
3 participants