Skip to content

Commit f3be503

Browse files
authored
Merge pull request #192 from shamirShahzad/master
Issue #186 more basic IP tools
2 parents d2f94f9 + 24374e7 commit f3be503

17 files changed

+1083
-34
lines changed

ipinfo/cmd_tool.go

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,28 @@ import (
1111

1212
var completionsTool = &complete.Command{
1313
Sub: map[string]*complete.Command{
14-
"aggregate": completionsToolAggregate,
15-
"next": completionsToolNext,
16-
"prev": completionsToolPrev,
17-
"is_v4": completionsToolIsV4,
18-
"is_v6": completionsToolIsV6,
19-
"is_valid": completionsToolIsValid,
20-
"is_one_ip": completionsToolIsOneIp,
21-
"unmap": completionsToolUnmap,
22-
"lower": completionsToolLower,
23-
"upper": completionsToolUpper,
24-
"is_v4in6": completionsToolIs4In6,
25-
"ip2n": completionsToolIP2n,
26-
"n2ip": completionsToolN2IP,
27-
"n2ip6": completionsToolN2IP6,
28-
"prefix": completionsToolPrefix,
14+
"aggregate": completionsToolAggregate,
15+
"next": completionsToolNext,
16+
"prev": completionsToolPrev,
17+
"is_v4": completionsToolIsV4,
18+
"is_v6": completionsToolIsV6,
19+
"is_valid": completionsToolIsValid,
20+
"is_one_ip": completionsToolIsOneIp,
21+
"unmap": completionsToolUnmap,
22+
"lower": completionsToolLower,
23+
"upper": completionsToolUpper,
24+
"is_v4in6": completionsToolIs4In6,
25+
"ip2n": completionsToolIP2n,
26+
"n2ip": completionsToolN2IP,
27+
"n2ip6": completionsToolN2IP6,
28+
"prefix": completionsToolPrefix,
29+
"is_loopback": completionsToolIsLoopBack,
30+
"is_multicast": completionsToolIsMulticast,
31+
"is_unspecified": completionsToolIsUnspecified,
32+
"is_global_unicast": completionsToolIsGlobalUnicast,
33+
"is_link_local_unicast": completionsToolIsLinkLocalUnicast,
34+
"is_link_local_multicast": completionsToolIsLinkLocalMulticast,
35+
"is_interface_local_multicast": completionsToolIsInterfaceLocalMulticast,
2936
},
3037
Flags: map[string]complete.Predictor{
3138
"-h": predict.Nothing,
@@ -39,21 +46,28 @@ func printHelpTool() {
3946
`Usage: %s tool <cmd> [<opts>] [<args>]
4047
4148
Commands:
42-
aggregate aggregate IPs, IP ranges, and CIDRs.
43-
next get the next IP of the input IP
44-
prev get the previous IP of the input IP
45-
is_v4 reports whether input is an IPv4 address.
46-
is_v6 reports whether input is an IPv6 address.
47-
is_valid reports whether an IP is valid.
48-
is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
49-
unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
50-
lower get start IP of IPs, IP ranges, and CIDRs.
51-
upper get end IP of IPs, IP ranges, and CIDRs.
52-
is_v4in6 get whether the IP is an IPv4-mapped IPv6 address.
53-
ip2n converts an IPv4 or IPv6 address to its decimal representation.
54-
n2ip evaluates a mathematical expression and converts it to an IPv4 or IPv6.
55-
n2ip6 evaluates a mathematical expression and converts it to an IPv6.
56-
prefix misc. prefix tools related to CIDRs.
49+
aggregate aggregate IPs, IP ranges, and CIDRs.
50+
next get the next IP of the input IP
51+
prev get the previous IP of the input IP
52+
is_v4 reports whether input is an IPv4 address.
53+
is_v6 reports whether input is an IPv6 address.
54+
is_valid reports whether an IP is valid.
55+
is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
56+
unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
57+
lower get start IP of IPs, IP ranges, and CIDRs.
58+
upper get end IP of IPs, IP ranges, and CIDRs.
59+
is_v4in6 get whether the IP is an IPv4-mapped IPv6 address.
60+
ip2n converts an IPv4 or IPv6 address to its decimal representation.
61+
n2ip evaluates a mathematical expression and converts it to an IPv4 or IPv6.
62+
n2ip6 evaluates a mathematical expression and converts it to an IPv6.
63+
prefix misc. prefix tools related to CIDRs.
64+
is_loopback reports whether an IP is a valid loopback address.
65+
is_multicast reports whether an IP is a valid multicast address.
66+
is_unspecified reports whether an IP is an unspecified address.
67+
is_global_unicast reports whether an IP is a global unicast address.
68+
is_link_local_unicast reports whether IP is a link local unicast.
69+
is_link_local_multicast reports whether IP is a link local multicast address.
70+
is_interface_local_multicast reports whether IP is an interface local multicast.
5771
5872
Options:
5973
--help, -h
@@ -112,6 +126,20 @@ func cmdTool() error {
112126
err = cmdToolN2IP6()
113127
case cmd == "prefix":
114128
err = cmdToolPrefix()
129+
case cmd == "is_loopback":
130+
err = cmdToolIsLoopBack()
131+
case cmd == "is_multicast":
132+
err = cmdToolIsMultiCast()
133+
case cmd == "is_unspecified":
134+
err = cmdToolIsUnspecified()
135+
case cmd == "is_global_unicast":
136+
err = cmdToolisGlobalUnicast()
137+
case cmd == "is_link_local_unicast":
138+
err = cmdToolIsLinkLocalUnicast()
139+
case cmd == "is_link_local_multicast":
140+
err = cmdToolIsLinkLocalMulticast()
141+
case cmd == "is_interface_local_multicast":
142+
err = cmdToolIsInterfaceLocalMulticast()
115143
default:
116144
err = toolHelp()
117145
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 completionsToolIsGlobalUnicast = &complete.Command{
13+
Flags: map[string]complete.Predictor{
14+
"-h": predict.Nothing,
15+
"--help": predict.Nothing,
16+
"-q": predict.Nothing,
17+
"--quiet": predict.Nothing,
18+
},
19+
}
20+
21+
func printHelpToolIsGlobalUnicast() {
22+
fmt.Printf(
23+
`Usage: %s tool is_global_unicast [<opts>] <cidr | ip | ip-range | filepath>
24+
25+
Description:
26+
Checks if the input is a global unicast address.
27+
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file
28+
29+
Examples:
30+
$ %[1]s tool is_global_unicast 10.255.0.0
31+
$ %[1]s tool is_global_unicast 255.255.255.255
32+
$ %[1]s tool is_global_unicast 2000::1
33+
$ %[1]s tool is_global_unicast ff00::1
34+
35+
# Check CIDR.
36+
$ %[1]s tool is_global_unicast 10.255.0.0/32
37+
$ %[1]s tool is_global_unicast 255.255.255.255/32
38+
$ %[1]s tool is_global_unicast 2000::1/64
39+
$ %[1]s tool is_global_unicast ff00::1/64
40+
41+
# Check IP range.
42+
$ %[1]s tool is_global_unicast 10.0.0.1-10.8.95.6
43+
$ %[1]s tool is_global_unicast 0.0.0.0-0.255.95.6
44+
$ %[1]s tool is_global_unicast 2000::1-2000::ffff
45+
$ %[1]s tool is_global_unicast ff00::1-ff00::ffff
46+
47+
# Check for file.
48+
$ %[1]s tool is_global_unicast /path/to/file.txt
49+
50+
# Check entries from stdin.
51+
$ cat /path/to/file1.txt | %[1]s tool is_global_unicast
52+
53+
Options:
54+
--help, -h
55+
show help.
56+
--quiet, -q
57+
quiet mode; suppress additional output.
58+
`, progBase)
59+
}
60+
61+
func cmdToolisGlobalUnicast() (err error) {
62+
f := lib.CmdToolIsGlobalUnicastFlags{}
63+
f.Init()
64+
pflag.Parse()
65+
66+
return lib.CmdToolIsGlobalUnicast(f, pflag.Args()[2:], printHelpToolIsGlobalUnicast)
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 completionsToolIsInterfaceLocalMulticast = &complete.Command{
13+
Flags: map[string]complete.Predictor{
14+
"-h": predict.Nothing,
15+
"--help": predict.Nothing,
16+
"-q": predict.Nothing,
17+
"--quiet": predict.Nothing,
18+
},
19+
}
20+
21+
func printHelpToolIsInterfaceLocalMulticast() {
22+
fmt.Printf(
23+
`Usage: %s tool is_interface_local_multicast [<opts>] <cidr | ip | ip-range | filepath>
24+
25+
Description:
26+
Checks if the input is an interface local multicast address.
27+
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file
28+
29+
Examples:
30+
$ %[1]s tool is_interface_local_multicast ff01::1
31+
$ %[1]s tool is_interface_local_multicast ::1
32+
33+
# Check CIDR.
34+
$ %[1]s tool is_interface_local_multicast ff01::ffff/32
35+
$ %[1]s tool is_interface_local_multicast ff03::ffff/32
36+
37+
# Check IP range.
38+
$ %[1]s tool is_interface_local_multicast ff01::1-ff01:ffff::1
39+
$ %[1]s tool is_interface_local_multicast ff03::1-ff03:ffff::1
40+
41+
# Check for file.
42+
$ %[1]s tool is_interface_local_multicast /path/to/file.txt
43+
44+
# Check entries from stdin.
45+
$ cat /path/to/file1.txt | %[1]s tool is_interface_local_multicast
46+
47+
Options:
48+
--help, -h
49+
show help.
50+
--quiet, -q
51+
quiet mode; suppress additional output.
52+
`, progBase)
53+
}
54+
55+
func cmdToolIsInterfaceLocalMulticast() (err error) {
56+
f := lib.CmdToolIsInterfaceLocalMulticastFlags{}
57+
f.Init()
58+
pflag.Parse()
59+
60+
return lib.CmdToolIsInterfaceLocalMulticast(f, pflag.Args()[2:], printHelpToolIsInterfaceLocalMulticast)
61+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 completionsToolIsLinkLocalMulticast = &complete.Command{
13+
Flags: map[string]complete.Predictor{
14+
"-h": predict.Nothing,
15+
"--help": predict.Nothing,
16+
"-q": predict.Nothing,
17+
"--quiet": predict.Nothing,
18+
},
19+
}
20+
21+
func printHelpToolIsLinkLocalMulticast() {
22+
fmt.Printf(
23+
`Usage: %s tool is_link_local_multicast [<opts>] <cidr | ip | ip-range | filepath>
24+
25+
Description:
26+
Checks if the input is a link local multicast address.
27+
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file
28+
29+
Examples:
30+
$ %[1]s tool is_link_local_multicast 224.0.0.0
31+
$ %[1]s tool is_link_local_multicast 169.200.0.0
32+
$ %[1]s tool is_link_local_multicast ff02::2
33+
$ %[1]s tool is_link_local_multicast fe80::
34+
35+
# Check CIDR.
36+
$ %[1]s tool is_link_local_multicast 224.0.0.0/32
37+
$ %[1]s tool is_link_local_multicast 169.200.0.0/32
38+
$ %[1]s tool is_link_local_multicast ff02::1/64
39+
$ %[1]s tool is_link_local_multicast fe80::1/64
40+
41+
# Check IP range.
42+
$ %[1]s tool is_link_local_multicast 224.0.0.1-224.255.255.255
43+
$ %[1]s tool is_link_local_multicast 169.254.0.1-169.254.255.0
44+
$ %[1]s tool is_link_local_multicast ff02::1-ff02::ffff
45+
$ %[1]s tool is_link_local_multicast fe80::1-fe80::ffff
46+
47+
# Check for file.
48+
$ %[1]s tool is_link_local_multicast /path/to/file.txt
49+
50+
# Check entries from stdin.
51+
$ cat /path/to/file1.txt | %[1]s tool is_link_local_multicast
52+
53+
Options:
54+
--help, -h
55+
show help.
56+
--quiet, -q
57+
quiet mode; suppress additional output.
58+
`, progBase)
59+
}
60+
61+
func cmdToolIsLinkLocalMulticast() (err error) {
62+
f := lib.CmdToolIsLinkLocalMulticastFlags{}
63+
f.Init()
64+
pflag.Parse()
65+
66+
return lib.CmdToolIsLinkLocalMulticast(f, pflag.Args()[2:], printHelpToolIsLinkLocalMulticast)
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 completionsToolIsLinkLocalUnicast = &complete.Command{
13+
Flags: map[string]complete.Predictor{
14+
"-h": predict.Nothing,
15+
"--help": predict.Nothing,
16+
"-q": predict.Nothing,
17+
"--quiet": predict.Nothing,
18+
},
19+
}
20+
21+
func printHelpToolIsLinkLocalUnicast() {
22+
fmt.Printf(
23+
`Usage: %s tool is_link_local_unicast [<opts>] <cidr | ip | ip-range | filepath>
24+
25+
Description:
26+
Checks if the input is a link local unicast address.
27+
Inputs can be IPs, IP ranges, CIDRs, or filepath to a file
28+
29+
Examples:
30+
$ %[1]s tool is_link_local_unicast 169.254.0.0
31+
$ %[1]s tool is_link_local_unicast 127.0.0.0
32+
$ %[1]s tool is_link_local_unicast fe80::1
33+
$ %[1]s tool is_link_local_unicast ::1
34+
35+
# Check CIDR.
36+
$ %[1]s tool is_link_local_unicast 169.254.0.0/32
37+
$ %[1]s tool is_link_local_unicast 139.0.0.0/32
38+
$ %[1]s tool is_link_local_unicast fe80::1/64
39+
$ %[1]s tool is_link_local_unicast ::1/64
40+
41+
# Check IP range.
42+
$ %[1]s tool is_link_local_unicast 169.254.0.0-169.254.255.1
43+
$ %[1]s tool is_link_local_unicast 240.0.0.0-240.255.255.1
44+
$ %[1]s tool is_link_local_unicast fe80::1-feb0::1
45+
$ %[1]s tool is_link_local_unicast ::1-::ffff
46+
47+
# Check for file.
48+
$ %[1]s tool is_link_local_unicast /path/to/file.txt
49+
50+
# Check entries from stdin.
51+
$ cat /path/to/file1.txt | %[1]s tool is_link_local_unicast
52+
53+
Options:
54+
--help, -h
55+
show help.
56+
--quiet, -q
57+
quiet mode; suppress additional output.
58+
`, progBase)
59+
}
60+
61+
func cmdToolIsLinkLocalUnicast() (err error) {
62+
f := lib.CmdToolIsLinkLocalUnicastFlags{}
63+
f.Init()
64+
pflag.Parse()
65+
66+
return lib.CmdToolIsLinkLocalUnicast(f, pflag.Args()[2:], printHelpToolIsLinkLocalUnicast)
67+
}

0 commit comments

Comments
 (0)