Skip to content

Commit 116912e

Browse files
authored
Merge branch 'master' into haris/BE-2218
2 parents 5fbd46f + 7547c17 commit 116912e

File tree

10 files changed

+356
-34
lines changed

10 files changed

+356
-34
lines changed

ipinfo/cmd_tool.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ var completionsTool = &complete.Command{
1515
"next": completionsToolNext,
1616
"prev": completionsToolPrev,
1717
"is_v4": completionsToolIsV4,
18+
"is_v6": completionsToolIsV6,
19+
"is_valid": completionsToolIsValid,
20+
"is_one_ip": completionsToolIsOneIp,
1821
"unmap": completionsToolUnmap,
1922
"lower": completionsToolLower,
2023
"upper": completionsToolUpper,
24+
"is_v4in6": completionsToolIs4In6,
2125
"ip2n": completionsToolIP2n,
2226
"n2ip": completionsToolN2IP,
2327
"n2ip6": completionsToolN2IP6,
@@ -39,9 +43,12 @@ Commands:
3943
prev get the previous IP of the input IP
4044
is_v4 reports whether input is an IPv4 address.
4145
is_v6 reports whether input is an IPv6 address.
46+
is_valid reports whether an IP is valid.
47+
is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
4248
unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
4349
lower get start IP of IPs, IP ranges, and CIDRs.
4450
upper get end IP of IPs, IP ranges, and CIDRs.
51+
is_v4in6 get whether the IP is an IPv4-mapped IPv6 address.
4552
ip2n converts an IPv4 or IPv6 address to its decimal representation.
4653
n2ip evaluates a mathematical expression and converts it to an IPv4 or IPv6.
4754
n2ip6 evaluates a mathematical expression and converts it to an IPv6.
@@ -83,12 +90,18 @@ func cmdTool() error {
8390
err = cmdToolIsV4()
8491
case cmd == "is_v6":
8592
err = cmdToolIsV6()
93+
case cmd == "is_valid":
94+
err = cmdToolIsValid()
95+
case cmd == "is_one_ip":
96+
err = cmdToolIsOneIp()
8697
case cmd == "unmap":
8798
err = cmdToolUnmap()
8899
case cmd == "lower":
89100
err = cmdToolLower()
90101
case cmd == "upper":
91102
err = cmdToolUpper()
103+
case cmd == "is_v4in6":
104+
err = cmdToolIsV4In6()
92105
case cmd == "ip2n":
93106
err = cmdToolIP2n()
94107
case cmd == "n2ip":

ipinfo/cmd_tool_isV4In6.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+
// cmdToolIsV4In6 is the handler for the "is_v4in6" command.
12+
var completionsToolIs4In6 = &complete.Command{
13+
Flags: map[string]complete.Predictor{
14+
"-h": predict.Nothing,
15+
"--help": predict.Nothing,
16+
},
17+
}
18+
19+
// printHelpToolIsV4In6 prints the help message for the "is_v4in6" command.
20+
func printHelpToolIsV4In6() {
21+
fmt.Printf(
22+
`Usage: %s tool is_v4in6 [<opts>] <ips>
23+
24+
Description:
25+
get whether the IP is an IPv4-mapped IPv6 address.
26+
27+
Examples:
28+
%[1]s is_v4in6 "::7f00:1"
29+
%[1]s is_v4in6 "::ffff"
30+
%[1]s is_v4in6 "::ffff:8.8.8.8"
31+
%[1]s is_v4in6 "::ffff:192.0.2.1
32+
33+
Options:
34+
General:
35+
--help, -h
36+
show help.
37+
`, progBase)
38+
}
39+
40+
// cmdToolIsV4In6 is the handler for the "is_v4in6" command.
41+
func cmdToolIsV4In6() error {
42+
f := lib.CmdToolIsV4In6Flags{}
43+
f.Init()
44+
pflag.Parse()
45+
46+
return lib.CmdToolIsV4In6(f, pflag.Args()[2:], printHelpToolIsV4In6)
47+
}

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+
}

ipinfo/cmd_tool_is_one_ip.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ipinfo/cli/lib"
7+
"github.com/ipinfo/cli/lib/complete"
8+
"github.com/ipinfo/cli/lib/complete/predict"
9+
"github.com/spf13/pflag"
10+
)
11+
12+
var completionsToolIsOneIp = &complete.Command{
13+
Flags: map[string]complete.Predictor{
14+
"-h": predict.Nothing,
15+
"--help": predict.Nothing,
16+
},
17+
}
18+
19+
func printHelpToolIsOneIp() {
20+
fmt.Printf(
21+
`Usage: %s tool is_one_ip [<opts>] <cidr | ip | ip-range | filepath>
22+
23+
Description:
24+
Checks whether a CIDR or IP Range contains exactly one IP.
25+
26+
Examples:
27+
# Check CIDR.
28+
$ %[1]s tool is_one_ip 1.1.1.0/30
29+
30+
# Check IP.
31+
$ %[1]s tool is_one_ip 1.1.1.1
32+
33+
# Check IP range.
34+
$ %[1]s tool is_one_ip 1.1.1.1-2.2.2.2
35+
36+
# Check for file.
37+
$ %[1]s tool is_one_ip /path/to/file.txt
38+
39+
# Check entries from stdin.
40+
$ cat /path/to/file1.txt | %[1]s tool is_one_ip
41+
42+
Options:
43+
--help, -h
44+
show help.
45+
`, progBase)
46+
}
47+
48+
func cmdToolIsOneIp() (err error) {
49+
f := lib.CmdToolIsOneIpFlags{}
50+
f.Init()
51+
pflag.Parse()
52+
53+
return lib.CmdToolIsOneIp(f, pflag.Args()[2:], printHelpToolIsOneIp)
54+
}

ipinfo/cmd_tool_n2ip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var completionsToolN2IP = &complete.Command{
2121
// printHelpToolN2IP prints the help message for the "n2ip" command.
2222
func printHelpToolN2IP() {
2323
fmt.Printf(
24-
`Usage: %s n2ip tool [<opts>] <number>
24+
`Usage: %s tool n2ip [<opts>] <number>
2525
2626
Description:
2727
Converts a given numeric representation to its corresponding IPv4 or IPv6 address,

lib/cmd_tool_isV4In6.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package lib
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/pflag"
6+
"net/netip"
7+
)
8+
9+
// CmdToolIsV4In6Flags are flags expected by CmdToolIsV4In6
10+
type CmdToolIsV4In6Flags struct {
11+
Help bool
12+
}
13+
14+
// Init initializes the common flags available to CmdToolIsV4In6 with sensible
15+
func (f *CmdToolIsV4In6Flags) Init() {
16+
pflag.BoolVarP(
17+
&f.Help,
18+
"help", "h", false,
19+
"show help.",
20+
)
21+
}
22+
23+
// CmdToolIsV4In6 checks if given ip is an IPv4-mapped IPv6 address.
24+
func CmdToolIsV4In6(f CmdToolIsV4In6Flags, args []string, printHelp func()) error {
25+
if f.Help {
26+
printHelp()
27+
return nil
28+
}
29+
30+
op := func(input string, inputType INPUT_TYPE) error {
31+
switch inputType {
32+
case INPUT_TYPE_IP:
33+
addr, err := netip.ParseAddr(input)
34+
if err != nil {
35+
return ErrInvalidInput
36+
}
37+
38+
fmt.Printf("%s,%t\n", input, addr.Is4In6())
39+
default:
40+
return ErrInvalidInput
41+
}
42+
return nil
43+
}
44+
45+
return GetInputFrom(args, true, true, op)
46+
}

lib/cmd_tool_is_one_ip.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package lib
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/pflag"
6+
"net/netip"
7+
)
8+
9+
// CmdToolIsOneIpFlags are flags expected by CmdToolIP2n
10+
type CmdToolIsOneIpFlags struct {
11+
Help bool
12+
}
13+
14+
// Init initializes the common flags available to CmdToolIsOneIp with sensible
15+
func (f *CmdToolIsOneIpFlags) Init() {
16+
pflag.BoolVarP(
17+
&f.Help,
18+
"help", "h", false,
19+
"show help.",
20+
)
21+
}
22+
23+
func CmdToolIsOneIp(f CmdToolIsOneIpFlags, args []string, printHelp func()) error {
24+
if f.Help {
25+
printHelp()
26+
return nil
27+
}
28+
29+
op := func(input string, inputType INPUT_TYPE) error {
30+
isOneIp := false
31+
switch inputType {
32+
case INPUT_TYPE_CIDR:
33+
prefix, err := netip.ParsePrefix(input)
34+
if err != nil {
35+
return ErrInvalidInput
36+
}
37+
isOneIp = prefix.IsSingleIP()
38+
case INPUT_TYPE_IP:
39+
isOneIp = true
40+
case INPUT_TYPE_IP_RANGE:
41+
isOneIp = ipRangeContainsExactlyOneIP(input)
42+
default:
43+
return ErrInvalidInput
44+
}
45+
fmt.Printf("%s,%v\n", input, isOneIp)
46+
return nil
47+
}
48+
49+
return GetInputFrom(args, true, true, op)
50+
}
51+
52+
func ipRangeContainsExactlyOneIP(ipRangeStr string) bool {
53+
ipRange, err := IPRangeStrFromStr(ipRangeStr)
54+
if err != nil {
55+
return false
56+
}
57+
58+
return ipRange.Start == ipRange.End
59+
}

lib/cmd_tool_lower.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ func CmdToolLower(
3636
}
3737

3838
actionFunc := func(input string, inputType INPUT_TYPE) error {
39+
var err error
3940
switch inputType {
4041
case INPUT_TYPE_IP:
41-
ActionForIP(input)
42+
fmt.Println(input)
4243
case INPUT_TYPE_IP_RANGE:
43-
ActionForRange(input)
44+
err = ActionForRange(input)
4445
case INPUT_TYPE_CIDR:
45-
ActionForCIDR(input)
46+
err = ActionForCIDR(input)
4647
default:
47-
return ErrNotIP
48+
return ErrInvalidInput
4849
}
49-
return nil
50+
return err
5051
}
5152
err := GetInputFrom(args, true, true, actionFunc)
5253
if err != nil {
@@ -56,24 +57,30 @@ func CmdToolLower(
5657
return nil
5758
}
5859

59-
func ActionForIP(input string) {
60-
fmt.Println(input)
61-
}
62-
63-
func ActionForRange(input string) {
60+
func ActionForRange(input string) error {
6461
ipRange, err := IPRangeStrFromStr(input)
6562
if err != nil {
66-
fmt.Println(err)
67-
return
63+
return err
6864
}
6965
fmt.Println(ipRange.Start)
66+
return nil
7067
}
7168

72-
func ActionForCIDR(input string) {
69+
func ActionForCIDR(input string) error {
7370
_, ipnet, err := net.ParseCIDR(input)
7471
if err != nil {
75-
fmt.Println(err)
76-
return
72+
return err
73+
}
74+
75+
var lower string
76+
if ipnet.IP.To4() != nil {
77+
ipRange, _ := IPRangeStrFromCIDR(input)
78+
lower = ipRange.Start
79+
} else if ipnet.IP.To16() != nil {
80+
ipRange, _ := IP6RangeStrFromCIDR(input)
81+
lower = ipRange.Start
7782
}
78-
fmt.Println(ipnet.IP)
83+
84+
fmt.Println(lower)
85+
return nil
7986
}

0 commit comments

Comments
 (0)