Skip to content

Commit 7547c17

Browse files
authored
Merge pull request #170 from ipinfo/haris/BE-2215
Implements `ipinfo tool is_valid`
2 parents 4c357bb + ed50434 commit 7547c17

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

ipinfo/cmd_tool.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var completionsTool = &complete.Command{
1616
"prev": completionsToolPrev,
1717
"is_v4": completionsToolIsV4,
1818
"is_v6": completionsToolIsV6,
19+
"is_valid": completionsToolIsValid,
1920
"is_one_ip": completionsToolIsOneIp,
2021
"lower": completionsToolLower,
2122
"upper": completionsToolUpper,
@@ -41,6 +42,7 @@ Commands:
4142
prev get the previous IP of the input IP
4243
is_v4 reports whether input is an IPv4 address.
4344
is_v6 reports whether input is an IPv6 address.
45+
is_valid reports whether an IP is valid.
4446
is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
4547
lower get start IP of IPs, IP ranges, and CIDRs.
4648
upper get end IP of IPs, IP ranges, and CIDRs.
@@ -86,6 +88,8 @@ func cmdTool() error {
8688
err = cmdToolIsV4()
8789
case cmd == "is_v6":
8890
err = cmdToolIsV6()
91+
case cmd == "is_valid":
92+
err = cmdToolIsValid()
8993
case cmd == "is_one_ip":
9094
err = cmdToolIsOneIp()
9195
case cmd == "lower":

ipinfo/cmd_tool_isValid.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/ipinfo/cli/lib"
6+
"github.com/ipinfo/cli/lib/complete"
7+
"github.com/ipinfo/cli/lib/complete/predict"
8+
"github.com/spf13/pflag"
9+
)
10+
11+
var completionsToolIsValid = &complete.Command{
12+
Flags: map[string]complete.Predictor{
13+
"-h": predict.Nothing,
14+
"--help": predict.Nothing,
15+
},
16+
}
17+
18+
// printHelpToolIsValid prints the help message for the "is_valid" command.
19+
func printHelpToolIsValid() {
20+
fmt.Printf(
21+
`Usage: %s tool is_valid <ip>
22+
23+
Description:
24+
Reports whether an IP is valid.
25+
26+
Examples:
27+
%[1]s is_valid "190.87.89.1"
28+
%[1]s is_valid "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
29+
%[1]s is_valid "::"
30+
%[1]s is_valid "0"
31+
%[1]s is_valid ""
32+
33+
Options:
34+
General:
35+
--help, -h
36+
show help.
37+
`, progBase)
38+
}
39+
40+
// cmdToolIsValid is the handler for the "is_valid" command.
41+
func cmdToolIsValid() error {
42+
f := lib.CmdToolIsValidFlags{}
43+
f.Init()
44+
pflag.Parse()
45+
46+
return lib.CmdToolIsValid(f, pflag.Args()[2:], printHelpToolIsValid)
47+
}

lib/cmd_tool_valid.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package lib
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/pflag"
6+
)
7+
8+
// CmdToolIsValidFlags are flags expected by CmdToolIsValid
9+
type CmdToolIsValidFlags struct {
10+
Help bool
11+
ipv6 bool
12+
}
13+
14+
// Init initializes the common flags available to CmdToolIsValid with sensible
15+
func (f *CmdToolIsValidFlags) Init() {
16+
pflag.BoolVarP(
17+
&f.Help,
18+
"help", "h", false,
19+
"show help.",
20+
)
21+
}
22+
23+
// CmdToolIsValid converts a number to an IP address
24+
func CmdToolIsValid(f CmdToolIsValidFlags, args []string, printHelp func()) error {
25+
if f.Help {
26+
printHelp()
27+
return nil
28+
}
29+
30+
op := func(input string, input_type INPUT_TYPE) error {
31+
switch input_type {
32+
case INPUT_TYPE_IP:
33+
fmt.Printf("%s,%v\n", input, true)
34+
default:
35+
fmt.Printf("%s,%v\n", input, false)
36+
}
37+
return nil
38+
}
39+
40+
return GetInputFrom(args, true, true, op)
41+
}

0 commit comments

Comments
 (0)