File tree Expand file tree Collapse file tree 3 files changed +98
-0
lines changed
Expand file tree Collapse file tree 3 files changed +98
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ var completionsTool = &complete.Command{
1818 "is_v6" : completionsToolIsV6 ,
1919 "is_valid" : completionsToolIsValid ,
2020 "is_one_ip" : completionsToolIsOneIp ,
21+ "unmap" : completionsToolUnmap ,
2122 "lower" : completionsToolLower ,
2223 "upper" : completionsToolUpper ,
2324 "is_v4in6" : completionsToolIs4In6 ,
@@ -44,6 +45,7 @@ Commands:
4445 is_v6 reports whether input is an IPv6 address.
4546 is_valid reports whether an IP is valid.
4647 is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
48+ unmap returns ip with any IPv4-mapped IPv6 address prefix removed.
4749 lower get start IP of IPs, IP ranges, and CIDRs.
4850 upper get end IP of IPs, IP ranges, and CIDRs.
4951 is_v4in6 get whether the IP is an IPv4-mapped IPv6 address.
@@ -92,6 +94,8 @@ func cmdTool() error {
9294 err = cmdToolIsValid ()
9395 case cmd == "is_one_ip" :
9496 err = cmdToolIsOneIp ()
97+ case cmd == "unmap" :
98+ err = cmdToolUnmap ()
9599 case cmd == "lower" :
96100 err = cmdToolLower ()
97101 case cmd == "upper" :
Original file line number Diff line number Diff line change 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+ // cmdToolUnmap is the handler for the "unmap" command.
12+ var completionsToolUnmap = & complete.Command {
13+ Flags : map [string ]complete.Predictor {
14+ "-h" : predict .Nothing ,
15+ "--help" : predict .Nothing ,
16+ },
17+ }
18+
19+ // printHelpToolUnmap prints the help message for the "unmap" command.
20+ func printHelpToolUnmap () {
21+ fmt .Printf (
22+ `Usage: %s tool unmap [<opts>] <ip>
23+
24+ Description:
25+ Unmap returns an IP with any IPv4-mapped IPv6 address prefix removed.
26+
27+ That is, if the IP is an IPv6 address wrapping an IPv4 address, it returns the
28+ wrapped IPv4 address. Otherwise it returns the IP unmodified.
29+
30+ Examples:
31+ %[1]s tool unmap "::ffff:8.8.8.8"
32+ %[1]s tool unmap "192.180.32.1"
33+ %[1]s tool unmap "::ffff:192.168.1.1"
34+
35+ Options:
36+ General:
37+ --help, -h
38+ show help.
39+ ` , progBase )
40+ }
41+
42+ // cmdToolUnmap is the handler for the "unmap" command.
43+ func cmdToolUnmap () error {
44+ f := lib.CmdToolUnmapFlags {}
45+ f .Init ()
46+ pflag .Parse ()
47+
48+ return lib .CmdToolUnmap (f , pflag .Args ()[2 :], printHelpToolUnmap )
49+ }
Original file line number Diff line number Diff line change 1+ package lib
2+
3+ import (
4+ "fmt"
5+ "github.com/spf13/pflag"
6+ "net/netip"
7+ )
8+
9+ // CmdToolUnmapFlags are flags expected by CmdToolUnmap
10+ type CmdToolUnmapFlags struct {
11+ Help bool
12+ }
13+
14+ // Init initializes the common flags available to CmdToolUnmap with sensible
15+ func (f * CmdToolUnmapFlags ) Init () {
16+ pflag .BoolVarP (
17+ & f .Help ,
18+ "help" , "h" , false ,
19+ "show help." ,
20+ )
21+ }
22+
23+ // CmdToolUnmap converts a number to an IP address
24+ func CmdToolUnmap (f CmdToolUnmapFlags , 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+ addr , err := netip .ParseAddr (input )
34+ if err != nil {
35+ return err
36+ }
37+ fmt .Println (addr .Unmap ())
38+ default :
39+ return ErrNotIP
40+ }
41+ return nil
42+ }
43+
44+ return GetInputFrom (args , true , true , op )
45+ }
You can’t perform that action at this time.
0 commit comments