Skip to content

Commit

Permalink
Merge pull request #180 from ipinfo/ahmad/BE-2211
Browse files Browse the repository at this point in the history
CLI: ipinfo tool prefix Masked / Bits / IsValid
  • Loading branch information
UmanShahzad authored Oct 4, 2023
2 parents 3f1b10a + 6a4160f commit 5040fa4
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ipinfo/cmd_tool_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (

var completionsToolPrefix = &complete.Command{
Sub: map[string]*complete.Command{
"addr": completionsToolPrefixAddr,
"addr": completionsToolPrefixAddr,
"bits": completionsToolPrefixBits,
"masked": completionsToolPrefixMasked,
"is_valid": completionsToolPrefixIsValid,
},
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
Expand All @@ -24,7 +27,10 @@ func printHelpToolPrefix() {
`Usage: %s tool prefix <cmd> [<opts>] [<args>]
Commands:
addr returns the base IP address of a prefix.
addr returns the base IP address of a prefix.
bits returns the length of a prefix and reports -1 if invalid.
masked returns canonical form of a prefix, masking off non-high bits, and returns the zero if invalid.
is_valid reports whether a prefix is valid.
Options:
--help, -h
Expand Down Expand Up @@ -55,6 +61,12 @@ func cmdToolPrefix() error {
switch {
case cmd == "addr":
err = cmdToolPrefixAddr()
case cmd == "bits":
err = cmdToolPrefixBits()
case cmd == "masked":
err = cmdToolPrefixMasked()
case cmd == "is_valid":
err = cmdToolPrefixIsValid()
default:
err = toolPrefixHelp()
}
Expand Down
49 changes: 49 additions & 0 deletions ipinfo/cmd_tool_prefix_bits.go
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)
}
49 changes: 49 additions & 0 deletions ipinfo/cmd_tool_prefix_isValid.go
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)
}
49 changes: 49 additions & 0 deletions ipinfo/cmd_tool_prefix_masked.go
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)
}
41 changes: 41 additions & 0 deletions lib/cmd_tool_prefix_bits.go
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)
}
41 changes: 41 additions & 0 deletions lib/cmd_tool_prefix_masked.go
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)
}
41 changes: 41 additions & 0 deletions lib/cmd_tool_prefix_valid.go
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)
}

0 comments on commit 5040fa4

Please sign in to comment.