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

Issue #186 more basic IP tools #192

Merged
merged 16 commits into from
Nov 28, 2023
88 changes: 58 additions & 30 deletions ipinfo/cmd_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@ import (

var completionsTool = &complete.Command{
Sub: map[string]*complete.Command{
"aggregate": completionsToolAggregate,
"next": completionsToolNext,
"prev": completionsToolPrev,
"is_v4": completionsToolIsV4,
"is_v6": completionsToolIsV6,
"is_valid": completionsToolIsValid,
"is_one_ip": completionsToolIsOneIp,
"unmap": completionsToolUnmap,
"lower": completionsToolLower,
"upper": completionsToolUpper,
"is_v4in6": completionsToolIs4In6,
"ip2n": completionsToolIP2n,
"n2ip": completionsToolN2IP,
"n2ip6": completionsToolN2IP6,
"prefix": completionsToolPrefix,
"aggregate": completionsToolAggregate,
"next": completionsToolNext,
"prev": completionsToolPrev,
"is_v4": completionsToolIsV4,
"is_v6": completionsToolIsV6,
"is_valid": completionsToolIsValid,
"is_one_ip": completionsToolIsOneIp,
"unmap": completionsToolUnmap,
"lower": completionsToolLower,
"upper": completionsToolUpper,
"is_v4in6": completionsToolIs4In6,
"ip2n": completionsToolIP2n,
"n2ip": completionsToolN2IP,
"n2ip6": completionsToolN2IP6,
"prefix": completionsToolPrefix,
"is_loopback": completionsToolIsLoopBack,
"is_multicast": completionsToolIsMulticast,
"is_unspecified": completionsToolIsUnspecified,
"is_global_unicast": completionsToolIsGlobalUnicast,
"is_link_local_unicast": completionsToolIsLinkLocalUnicast,
"is_link_local_multicast": completionsToolIsLinkLocalMulticast,
"is_interface_local_multicast":completionsToolIsInterfaceLocalMulticast,
shamirShahzad marked this conversation as resolved.
Show resolved Hide resolved
},
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
Expand All @@ -39,21 +46,28 @@ func printHelpTool() {
`Usage: %s tool <cmd> [<opts>] [<args>]

Commands:
aggregate aggregate IPs, IP ranges, and CIDRs.
next get the next IP of the input IP
prev get the previous IP of the input IP
is_v4 reports whether input is an IPv4 address.
is_v6 reports whether input is an IPv6 address.
is_valid reports whether an IP is valid.
is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
lower get start IP of IPs, IP ranges, and CIDRs.
upper get end IP of IPs, IP ranges, and CIDRs.
is_v4in6 get whether the IP is an IPv4-mapped IPv6 address.
ip2n converts an IPv4 or IPv6 address to its decimal representation.
n2ip evaluates a mathematical expression and converts it to an IPv4 or IPv6.
n2ip6 evaluates a mathematical expression and converts it to an IPv6.
prefix misc. prefix tools related to CIDRs.
aggregate aggregate IPs, IP ranges, and CIDRs.
next get the next IP of the input IP
prev get the previous IP of the input IP
is_v4 reports whether input is an IPv4 address.
is_v6 reports whether input is an IPv6 address.
is_valid reports whether an IP is valid.
is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
lower get start IP of IPs, IP ranges, and CIDRs.
upper get end IP of IPs, IP ranges, and CIDRs.
is_v4in6 get whether the IP is an IPv4-mapped IPv6 address.
ip2n converts an IPv4 or IPv6 address to its decimal representation.
n2ip evaluates a mathematical expression and converts it to an IPv4 or IPv6.
n2ip6 evaluates a mathematical expression and converts it to an IPv6.
prefix misc. prefix tools related to CIDRs.
is_loopback reports whether an IP is a valid Loopback address.
is_multicast reports whether an IP is a valid Multicast address.
is_unspecified reports whether an IP is an Unspecified address.
is_global_unicast reports whether an IP is a global unicast address.
is_link_local_unicast reports whether IP is a Link Local Unicast
is_link_local_multicast reports whether IP is a Link Local Multicast address
is_interface_local_multicast reports whether an IP is a interface local multicast
shamirShahzad marked this conversation as resolved.
Show resolved Hide resolved

Options:
--help, -h
Expand Down Expand Up @@ -96,6 +110,20 @@ func cmdTool() error {
err = cmdToolIsValid()
case cmd == "is_one_ip":
err = cmdToolIsOneIp()
case cmd == "is_loopback":
err = cmdToolIsLoopBack()
case cmd == "is_multicast":
err = cmdToolIsMultiCast()
case cmd == "is_unspecified":
err = cmdToolIsUnspecified()
case cmd == "is_global_unicast":
err = cmdToolisGlobalUnicast()
case cmd == "is_interface_local_multicast":
err = cmdToolIsInterfaceLocalMulticast()
case cmd == "is_link_local_multicast":
err = cmdToolIsLinkLocalMulticast()
case cmd == "is_link_local_unicast":
err = cmdToolIsLinkLocalUnicast()
shamirShahzad marked this conversation as resolved.
Show resolved Hide resolved
case cmd == "unmap":
err = cmdToolUnmap()
case cmd == "lower":
Expand Down
56 changes: 56 additions & 0 deletions ipinfo/cmd_tool_is_global_unicast.go
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 completionsToolIsGlobalUnicast = &complete.Command{
Flags: map[string]complete.Predictor{
"-h":predict.Nothing,
"--help":predict.Nothing,
"-q":predict.Nothing,
"--quiet":predict.Nothing,
},
}

func printHelpToolIsGlobalUnicast(){
fmt.Printf(
`Usage: %s tool is_global_unicast [<opts>] <cidr | ip | ip-range | filepath>

Description:
Checks if the input is an globl unicast address.
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file

Examples:
# Check CIDR.
$ %[1]s tool is_global_unicast 1.1.1.0/30

# Check IP range.
$ %[1]s tool is_global_unicast 1.1.1.0-1.1.1.244

# Check for file.
$ %[1]s tool is_global_unicast /path/to/file.txt

# Check entries from stdin.
$ cat /path/to/file1.txt | %[1]s tool is_global_unicast

Options:
--help, -h
show help.
--quiet, -q
quiet mode; suppress additional output.
`, progBase)
}

func cmdToolisGlobalUnicast()(err error){
f:=lib.CmdToolIsGlobalUnicastFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolIsGlobalUnicast(f,pflag.Args()[2:],printHelpToolIsGlobalUnicast)
}
56 changes: 56 additions & 0 deletions ipinfo/cmd_tool_is_interface_local_multicast.go
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 completionsToolIsInterfaceLocalMulticast = &complete.Command{
Flags: map[string]complete.Predictor{
"-h":predict.Nothing,
"--help":predict.Nothing,
"-q":predict.Nothing,
"--quiet":predict.Nothing,
},
}

func printHelpToolIsInterfaceLocalMulticast(){
fmt.Printf(
`Usage: %s tool is_interface_local_multicast [<opts>] <cidr | ip | ip-range | filepath>

Description:
Checks if the input is a Inteface Local Multicast Address.
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file

Examples:
# Check CIDR.
$ %[1]s tool is_interface_local_multicast 1.1.1.0/30

# Check IP range.
$ %[1]s tool is_interface_local_multicast 1.1.1.0-1.1.1.244

# Check for file.
$ %[1]s tool is_interface_local_multicast /path/to/file.txt

# Check entries from stdin.
$ cat /path/to/file1.txt | %[1]s tool is_interface_local_multicast

Options:
--help, -h
show help.
--quiet, -q
quiet mode; suppress additional output.
`, progBase)
}

func cmdToolIsInterfaceLocalMulticast()(err error){
f:=lib.CmdToolIsInterfaceLocalMulticastFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolIsInterfaceLocalMulticast(f,pflag.Args()[2:],printHelpToolIsInterfaceLocalMulticast)
}
58 changes: 58 additions & 0 deletions ipinfo/cmd_tool_is_link_local_multicast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 completionsToolIsLinkLocalMulticast = &complete.Command{
Flags:map[string]complete.Predictor{
"-h":predict.Nothing,
"--help":predict.Nothing,
"-q":predict.Nothing,
"--quiet":predict.Nothing,
},
}

func printHelpToolIsLinkLocalMulticast(){
fmt.Printf(

`
Usage: %s tool is_link_local_multicast [<opts>] <cidr | ip |ip-range| filepath>

Description:
Checks if the input is a Link Local Multicast Address.
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file

Examples:
# Check CIDR.
$ %[1]s tool is_link_local_multicast 1.1.1.0/30

# Check IP range.
$ %[1]s tool is_link_local_multicast 1.1.1.0-1.1.1.244

# Check for file.
$ %[1]s tool is_link_local_multicast /path/to/file.txt

# Check entries from stdin.
$ cat /path/to/file1.txt | %[1]s tool is_link_local_multicast

Options:
--help, -h
show help.
--quiet, -q
quiet mode; suppress additional output.
`, progBase)
}

func cmdToolIsLinkLocalMulticast()(err error){
f:=lib.CmdToolIsLinkLocalMulticastFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolIsLinkLocalMulticast(f,pflag.Args()[2:],printHelpToolIsLinkLocalMulticast)
}
56 changes: 56 additions & 0 deletions ipinfo/cmd_tool_is_link_local_unicast.go
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 completionsToolIsLinkLocalUnicast = &complete.Command{
Flags:map[string]complete.Predictor{
"-h":predict.Nothing,
"--help":predict.Nothing,
"-q":predict.Nothing,
"--quiet":predict.Nothing,
},
}

func printHelpToolIsLinkLocalUnicast(){
fmt.Printf(
`Usage: %s tool is_link_local_unicast [<opts>] <cidr | ip | ip-range | filepath>

Description:
Checks if the input is Link Local Unicast address.
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file

Examples:
# Check CIDR.
$ %[1]s tool is_link_local_unicast 1.1.1.0/30

# Check IP range.
$ %[1]s tool is_link_local_unicast 1.1.1.0-1.1.1.244

# Check for file.
$ %[1]s tool is_link_local_unicast /path/to/file.txt

# Check entries from stdin.
$ cat /path/to/file1.txt | %[1]s tool is_link_local_unicast

Options:
--help, -h
show help.
--quiet, -q
quiet mode; suppress additional output.
`, progBase)
}

func cmdToolIsLinkLocalUnicast()(err error){
f:=lib.CmdToolIsLinkLocalUnicastFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolIsLinkLocalUnicast(f,pflag.Args()[2:],printHelpToolIsLinkLocalUnicast)
}
60 changes: 60 additions & 0 deletions ipinfo/cmd_tool_is_loopback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 completionsToolIsLoopBack = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
"-q": predict.Nothing,
"--quiet": predict.Nothing,
},
}

func printHelpToolIsLoopBack(){
fmt.Printf(`
%s tool is_loopback [<opts>] <cidr | ip | ip-range | filepath>

Description: Checks the provided address is a loopback
Inputs can be any IPv4 or IPv6 Address

Examples
#IPv4/IPv6 Address.
$ %[1]s tool is_loopback 127.0.0.1

#IPv4/IPv6 Address Range
$%[1]s tool is_loopback 127.0.0.1-127.8.95.6

#Check CDIR
$%[1]s tool is_loopback 127.0.0.1/32

# Check for file.
$ %[1]s tool is_loopback /path/to/file.txt

# Check entries from stdin.
$ cat /path/to/file1.txt | %[1]s tool is_loopback

Options:
--help, -h
shamirShahzad marked this conversation as resolved.
Show resolved Hide resolved
show help.
--quiet, -q
quiet mode; suppress additional output.
`, progBase)
}

func cmdToolIsLoopBack() (err error){
f := lib.CmdToolIsLoopbackFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolIsLoopback(f, pflag.Args()[2:], printHelpToolIsLoopBack)
}
Loading