From 36fa47aa6894995ed9cab22e302b49baafc2464a Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Tue, 29 Aug 2023 17:22:57 +0500 Subject: [PATCH 1/8] Implements `ipinfo tool isValid` --- ipinfo/cmd_tool.go | 4 ++++ ipinfo/cmd_tool_isValid.go | 45 ++++++++++++++++++++++++++++++++++++++ lib/cmd_tool_valid.go | 41 ++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 ipinfo/cmd_tool_isValid.go create mode 100644 lib/cmd_tool_valid.go diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index f561eafb..32bb6409 100644 --- a/ipinfo/cmd_tool.go +++ b/ipinfo/cmd_tool.go @@ -15,6 +15,7 @@ var completionsTool = &complete.Command{ "next": completionsToolNext, "prev": completionsToolPrev, "is_v4": completionsToolIsV4, + "isValid": completionsToolIsValid, "lower": completionsToolLower, "upper": completionsToolUpper, "ip2n": completionsToolIP2n, @@ -38,6 +39,7 @@ Commands: 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. + isValid reports whether the Addr is an initialized address (not the zero Addr). lower get start IP of IPs, IP ranges, and CIDRs. upper get end IP of IPs, IP ranges, and CIDRs. ip2n converts an IPv4 or IPv6 address to its decimal representation. @@ -81,6 +83,8 @@ func cmdTool() error { err = cmdToolIsV4() case cmd == "is_v6": err = cmdToolIsV6() + case cmd == "isValid": + err = cmdToolIsValid() case cmd == "lower": err = cmdToolLower() case cmd == "upper": diff --git a/ipinfo/cmd_tool_isValid.go b/ipinfo/cmd_tool_isValid.go new file mode 100644 index 00000000..e65e3977 --- /dev/null +++ b/ipinfo/cmd_tool_isValid.go @@ -0,0 +1,45 @@ +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 completionsToolIsValid = &complete.Command{ + Flags: map[string]complete.Predictor{ + "-h": predict.Nothing, + "--help": predict.Nothing, + }, +} + +// printHelpToolIsValid prints the help message for the "IsValid" command. +func printHelpToolIsValid() { + fmt.Printf( + `Usage: %s tool IsValid + +Description: + reports whether the Addr is an initialized address (not the zero Addr). + +Examples: + %[1]s IsValid "190.87.89.1" + %[1]s IsValid "2001:0db8:85a3:0000:0000:8a2e:0370:7334" + %[1]s IsValid "::" + +Options: + General: + --help, -h + show help. +`, progBase) +} + +// cmdToolIsValid is the handler for the "IsValid" command. +func cmdToolIsValid() error { + f := lib.CmdToolIsValidFlags{} + f.Init() + pflag.Parse() + + return lib.CmdToolIsValid(f, pflag.Args()[2:], printHelpToolIsValid) +} diff --git a/lib/cmd_tool_valid.go b/lib/cmd_tool_valid.go new file mode 100644 index 00000000..fdb9e411 --- /dev/null +++ b/lib/cmd_tool_valid.go @@ -0,0 +1,41 @@ +package lib + +import ( + "fmt" + "github.com/spf13/pflag" +) + +// CmdToolIsValidFlags are flags expected by CmdToolIsValid +type CmdToolIsValidFlags struct { + Help bool + ipv6 bool +} + +// Init initializes the common flags available to CmdToolIsValid with sensible +func (f *CmdToolIsValidFlags) Init() { + pflag.BoolVarP( + &f.Help, + "help", "h", false, + "show help.", + ) +} + +// CmdToolIsValid converts a number to an IP address +func CmdToolIsValid(f CmdToolIsValidFlags, args []string, printHelp func()) error { + if f.Help { + printHelp() + return nil + } + + op := func(input string, input_type INPUT_TYPE) error { + switch input_type { + case INPUT_TYPE_IP: + fmt.Printf("%s %v\n", input, true) + default: + fmt.Printf("%s %v\n", input, false) + } + return nil + } + + return GetInputFrom(args, true, true, op) +} From 536a3a3139c45e63290a4f93fcef0ecd3c9e9f45 Mon Sep 17 00:00:00 2001 From: Haris Date: Wed, 30 Aug 2023 15:18:24 +0500 Subject: [PATCH 2/8] Update ipinfo/cmd_tool.go Co-authored-by: Uman Shahzad --- ipinfo/cmd_tool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index 32bb6409..703f7e2a 100644 --- a/ipinfo/cmd_tool.go +++ b/ipinfo/cmd_tool.go @@ -39,7 +39,7 @@ Commands: 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. - isValid reports whether the Addr is an initialized address (not the zero Addr). + isValid reports whether an IP is valid. lower get start IP of IPs, IP ranges, and CIDRs. upper get end IP of IPs, IP ranges, and CIDRs. ip2n converts an IPv4 or IPv6 address to its decimal representation. From d84dcf54c9f0aaa4980131793c441c58eb90bd4f Mon Sep 17 00:00:00 2001 From: Haris Date: Wed, 30 Aug 2023 15:18:31 +0500 Subject: [PATCH 3/8] Update lib/cmd_tool_valid.go Co-authored-by: Uman Shahzad --- lib/cmd_tool_valid.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cmd_tool_valid.go b/lib/cmd_tool_valid.go index fdb9e411..6fe3ed01 100644 --- a/lib/cmd_tool_valid.go +++ b/lib/cmd_tool_valid.go @@ -30,9 +30,9 @@ func CmdToolIsValid(f CmdToolIsValidFlags, args []string, printHelp func()) erro op := func(input string, input_type INPUT_TYPE) error { switch input_type { case INPUT_TYPE_IP: - fmt.Printf("%s %v\n", input, true) + fmt.Printf("%s,%v\n", input, true) default: - fmt.Printf("%s %v\n", input, false) + fmt.Printf("%s,%v\n", input, false) } return nil } From c29d99d52157a186458c6ad4dade5812d602dce3 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Wed, 30 Aug 2023 15:26:13 +0500 Subject: [PATCH 4/8] Corrections --- ipinfo/cmd_tool.go | 6 +++--- ipinfo/cmd_tool_isValid.go | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index 703f7e2a..c6f40fa1 100644 --- a/ipinfo/cmd_tool.go +++ b/ipinfo/cmd_tool.go @@ -15,7 +15,7 @@ var completionsTool = &complete.Command{ "next": completionsToolNext, "prev": completionsToolPrev, "is_v4": completionsToolIsV4, - "isValid": completionsToolIsValid, + "is_valid": completionsToolIsValid, "lower": completionsToolLower, "upper": completionsToolUpper, "ip2n": completionsToolIP2n, @@ -39,7 +39,7 @@ Commands: 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. - isValid reports whether an IP is valid. + is_valid reports whether an IP is valid. lower get start IP of IPs, IP ranges, and CIDRs. upper get end IP of IPs, IP ranges, and CIDRs. ip2n converts an IPv4 or IPv6 address to its decimal representation. @@ -83,7 +83,7 @@ func cmdTool() error { err = cmdToolIsV4() case cmd == "is_v6": err = cmdToolIsV6() - case cmd == "isValid": + case cmd == "is_valid": err = cmdToolIsValid() case cmd == "lower": err = cmdToolLower() diff --git a/ipinfo/cmd_tool_isValid.go b/ipinfo/cmd_tool_isValid.go index e65e3977..d3f0e0db 100644 --- a/ipinfo/cmd_tool_isValid.go +++ b/ipinfo/cmd_tool_isValid.go @@ -18,15 +18,17 @@ var completionsToolIsValid = &complete.Command{ // printHelpToolIsValid prints the help message for the "IsValid" command. func printHelpToolIsValid() { fmt.Printf( - `Usage: %s tool IsValid + `Usage: %s tool is_valid Description: - reports whether the Addr is an initialized address (not the zero Addr). + reports whether the ip address is an initialized address (not the zero Addr). Examples: - %[1]s IsValid "190.87.89.1" - %[1]s IsValid "2001:0db8:85a3:0000:0000:8a2e:0370:7334" - %[1]s IsValid "::" + %[1]s is_valid "190.87.89.1" + %[1]s is_valid "2001:0db8:85a3:0000:0000:8a2e:0370:7334" + %[1]s is_valid "::" + %[1]s is_valid "0" + %[1]s is_valid "" Options: General: From 21b073a69d69c7c3379dccd8845ec40e791d19bb Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Wed, 30 Aug 2023 15:27:03 +0500 Subject: [PATCH 5/8] Corrections --- ipinfo/cmd_tool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index c6f40fa1..855e497a 100644 --- a/ipinfo/cmd_tool.go +++ b/ipinfo/cmd_tool.go @@ -39,7 +39,7 @@ Commands: 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_valid reports whether an IP is valid. lower get start IP of IPs, IP ranges, and CIDRs. upper get end IP of IPs, IP ranges, and CIDRs. ip2n converts an IPv4 or IPv6 address to its decimal representation. From 8b24e62586ad28544a4a372cd620a77e7e9a4240 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Wed, 30 Aug 2023 15:52:21 +0500 Subject: [PATCH 6/8] Corrections --- ipinfo/cmd_tool_isValid.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ipinfo/cmd_tool_isValid.go b/ipinfo/cmd_tool_isValid.go index d3f0e0db..67f51d61 100644 --- a/ipinfo/cmd_tool_isValid.go +++ b/ipinfo/cmd_tool_isValid.go @@ -15,7 +15,7 @@ var completionsToolIsValid = &complete.Command{ }, } -// printHelpToolIsValid prints the help message for the "IsValid" command. +// printHelpToolIsValid prints the help message for the "is_valid" command. func printHelpToolIsValid() { fmt.Printf( `Usage: %s tool is_valid @@ -37,7 +37,7 @@ Options: `, progBase) } -// cmdToolIsValid is the handler for the "IsValid" command. +// cmdToolIsValid is the handler for the "is_valid" command. func cmdToolIsValid() error { f := lib.CmdToolIsValidFlags{} f.Init() From 5fa982b3356824ecf8fa9ae1f8946785ce6c0ac0 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Thu, 7 Sep 2023 14:18:58 +0500 Subject: [PATCH 7/8] Updated description --- ipinfo/cmd_tool_isValid.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipinfo/cmd_tool_isValid.go b/ipinfo/cmd_tool_isValid.go index 67f51d61..a70fe067 100644 --- a/ipinfo/cmd_tool_isValid.go +++ b/ipinfo/cmd_tool_isValid.go @@ -21,7 +21,7 @@ func printHelpToolIsValid() { `Usage: %s tool is_valid Description: - reports whether the ip address is an initialized address (not the zero Addr). + reports whether an IP is valid. Examples: %[1]s is_valid "190.87.89.1" From 0e50b3c1e95a9fed61c3a5883b54c756126fa667 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Thu, 7 Sep 2023 14:19:53 +0500 Subject: [PATCH 8/8] Updated description --- ipinfo/cmd_tool_isValid.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipinfo/cmd_tool_isValid.go b/ipinfo/cmd_tool_isValid.go index a70fe067..49e90614 100644 --- a/ipinfo/cmd_tool_isValid.go +++ b/ipinfo/cmd_tool_isValid.go @@ -21,7 +21,7 @@ func printHelpToolIsValid() { `Usage: %s tool is_valid Description: - reports whether an IP is valid. + Reports whether an IP is valid. Examples: %[1]s is_valid "190.87.89.1"