-
Notifications
You must be signed in to change notification settings - Fork 204
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8a9bcc6
from newbranch
134ceac
Code is Updated
b0eabf2
Files updated after second Review
beab743
Files updated after Third Review
73c38da
File updated after fourth Review
4e7eaae
Files updated after Fouth Review
b6221c4
Files updated after Fifth Review
437b62a
Files updated after Sixth Review
bf69be3
Updated with generic function
692a20b
Updated
7b82eae
Merge branch 'master' into newbranch
Kashaf-Sajid-Yaseen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/ipinfo/cli/lib" | ||
| "github.com/ipinfo/cli/lib/complete" | ||
| "github.com/ipinfo/cli/lib/complete/predict" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| var completionsToolLower = &complete.Command{ | ||
| Flags: map[string]complete.Predictor{ | ||
| "-h": predict.Nothing, | ||
| "--help": predict.Nothing, | ||
| "-q": predict.Nothing, | ||
| "--quiet": predict.Nothing, | ||
| }, | ||
| } | ||
|
|
||
| func printHelpToolLower() { | ||
| fmt.Printf( | ||
| `Usage: %s tool lower [<opts>] <cidr | ip | ip-range | filepath> | ||
|
|
||
| Description: | ||
|
UmanShahzad marked this conversation as resolved.
|
||
| Calculates the lower IP address (start address of a network) for the given inputs. | ||
| Input can be a mixture of IPs, IP ranges or CIDRs. | ||
|
|
||
| Examples: | ||
| # Finds lower IP for CIDR. | ||
| $ %[1]s tool lower 192.168.1.0/24 | ||
|
|
||
| # Finds lower IP for IP range. | ||
| $ %[1]s tool lower 1.1.1.0-1.1.1.244 | ||
|
|
||
| # Finds lower IPs from stdin. | ||
| $ cat /path/to/file.txt | %[1]s tool lower | ||
|
|
||
| # Find lower IPs from file. | ||
| $ %[1]s tool lower /path/to/file1.txt | ||
|
|
||
| Options: | ||
| --help, -h | ||
| show help. | ||
| --quiet, -q | ||
| quiet mode; suppress additional output. | ||
| `, progBase) | ||
| } | ||
|
|
||
| func cmdToolLower() (err error) { | ||
| f := lib.CmdToolLowerFlags{} | ||
| f.Init() | ||
| pflag.Parse() | ||
|
|
||
| return lib.CmdToolLower(f, pflag.Args()[2:], printHelpToolLower) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/ipinfo/cli/lib" | ||
| "github.com/ipinfo/cli/lib/complete" | ||
| "github.com/ipinfo/cli/lib/complete/predict" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| var completionsToolUpper = &complete.Command{ | ||
| Flags: map[string]complete.Predictor{ | ||
| "-h": predict.Nothing, | ||
| "--help": predict.Nothing, | ||
| "-q": predict.Nothing, | ||
| "--quiet": predict.Nothing, | ||
| }, | ||
| } | ||
|
|
||
| func printHelpToolUpper() { | ||
| fmt.Printf( | ||
| `Usage: %s tool upper [<opts>] <cidr | ip | ip-range | filepath> | ||
|
UmanShahzad marked this conversation as resolved.
|
||
|
|
||
| Description: | ||
| Calculates the upper IP address (end address of a network) for the given inputs. | ||
| Input can be a mixture of Ips, IP ranges or CIDRs. | ||
|
|
||
| Examples: | ||
| # Finds upper IP for CIDR. | ||
| $ %[1]s tool upper 192.168.1.0/24 | ||
|
|
||
| # Finds upper IP for IP range. | ||
| $ %[1]s tool upper 1.1.1.0-1.1.1.244 | ||
|
|
||
| # Finds upper IPs from stdin. | ||
| $ cat /path/to/file.txt | %[1]s tool upper | ||
|
|
||
| # Find upper IPs from file. | ||
| $ %[1]s tool upper /path/to/file1.txt | ||
|
|
||
| Options: | ||
| --help, -h | ||
| Show help. | ||
| --quiet, -q | ||
| Quiet mode; suppress additional output. | ||
| `, progBase) | ||
| } | ||
|
|
||
| func cmdToolUpper() (err error) { | ||
| f := lib.CmdToolUpperFlags{} | ||
| f.Init() | ||
| pflag.Parse() | ||
|
|
||
| return lib.CmdToolUpper(f, pflag.Args()[2:], printHelpToolUpper) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package lib | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
|
|
||
|
UmanShahzad marked this conversation as resolved.
|
||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| type CmdToolLowerFlags struct { | ||
| Help bool | ||
| Quiet bool | ||
|
UmanShahzad marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func (f *CmdToolLowerFlags) Init() { | ||
| pflag.BoolVarP( | ||
| &f.Help, | ||
| "help", "h", false, | ||
| "show help.", | ||
| ) | ||
| pflag.BoolVarP( | ||
| &f.Quiet, | ||
| "quiet", "q", false, | ||
| "quiet mode; suppress additional output.", | ||
| ) | ||
| } | ||
|
|
||
| func CmdToolLower( | ||
| f CmdToolLowerFlags, | ||
| args []string, | ||
| printHelp func(), | ||
| ) error { | ||
| if f.Help { | ||
| printHelp() | ||
| return nil | ||
| } | ||
|
UmanShahzad marked this conversation as resolved.
|
||
|
|
||
| actionFunc := func(input string, inputType INPUT_TYPE) error { | ||
| switch inputType { | ||
| case INPUT_TYPE_IP: | ||
| ActionForIP(input) | ||
| case INPUT_TYPE_IP_RANGE: | ||
| ActionForRange(input) | ||
| case INPUT_TYPE_CIDR: | ||
| ActionForCIDR(input) | ||
| default: | ||
| return ErrNotIP | ||
| } | ||
| return nil | ||
| } | ||
| err := GetInputFrom(args, true, true, actionFunc) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| } | ||
|
UmanShahzad marked this conversation as resolved.
|
||
|
|
||
|
UmanShahzad marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
|
|
||
|
UmanShahzad marked this conversation as resolved.
|
||
| func ActionForIP(input string) { | ||
| fmt.Println(input) | ||
| } | ||
|
|
||
| func ActionForRange(input string) { | ||
| ipRange, err := IPRangeStrFromStr(input) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| return | ||
| } | ||
| fmt.Println(ipRange.Start) | ||
| } | ||
|
|
||
| func ActionForCIDR(input string) { | ||
| _, ipnet, err := net.ParseCIDR(input) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| return | ||
| } | ||
| fmt.Println(ipnet.IP) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package lib | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
|
UmanShahzad marked this conversation as resolved.
|
||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| type CmdToolUpperFlags struct { | ||
| Help bool | ||
| Quiet bool | ||
| } | ||
|
|
||
| func (f *CmdToolUpperFlags) Init() { | ||
| pflag.BoolVarP( | ||
| &f.Help, | ||
| "help", "h", false, | ||
| "show help.", | ||
| ) | ||
| pflag.BoolVarP( | ||
| &f.Quiet, | ||
| "quiet", "q", false, | ||
| "quiet mode; suppress additional output.", | ||
| ) | ||
| } | ||
|
|
||
| func CmdToolUpper( | ||
| f CmdToolUpperFlags, | ||
| args []string, | ||
| printHelp func(), | ||
| ) error { | ||
| if f.Help { | ||
| printHelp() | ||
| return nil | ||
| } | ||
|
UmanShahzad marked this conversation as resolved.
|
||
|
|
||
|
UmanShahzad marked this conversation as resolved.
|
||
| actionFunc := func(input string, inputType INPUT_TYPE) error { | ||
| switch inputType { | ||
| case INPUT_TYPE_IP: | ||
| ActionForIPUpper(input) | ||
| case INPUT_TYPE_IP_RANGE: | ||
| ActionForRangeUpper(input) | ||
| case INPUT_TYPE_CIDR: | ||
| ActionForCIDRUpper(input) | ||
| default: | ||
| return ErrNotIP | ||
| } | ||
| return nil | ||
| } | ||
| err := GetInputFrom(args, true, true, actionFunc) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| } | ||
|
UmanShahzad marked this conversation as resolved.
|
||
|
|
||
|
UmanShahzad marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
|
|
||
| func ActionForIPUpper(input string) { | ||
| fmt.Println(input) | ||
| } | ||
|
|
||
| func ActionForRangeUpper(input string) { | ||
| ipRange, err := IPRangeStrFromStr(input) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| return | ||
| } | ||
| fmt.Println(ipRange.End) | ||
| } | ||
|
|
||
| func ActionForCIDRUpper(input string) { | ||
| ipRange, err := IPRangeStrFromCIDR(input) | ||
| if err != nil { | ||
| fmt.Println(err) | ||
| return | ||
| } | ||
| fmt.Println(ipRange.End) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.