-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from ipinfo/ahmad/BE-2211
CLI: ipinfo tool prefix Masked / Bits / IsValid
- Loading branch information
Showing
7 changed files
with
284 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,49 @@ | ||
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 completionsToolPrefixBits = &complete.Command{ | ||
Flags: map[string]complete.Predictor{ | ||
"-h": predict.Nothing, | ||
"--help": predict.Nothing, | ||
}, | ||
} | ||
|
||
func printHelpToolPrefixBits() { | ||
fmt.Printf( | ||
`Usage: %s tool prefix bits <cidr> | ||
Description: | ||
Returns the length of a prefix and reports -1 if invalid. | ||
Examples: | ||
# CIDR Valid Examples. | ||
$ %[1]s tool prefix bits 192.168.0.0/16 | ||
$ %[1]s tool prefix bits 10.0.0.0/8 | ||
$ %[1]s tool prefix bits 2001:0db8:1234::/48 | ||
$ %[1]s tool prefix bits 2606:2800:220:1::/64 | ||
# CIDR Invalid Examples. | ||
$ %[1]s tool prefix bits 192.168.0.0/40 | ||
$ %[1]s tool prefix bits 2001:0db8:1234::/129 | ||
Options: | ||
--help, -h | ||
show help. | ||
`, progBase) | ||
} | ||
|
||
func cmdToolPrefixBits() (err error) { | ||
f := lib.CmdToolPrefixBitsFlags{} | ||
f.Init() | ||
pflag.Parse() | ||
|
||
return lib.CmdToolPrefixBits(f, pflag.Args()[3:], printHelpToolPrefixBits) | ||
} |
This file contains 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,49 @@ | ||
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 completionsToolPrefixIsValid = &complete.Command{ | ||
Flags: map[string]complete.Predictor{ | ||
"-h": predict.Nothing, | ||
"--help": predict.Nothing, | ||
}, | ||
} | ||
|
||
func printHelpToolPrefixIsValid() { | ||
fmt.Printf( | ||
`Usage: %s tool prefix is_valid <cidr> | ||
Description: | ||
Reports whether a prefix is valid. | ||
Examples: | ||
# CIDR Valid Examples. | ||
$ %[1]s tool prefix is_valid 192.168.0.0/16 | ||
$ %[1]s tool prefix is_valid 10.0.0.0/8 | ||
$ %[1]s tool prefix is_valid 2001:0db8:1234::/48 | ||
$ %[1]s tool prefix is_valid 2606:2800:220:1::/64 | ||
# CIDR Invalid Examples. | ||
$ %[1]s tool prefix is_valid 192.168.0.0/40 | ||
$ %[1]s tool prefix is_valid 2001:0db8:1234::/129 | ||
Options: | ||
--help, -h | ||
show help. | ||
`, progBase) | ||
} | ||
|
||
func cmdToolPrefixIsValid() (err error) { | ||
f := lib.CmdToolPrefixIsValidFlags{} | ||
f.Init() | ||
pflag.Parse() | ||
|
||
return lib.CmdToolPrefixIsValid(f, pflag.Args()[3:], printHelpToolPrefixIsValid) | ||
} |
This file contains 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,49 @@ | ||
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 completionsToolPrefixMasked = &complete.Command{ | ||
Flags: map[string]complete.Predictor{ | ||
"-h": predict.Nothing, | ||
"--help": predict.Nothing, | ||
}, | ||
} | ||
|
||
func printHelpToolPrefixMasked() { | ||
fmt.Printf( | ||
`Usage: %s tool prefix masked <cidr> | ||
Description: | ||
Returns canonical form of a prefix, masking off non-high bits, and returns the zero if invalid. | ||
Examples: | ||
# CIDR Valid Examples. | ||
$ %[1]s tool prefix masked 192.168.0.0/16 | ||
$ %[1]s tool prefix masked 10.0.0.0/8 | ||
$ %[1]s tool prefix masked 2001:0db8:1234::/48 | ||
$ %[1]s tool prefix masked 2606:2800:220:1::/64 | ||
# CIDR Invalid Examples. | ||
$ %[1]s tool prefix masked 192.168.0.0/40 | ||
$ %[1]s tool prefix masked 2001:0db8:1234::/129 | ||
Options: | ||
--help, -h | ||
show help. | ||
`, progBase) | ||
} | ||
|
||
func cmdToolPrefixMasked() (err error) { | ||
f := lib.CmdToolPrefixMaskedFlags{} | ||
f.Init() | ||
pflag.Parse() | ||
|
||
return lib.CmdToolPrefixMasked(f, pflag.Args()[3:], printHelpToolPrefixMasked) | ||
} |
This file contains 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,41 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type CmdToolPrefixBitsFlags struct { | ||
Help bool | ||
} | ||
|
||
func (f *CmdToolPrefixBitsFlags) Init() { | ||
pflag.BoolVarP( | ||
&f.Help, | ||
"help", "h", false, | ||
"show help.", | ||
) | ||
} | ||
|
||
func CmdToolPrefixBits(f CmdToolPrefixBitsFlags, args []string, printHelp func()) error { | ||
if f.Help { | ||
printHelp() | ||
return nil | ||
} | ||
|
||
op := func(input string, inputType INPUT_TYPE) error { | ||
switch inputType { | ||
case INPUT_TYPE_CIDR: | ||
prefix, err := netip.ParsePrefix(input) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%s,%d\n", input, prefix.Bits()) | ||
} | ||
return nil | ||
} | ||
|
||
return GetInputFrom(args, true, true, op) | ||
} |
This file contains 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,41 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type CmdToolPrefixMaskedFlags struct { | ||
Help bool | ||
} | ||
|
||
func (f *CmdToolPrefixMaskedFlags) Init() { | ||
pflag.BoolVarP( | ||
&f.Help, | ||
"help", "h", false, | ||
"show help.", | ||
) | ||
} | ||
|
||
func CmdToolPrefixMasked(f CmdToolPrefixMaskedFlags, args []string, printHelp func()) error { | ||
if f.Help { | ||
printHelp() | ||
return nil | ||
} | ||
|
||
op := func(input string, inputType INPUT_TYPE) error { | ||
switch inputType { | ||
case INPUT_TYPE_CIDR: | ||
prefix, err := netip.ParsePrefix(input) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%s,%s\n", input, prefix.Masked()) | ||
} | ||
return nil | ||
} | ||
|
||
return GetInputFrom(args, true, true, op) | ||
} |
This file contains 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,41 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type CmdToolPrefixIsValidFlags struct { | ||
Help bool | ||
} | ||
|
||
func (f *CmdToolPrefixIsValidFlags) Init() { | ||
pflag.BoolVarP( | ||
&f.Help, | ||
"help", "h", false, | ||
"show help.", | ||
) | ||
} | ||
|
||
func CmdToolPrefixIsValid(f CmdToolPrefixIsValidFlags, args []string, printHelp func()) error { | ||
if f.Help { | ||
printHelp() | ||
return nil | ||
} | ||
|
||
op := func(input string, inputType INPUT_TYPE) error { | ||
switch inputType { | ||
case INPUT_TYPE_CIDR: | ||
prefix, err := netip.ParsePrefix(input) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("%s,%t\n", input, prefix.IsValid()) | ||
} | ||
return nil | ||
} | ||
|
||
return GetInputFrom(args, true, true, op) | ||
} |