diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index 61db331a..2d81f483 100644 --- a/ipinfo/cmd_tool.go +++ b/ipinfo/cmd_tool.go @@ -14,6 +14,7 @@ var completionsTool = &complete.Command{ "aggregate": completionsToolAggregate, "lower": completionsToolLower, "upper": completionsToolUpper, + "is_v6": completionsToolIsV6, "ip2n": completionsToolIP2n, "n2ip": completionsToolN2IP, "n2ip6": completionsToolN2IP6, @@ -33,6 +34,7 @@ Commands: aggregate aggregate IPs, IP ranges, and CIDRs. lower get start IP of IPs, IP ranges, and CIDRs. upper get end IP of IPs, IP ranges, and CIDRs. + is_v6 check if IPs are IPv6. 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. @@ -70,6 +72,8 @@ func cmdTool() error { err = cmdToolLower() case cmd == "upper": err = cmdToolUpper() + case cmd == "is_v6": + err = cmdToolIsV6() case cmd == "ip2n": err = cmdToolIP2n() case cmd == "n2ip": diff --git a/ipinfo/cmd_tool_is_v6.go b/ipinfo/cmd_tool_is_v6.go new file mode 100644 index 00000000..aa2a8283 --- /dev/null +++ b/ipinfo/cmd_tool_is_v6.go @@ -0,0 +1,43 @@ +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 completionsToolIsV6 = &complete.Command{ + Flags: map[string]complete.Predictor{ + "-h": predict.Nothing, + "--help": predict.Nothing, + }, +} + +func printHelpToolIsV6() { + fmt.Printf( + `Usage: %s tool is_v6 + +Description: + check if IPs are IPv6. + +Examples: + $ %[1]s tool is_v6 "2001:0db8:85a3:0000:0000:8a2e:0370:7334" + $ %[1]s tool is_v6 "2001:0db8:85a3::8a2e:0370:7334" + $ %[1]s tool is_v6 "::7334" + +Options: + General: + --help, -h + show help. +`, progBase) +} + +func cmdToolIsV6() error { + f := lib.CmdToolIsV6Flags{} + f.Init() + pflag.Parse() + + return lib.CmdToolIsV6(f, pflag.Args()[2:], printHelpToolIsV6) +} diff --git a/lib/cmd_tool_is_v6.go b/lib/cmd_tool_is_v6.go new file mode 100644 index 00000000..a7adef21 --- /dev/null +++ b/lib/cmd_tool_is_v6.go @@ -0,0 +1,34 @@ +package lib + +import ( + "fmt" + "github.com/spf13/pflag" +) + +// CmdToolIsV6Flags are flags expected by CmdToolIsV6. +type CmdToolIsV6Flags struct { + Help bool +} + +// Init initializes the common flags available to CmdToolIsV6 with sensible +func (f *CmdToolIsV6Flags) Init() { + pflag.BoolVarP( + &f.Help, + "help", "h", false, + "show help.", + ) +} + +func CmdToolIsV6(f CmdToolIsV6Flags, args []string, printHelp func()) error { + if f.Help { + printHelp() + return nil + } + + op := func(input string, input_type INPUT_TYPE) error { + fmt.Printf("%s %t\n", input, StrIsIPv6Str(input)) + return nil + } + + return GetInputFrom(args, true, true, op) +}