-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #173 from ipinfo/haris/BE-2218
Implements `ipinfo tool unmap`
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |