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,
},
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 IP is an interface local multicast.

Options:
--help, -h
Expand Down Expand Up @@ -112,6 +126,20 @@ func cmdTool() error {
err = cmdToolN2IP6()
case cmd == "prefix":
err = cmdToolPrefix()
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_link_local_unicast":
err = cmdToolIsLinkLocalUnicast()
case cmd == "is_link_local_multicast":
err = cmdToolIsLinkLocalMulticast()
case cmd == "is_interface_local_multicast":
err = cmdToolIsInterfaceLocalMulticast()
default:
err = toolHelp()
}
Expand Down
61 changes: 61 additions & 0 deletions ipinfo/cmd_tool_is_global_unicast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 a global unicast address.
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file

Examples:
$ %[1]s tool is_global_unicast 10.255.0.0 | 2000::1
$ %[1]s tool is_global_unicast 255.255.255.255 | ff00::1
shamirShahzad marked this conversation as resolved.
Show resolved Hide resolved

# Check CIDR.
$ %[1]s tool is_global_unicast 10.255.0.0/32 | 2000::1/64
$ %[1]s tool is_global_unicast 154.0.0.1/32 | ff00::1/64

# Check IP range.
$ %[1]s tool is_global_unicast 10.0.0.1-10.8.95.6 | 2000::1-2000::ffff
$ %[1]s tool is_global_unicast 0.0.0.0-0.255.95.6 | ff00::1-ff00::ffff

# 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)
}
61 changes: 61 additions & 0 deletions ipinfo/cmd_tool_is_interface_local_multicast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 an interface local multicast address.
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file

Examples:
$ %[1]s tool is_interface_local_multicast ff01::1
$ %[1]s tool is_interface_local_multicast 169.254.0.0 | ::1

# Check CIDR.
$ %[1]s tool is_interface_local_multicast ff01::ffff/32
$ %[1]s tool is_interface_local_multicast ff03::ffff/32

# Check IP range.
$ %[1]s tool is_interface_local_multicast ff01::1-ff01:ffff::1
$ %[1]s tool is_interface_local_multicast ff03::1-ff03:ffff::1

# 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)
}
61 changes: 61 additions & 0 deletions ipinfo/cmd_tool_is_link_local_multicast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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:
$ %[1]s tool is_link_local_multicast 224.0.0.0 | ff02::2
$ %[1]s tool is_link_local_multicast 169.200.0.0 | fe80::

# Check CIDR.
$ %[1]s tool is_link_local_multicast 224.0.0.0/32 | ff02::1/64
$ %[1]s tool is_link_local_multicast 169.200.0.0/32 | fe80::1/64
shamirShahzad marked this conversation as resolved.
Show resolved Hide resolved

# Check IP range.
$ %[1]s tool is_link_local_multicast 224.0.0.1-224.255.255.255 | ff02::1-ff02::ffff
$ %[1]s tool is_link_local_multicast 169.254.0.1-169.254.255.0 | fe80::1-fe80::ffff

# 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)
}
61 changes: 61 additions & 0 deletions ipinfo/cmd_tool_is_link_local_unicast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 a link local unicast address.
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file

Examples:
$ %[1]s tool is_link_local_unicast 169.254.0.0 | fe80::
$ %[1]s tool is_link_local_unicast 127.0.0.0 | ::1

# Check CIDR.
$ %[1]s tool is_link_local_unicast 169.254.0.0/32 | fe80::1/64
$ %[1]s tool is_link_local_unicast 139.0.0.0/32 | ::1/64

# Check IP range.
$ %[1]s tool is_link_local_unicast 169.254.0.0-169.254.255.1 | fe80::1-feb0::1
$ %[1]s tool is_link_local_unicast 240.0.0.0-240.255.255.1 | ::1-::ffff

# 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)
}
Loading