Skip to content
Closed
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 @@ -14,6 +14,7 @@ var completionsTool = &complete.Command{
"aggregate": completionsToolAggregate,
"lower": completionsToolLower,
"upper": completionsToolUpper,
"is_v4": completionsToolIsV4,
"ip2n": completionsToolIP2n,
"n2ip": completionsToolN2IP,
"n2ip6": completionsToolN2IP6,
Expand All @@ -33,6 +34,7 @@ Commands:
aggregate aggregate IPs, IP ranges, and CIDRs.
lower get start IP of IPs, IP ranges, and CIDRs.
upper get end IP of IPs, IP ranges, and CIDRs.
is_v4 check if IPs are IPv4.
ip2n converts an IPv4 or IPv6 address to its decimal representation.
n2ip evaluates a mathematical expression and converts it to an IPv4 or IPv6.
n2ip6 evaluates a mathematical expression and converts it to an IPv6.
Expand Down Expand Up @@ -70,6 +72,8 @@ func cmdTool() error {
err = cmdToolLower()
case cmd == "upper":
err = cmdToolUpper()
case cmd == "is_v4":
err = cmdToolIsV4()
case cmd == "ip2n":
err = cmdToolIP2n()
case cmd == "n2ip":
Expand Down
42 changes: 42 additions & 0 deletions ipinfo/cmd_tool_is_v4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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"
)

var completionsToolIsV4 = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
},
}

func printHelpToolIsV4() {
fmt.Printf(
`Usage: %s tool is_v4 <ip>

Description:
check if IPs are IPv4.

Examples:
$ %[1]s tool is_v4 "192.168.1.1"
$ %[1]s tool is_v4 "1.1.1.1"

Options:
General:
--help, -h
show help.
`, progBase)
}

func cmdToolIsV4() error {
f := lib.CmdToolIsV4Flags{}
f.Init()
pflag.Parse()

return lib.CmdToolIsV4(f, pflag.Args()[2:], printHelpToolIsV4)
}
34 changes: 34 additions & 0 deletions lib/cmd_tool_is_v4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lib

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

// CmdToolIsV4Flags are flags expected by CmdToolIsV4.
type CmdToolIsV4Flags struct {
Help bool
}

// Init initializes the common flags available to CmdToolIsV4 with sensible
func (f *CmdToolIsV4Flags) Init() {
pflag.BoolVarP(
&f.Help,
"help", "h", false,
"show help.",
)
}

func CmdToolIsV4(f CmdToolIsV4Flags, args []string, printHelp func()) error {
if f.Help {
printHelp()
return nil
}

op := func(input string, input_type INPUT_TYPE) error {
fmt.Printf("%s %t\n", input, StrIsIPv4Str(input))
return nil
}

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