-
Notifications
You must be signed in to change notification settings - Fork 160
Lower & upper IP tool #155
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
Conversation
There was a problem hiding this 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.
There was a problem hiding this 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
lib/cmd_tool_lower.go
Outdated
|
||
import ( | ||
"fmt" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lib/common.go
Outdated
@@ -0,0 +1,35 @@ | |||
package lib |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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.
ipinfo/build/ipinfo
Outdated
There was a problem hiding this comment.
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_upper.go
Outdated
printHelp() | ||
return nil | ||
} | ||
stat, _ := os.Stdin.Stat() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stat, _ := os.Stdin.Stat() | |
stat, _ := os.Stdin.Stat() |
lib/cmd_tool_upper.go
Outdated
} | ||
stat, _ := os.Stdin.Stat() | ||
isStdin := (stat.Mode() & os.ModeCharDevice) == 0 | ||
if len(args) == 0 && !isStdin { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if len(args) == 0 && !isStdin { | |
if len(args) == 0 && !isStdin { |
lib/cmd_tool_upper.go
Outdated
if !f.Quiet { | ||
fmt.Printf("Error parsing CIDR: %v\n", err) | ||
} | ||
return nil |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same, no binaries
There was a problem hiding this 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.
ipinfo/cmd_tool.go
Outdated
@@ -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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lower lower CIDRs(return the starting IP addresses of entered input). | |
lower get start IP of CIDR |
ipinfo/cmd_tool.go
Outdated
@@ -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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
upper upper CIDRs(return the ending IP addresses of entered input). | |
upper get end IP of CIDR |
@UmanShahzad Please do the final review. I am done from my side. |
There was a problem hiding this 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
.
@UmanShahzad Please check |
There was a problem hiding this 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
@@ -27,6 +29,8 @@ func printHelpTool() { | |||
Commands: | |||
aggregate aggregate IPs, IP ranges, and CIDRs. | |||
|
|||
lower get start IP of IP, IP ranges, and CIDRs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lower get start IP of IP, IP ranges, and CIDRs. | |
lower get start IP of IPs, IP ranges, and CIDRs. |
ipinfo/cmd_tool.go
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
upper get end IP of IP, IP ranges, and CIDRs. | |
upper get end IP of IPs, IP ranges, and CIDRs. |
ipinfo/cmd_tool_lower.go
Outdated
|
||
func printHelpToolLower() { | ||
fmt.Printf( | ||
`Usage: %s tool lower [<opts>] <cidr> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
ipinfo/cmd_tool_upper.go
Outdated
|
||
func printHelpToolUpper() { | ||
fmt.Printf( | ||
`Usage: %s tool upper [<opts>] <cidr> |
There was a problem hiding this comment.
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
ipinfo/cmd_tool_lower.go
Outdated
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. |
There was a problem hiding this comment.
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
ipinfo/cmd_tool_lower.go
Outdated
|
||
Description: | ||
Calculates the lower IP address (start address of a network) for the given inputs. | ||
Input can be IP, IP range or CIDR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Input can be IP, IP range or CIDR. | |
Inputs can be a mixture of IPs, IP ranges or CIDRs. |
ipinfo/cmd_tool_upper.go
Outdated
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. |
There was a problem hiding this comment.
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
ipinfo/cmd_tool_upper.go
Outdated
|
||
Description: | ||
Calculates the upper IP address (end address of a network) for the given inputs. | ||
Input can be Ip, IP range or CIDR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Input can be Ip, IP range or CIDR. | |
Inputs can be a mixture of IPs, IP ranges or CIDRs. |
Please check @UmanShahzad, I have updated the above files. |
There was a problem hiding this 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
@Kashaf-Sajid-Yaseen lemme know when ready? |
There was a problem hiding this 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.
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). |
@awaismslm / @Kashaf-Sajid-Yaseen you can check the latest abstraction used in #159 for this, and use that for all tools now. |
Looks good to me. @Kashaf-Sajid-Yaseen Please resolve the conflicts. |
@UmanShahzad Please take a look. It looks good to me. |
ipinfo/cmd_tool_lower.go
Outdated
$ cat /path/to/file.txt | %[1]s tool lower | ||
|
||
# Find lower IPs from file. | ||
$ %[1]s tool lower /path/to/file1.txt |
There was a problem hiding this comment.
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)
lib/cmd_tool_lower.go
Outdated
} | ||
|
||
func ActionForIP(input string) { | ||
ip := net.ParseIP(input) |
There was a problem hiding this comment.
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.
lib/cmd_tool_lower.go
Outdated
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println(ipnet.IP) // Print the start IP of the CIDR |
There was a problem hiding this comment.
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_upper.go
Outdated
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println(ipRange.End) // Print the end IP of the range |
There was a problem hiding this comment.
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_upper.go
Outdated
fmt.Println(err) | ||
return | ||
} | ||
fmt.Println(ipRange.End) // Print the end IP of the CIDR |
There was a problem hiding this comment.
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_upper.go
Outdated
} | ||
|
||
func ActionForCIDRUpper(input string) { | ||
_, ipnet, err := net.ParseCIDR(input) |
There was a problem hiding this comment.
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.
lib/cmd_tool_upper.go
Outdated
} | ||
|
||
func ActionForIPUpper(input string) { | ||
ip := net.ParseIP(input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to parse
b0fdf7d
to
bf69be3
Compare
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.