Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements ipinfo tool unmap #173

Merged
merged 5 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ipinfo/cmd_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var completionsTool = &complete.Command{
"next": completionsToolNext,
"prev": completionsToolPrev,
"is_v4": completionsToolIsV4,
"unmap": completionsToolUnmap,
"lower": completionsToolLower,
"upper": completionsToolUpper,
"ip2n": completionsToolIP2n,
Expand All @@ -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.
Expand Down Expand Up @@ -81,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":
Expand Down
47 changes: 47 additions & 0 deletions ipinfo/cmd_tool_unmap.go
Original file line number Diff line number Diff line change
@@ -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 [<opts>] <ip>

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.
UmanShahzad marked this conversation as resolved.
Show resolved Hide resolved

Examples:
%[1]s unmap "::ffff:8.8.8.8"
%[1]s unmap "192.180.32.1"
%[1]s unmap "::ffff:192.168.1.1"
UmanShahzad marked this conversation as resolved.
Show resolved Hide resolved

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)
}
3 changes: 1 addition & 2 deletions lib/cmd_tool_next.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lib

import (
"fmt"
"net"
"github.com/spf13/pflag"
)

Expand Down Expand Up @@ -48,6 +47,6 @@ func CmdToolNext(
if err != nil {
fmt.Println(err)
}

return nil
}
45 changes: 45 additions & 0 deletions lib/cmd_tool_unmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lib

import (
"fmt"
"github.com/spf13/pflag"
"net/netip"
)

// 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:
addr, err := netip.ParseAddr(input)
if err != nil {
return err
}
fmt.Println(addr.Unmap())
default:
return ErrNotIP
}
return nil
}

return GetInputFrom(args, true, true, op)
}