From 1a69cad91743eeffb5585fb2546f442df4591b88 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Wed, 23 Aug 2023 13:54:44 +0500 Subject: [PATCH] Implements `ipinfo tool is_v4` --- ipinfo/cmd_tool.go | 4 ++++ ipinfo/cmd_tool_is_v4.go | 42 ++++++++++++++++++++++++++++++++++++++++ lib/cmd_tool_is_v4.go | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 ipinfo/cmd_tool_is_v4.go create mode 100644 lib/cmd_tool_is_v4.go diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index 61db331a..7a573dec 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_v4": completionsToolIsV4, "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_v4 check if IPs are IPv4. 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_v4": + err = cmdToolIsV4() case cmd == "ip2n": err = cmdToolIP2n() case cmd == "n2ip": diff --git a/ipinfo/cmd_tool_is_v4.go b/ipinfo/cmd_tool_is_v4.go new file mode 100644 index 00000000..75c25ae5 --- /dev/null +++ b/ipinfo/cmd_tool_is_v4.go @@ -0,0 +1,42 @@ +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 completionsToolIsV4 = &complete.Command{ + Flags: map[string]complete.Predictor{ + "-h": predict.Nothing, + "--help": predict.Nothing, + }, +} + +func printHelpToolIsV4() { + fmt.Printf( + `Usage: %s tool is_v4 + +Description: + check if IPs are IPv4. + +Examples: + $ %[1]s tool is_v4 "192.168.1.1" + $ %[1]s tool is_v4 "1.1.1.1" + +Options: + General: + --help, -h + show help. +`, progBase) +} + +func cmdToolIsV4() error { + f := lib.CmdToolIsV4Flags{} + f.Init() + pflag.Parse() + + return lib.CmdToolIsV4(f, pflag.Args()[2:], printHelpToolIsV4) +} diff --git a/lib/cmd_tool_is_v4.go b/lib/cmd_tool_is_v4.go new file mode 100644 index 00000000..ff667166 --- /dev/null +++ b/lib/cmd_tool_is_v4.go @@ -0,0 +1,34 @@ +package lib + +import ( + "fmt" + "github.com/spf13/pflag" +) + +// CmdToolIsV4Flags are flags expected by CmdToolIsV4. +type CmdToolIsV4Flags struct { + Help bool +} + +// Init initializes the common flags available to CmdToolIsV4 with sensible +func (f *CmdToolIsV4Flags) Init() { + pflag.BoolVarP( + &f.Help, + "help", "h", false, + "show help.", + ) +} + +func CmdToolIsV4(f CmdToolIsV4Flags, 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, StrIsIPv4Str(input)) + return nil + } + + return GetInputFrom(args, true, true, op) +}