From f65ffe67a833a4b115d29f015c282188324e8d67 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Wed, 30 Aug 2023 12:08:04 +0500 Subject: [PATCH 1/4] unmap --- ipinfo/cmd_tool.go | 2 ++ ipinfo/cmd_tool_unmap.go | 47 ++++++++++++++++++++++++++++++++++++++++ lib/cmd_tool_next.go | 3 +-- lib/cmd_tool_unmap.go | 44 +++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 ipinfo/cmd_tool_unmap.go create mode 100644 lib/cmd_tool_unmap.go diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index f561eafb..480ab579 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, + "unmap": completionsToolUnmap, "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. + unmap returns ip with any IPv4-mapped IPv6 address prefix removed. 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. diff --git a/ipinfo/cmd_tool_unmap.go b/ipinfo/cmd_tool_unmap.go new file mode 100644 index 00000000..7fb52127 --- /dev/null +++ b/ipinfo/cmd_tool_unmap.go @@ -0,0 +1,47 @@ +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" +) + +// cmdToolUnmap is the handler for the "unmap" command. +var completionsToolUnmap = &complete.Command{ + Flags: map[string]complete.Predictor{ + "-h": predict.Nothing, + "--help": predict.Nothing, + }, +} + +// printHelpToolUnmap prints the help message for the "unmap" command. +func printHelpToolUnmap() { + fmt.Printf( + `Usage: %s tool unmap [] + +Description: + Unmap returns ip with any IPv4-mapped IPv6 address prefix removed. + That is, if ip is an IPv6 address wrapping an IPv4 address, it returns the wrapped IPv4 address. Otherwise it returns ip unmodified. + +Examples: + %[1]s unmap "::ffff: + %[1]s unmap "::aa" + %[1]s unmap "::ffff:192.168.1.1" + +Options: + General: + --help, -h + show help. +`, progBase) +} + +// cmdToolUnmap is the handler for the "unmap" command. +func cmdToolUnmap() error { + f := lib.CmdToolUnmapFlags{} + f.Init() + pflag.Parse() + + return lib.CmdToolUnmap(f, pflag.Args()[2:], printHelpToolUnmap) +} diff --git a/lib/cmd_tool_next.go b/lib/cmd_tool_next.go index 3de07415..b4e7c511 100644 --- a/lib/cmd_tool_next.go +++ b/lib/cmd_tool_next.go @@ -2,7 +2,6 @@ package lib import ( "fmt" - "net" "github.com/spf13/pflag" ) @@ -48,6 +47,6 @@ func CmdToolNext( if err != nil { fmt.Println(err) } - + return nil } diff --git a/lib/cmd_tool_unmap.go b/lib/cmd_tool_unmap.go new file mode 100644 index 00000000..9dad682b --- /dev/null +++ b/lib/cmd_tool_unmap.go @@ -0,0 +1,44 @@ +package lib + +import ( + "fmt" + "github.com/spf13/pflag" +) + +// CmdToolUnmapFlags are flags expected by CmdToolUnmap +type CmdToolUnmapFlags struct { + Help bool +} + +// Init initializes the common flags available to CmdToolUnmap with sensible +func (f *CmdToolUnmapFlags) Init() { + pflag.BoolVarP( + &f.Help, + "help", "h", false, + "show help.", + ) +} + +// CmdToolUnmap converts a number to an IP address +func CmdToolUnmap(f CmdToolUnmapFlags, 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: + ip, err := IPtoDecimalStr(input) + if err != nil { + return err + } + fmt.Println(ip) + default: + return ErrNotIP + } + return nil + } + + return GetInputFrom(args, true, true, op) +} From fb5a7209ff004fb36c3c379f0060ea2cc6de4d22 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Thu, 7 Sep 2023 15:27:22 +0500 Subject: [PATCH 2/4] using `net/netip` --- ipinfo/cmd_tool.go | 2 ++ lib/cmd_tool_unmap.go | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/ipinfo/cmd_tool.go b/ipinfo/cmd_tool.go index 480ab579..38196998 100644 --- a/ipinfo/cmd_tool.go +++ b/ipinfo/cmd_tool.go @@ -83,6 +83,8 @@ func cmdTool() error { err = cmdToolIsV4() case cmd == "is_v6": err = cmdToolIsV6() + case cmd == "unmap": + err = cmdToolUnmap() case cmd == "lower": err = cmdToolLower() case cmd == "upper": diff --git a/lib/cmd_tool_unmap.go b/lib/cmd_tool_unmap.go index 9dad682b..4e0ed8d5 100644 --- a/lib/cmd_tool_unmap.go +++ b/lib/cmd_tool_unmap.go @@ -3,6 +3,7 @@ package lib import ( "fmt" "github.com/spf13/pflag" + "net/netip" ) // CmdToolUnmapFlags are flags expected by CmdToolUnmap @@ -29,11 +30,11 @@ func CmdToolUnmap(f CmdToolUnmapFlags, args []string, printHelp func()) error { op := func(input string, input_type INPUT_TYPE) error { switch input_type { case INPUT_TYPE_IP: - ip, err := IPtoDecimalStr(input) + addr, err := netip.ParseAddr(input) if err != nil { return err } - fmt.Println(ip) + fmt.Println(addr.Unmap()) default: return ErrNotIP } From 995e355ead09ccf1ff35cf883bd1de87b8118148 Mon Sep 17 00:00:00 2001 From: harisabdullah Date: Thu, 7 Sep 2023 15:36:13 +0500 Subject: [PATCH 3/4] Better examples --- ipinfo/cmd_tool_unmap.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ipinfo/cmd_tool_unmap.go b/ipinfo/cmd_tool_unmap.go index 7fb52127..88252e71 100644 --- a/ipinfo/cmd_tool_unmap.go +++ b/ipinfo/cmd_tool_unmap.go @@ -26,8 +26,8 @@ Description: That is, if ip is an IPv6 address wrapping an IPv4 address, it returns the wrapped IPv4 address. Otherwise it returns ip unmodified. Examples: - %[1]s unmap "::ffff: - %[1]s unmap "::aa" + %[1]s unmap "::ffff:8.8.8.8" + %[1]s unmap "192.180.32.1" %[1]s unmap "::ffff:192.168.1.1" Options: From 5fbd46f5574cc154b6aabb640625517fbb3eacb3 Mon Sep 17 00:00:00 2001 From: Uman Shahzad Date: Thu, 7 Sep 2023 16:40:20 +0500 Subject: [PATCH 4/4] Apply suggestions from code review --- ipinfo/cmd_tool_unmap.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ipinfo/cmd_tool_unmap.go b/ipinfo/cmd_tool_unmap.go index 88252e71..be9693ac 100644 --- a/ipinfo/cmd_tool_unmap.go +++ b/ipinfo/cmd_tool_unmap.go @@ -22,13 +22,15 @@ func printHelpToolUnmap() { `Usage: %s tool unmap [] Description: - Unmap returns ip with any IPv4-mapped IPv6 address prefix removed. - That is, if ip is an IPv6 address wrapping an IPv4 address, it returns the wrapped IPv4 address. Otherwise it returns ip unmodified. + Unmap returns an IP with any IPv4-mapped IPv6 address prefix removed. + + That is, if the IP is an IPv6 address wrapping an IPv4 address, it returns the + wrapped IPv4 address. Otherwise it returns the IP unmodified. Examples: - %[1]s unmap "::ffff:8.8.8.8" - %[1]s unmap "192.180.32.1" - %[1]s unmap "::ffff:192.168.1.1" + %[1]s tool unmap "::ffff:8.8.8.8" + %[1]s tool unmap "192.180.32.1" + %[1]s tool unmap "::ffff:192.168.1.1" Options: General: