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 all 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 @@ -18,6 +18,7 @@ var completionsTool = &complete.Command{
"is_v6": completionsToolIsV6,
"is_valid": completionsToolIsValid,
"is_one_ip": completionsToolIsOneIp,
"unmap": completionsToolUnmap,
"lower": completionsToolLower,
"upper": completionsToolUpper,
"is_v4in6": completionsToolIs4In6,
Expand All @@ -44,6 +45,7 @@ Commands:
is_v6 reports whether input is an IPv6 address.
is_valid reports whether an IP is valid.
is_one_ip checks whether a CIDR or IP Range contains exactly one IP.
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.
is_v4in6 get whether the IP is an IPv4-mapped IPv6 address.
Expand Down Expand Up @@ -92,6 +94,8 @@ func cmdTool() error {
err = cmdToolIsValid()
case cmd == "is_one_ip":
err = cmdToolIsOneIp()
case cmd == "unmap":
err = cmdToolUnmap()
case cmd == "lower":
err = cmdToolLower()
case cmd == "upper":
Expand Down
49 changes: 49 additions & 0 deletions ipinfo/cmd_tool_unmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 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 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:
--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)
}
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)
}