Skip to content
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
8 changes: 8 additions & 0 deletions ipinfo/cmd_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
var completionsTool = &complete.Command{
Sub: map[string]*complete.Command{
"aggregate": completionsToolAggregate,
"lower": completionsToolLower,
"upper": completionsToolUpper,
"ip2n": completionsToolIP2n,
"n2ip": completionsToolN2IP,
"n2ip6": completionsToolN2IP6,
Expand All @@ -29,6 +31,8 @@ func printHelpTool() {

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.
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 @@ -62,6 +66,10 @@ func cmdTool() error {
switch {
case cmd == "aggregate":
err = cmdToolAggregate()
case cmd == "lower":
err = cmdToolLower()
case cmd == "upper":
err = cmdToolUpper()
case cmd == "ip2n":
err = cmdToolIP2n()
case cmd == "n2ip":
Expand Down
56 changes: 56 additions & 0 deletions ipinfo/cmd_tool_lower.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 completionsToolLower = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
"-q": predict.Nothing,
"--quiet": predict.Nothing,
},
}

func printHelpToolLower() {
fmt.Printf(
`Usage: %s tool lower [<opts>] <cidr | ip | ip-range | filepath>
Comment thread
UmanShahzad marked this conversation as resolved.

Description:
Comment thread
UmanShahzad marked this conversation as resolved.
Calculates the lower IP address (start address of a network) for the given inputs.
Input can be a mixture of IPs, IP ranges or CIDRs.

Examples:
# Finds lower IP for CIDR.
$ %[1]s tool lower 192.168.1.0/24

# Finds lower IP for IP range.
$ %[1]s tool lower 1.1.1.0-1.1.1.244

# Finds lower IPs from stdin.
$ cat /path/to/file.txt | %[1]s tool lower

# Find lower IPs from file.
$ %[1]s tool lower /path/to/file1.txt

Options:
--help, -h
show help.
--quiet, -q
quiet mode; suppress additional output.
`, progBase)
}

func cmdToolLower() (err error) {
f := lib.CmdToolLowerFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolLower(f, pflag.Args()[2:], printHelpToolLower)
}
56 changes: 56 additions & 0 deletions ipinfo/cmd_tool_upper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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 completionsToolUpper = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
"-q": predict.Nothing,
"--quiet": predict.Nothing,
},
}

func printHelpToolUpper() {
fmt.Printf(
`Usage: %s tool upper [<opts>] <cidr | ip | ip-range | filepath>
Comment thread
UmanShahzad marked this conversation as resolved.

Description:
Calculates the upper IP address (end address of a network) for the given inputs.
Input can be a mixture of Ips, IP ranges or CIDRs.

Examples:
# Finds upper IP for CIDR.
$ %[1]s tool upper 192.168.1.0/24

# Finds upper IP for IP range.
$ %[1]s tool upper 1.1.1.0-1.1.1.244

# Finds upper IPs from stdin.
$ cat /path/to/file.txt | %[1]s tool upper

# Find upper IPs from file.
$ %[1]s tool upper /path/to/file1.txt

Options:
--help, -h
Show help.
--quiet, -q
Quiet mode; suppress additional output.
`, progBase)
}

func cmdToolUpper() (err error) {
f := lib.CmdToolUpperFlags{}
f.Init()
pflag.Parse()

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

import (
"fmt"
"net"

Comment thread
UmanShahzad marked this conversation as resolved.
"github.com/spf13/pflag"
)

type CmdToolLowerFlags struct {
Help bool
Quiet bool
Comment thread
UmanShahzad marked this conversation as resolved.
}

func (f *CmdToolLowerFlags) Init() {
pflag.BoolVarP(
&f.Help,
"help", "h", false,
"show help.",
)
pflag.BoolVarP(
&f.Quiet,
"quiet", "q", false,
"quiet mode; suppress additional output.",
)
}

func CmdToolLower(
f CmdToolLowerFlags,
args []string,
printHelp func(),
) error {
if f.Help {
printHelp()
return nil
}
Comment thread
UmanShahzad marked this conversation as resolved.

actionFunc := func(input string, inputType INPUT_TYPE) error {
switch inputType {
case INPUT_TYPE_IP:
ActionForIP(input)
case INPUT_TYPE_IP_RANGE:
ActionForRange(input)
case INPUT_TYPE_CIDR:
ActionForCIDR(input)
default:
return ErrNotIP
}
return nil
}
err := GetInputFrom(args, true, true, actionFunc)
if err != nil {
fmt.Println(err)
}
Comment thread
UmanShahzad marked this conversation as resolved.

Comment thread
UmanShahzad marked this conversation as resolved.
return nil
}

Comment thread
UmanShahzad marked this conversation as resolved.
func ActionForIP(input string) {
fmt.Println(input)
}

func ActionForRange(input string) {
ipRange, err := IPRangeStrFromStr(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ipRange.Start)
}

func ActionForCIDR(input string) {
_, ipnet, err := net.ParseCIDR(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ipnet.IP)
}
78 changes: 78 additions & 0 deletions lib/cmd_tool_upper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package lib

import (
"fmt"

Comment thread
UmanShahzad marked this conversation as resolved.
"github.com/spf13/pflag"
)

type CmdToolUpperFlags struct {
Help bool
Quiet bool
}

func (f *CmdToolUpperFlags) Init() {
pflag.BoolVarP(
&f.Help,
"help", "h", false,
"show help.",
)
pflag.BoolVarP(
&f.Quiet,
"quiet", "q", false,
"quiet mode; suppress additional output.",
)
}

func CmdToolUpper(
f CmdToolUpperFlags,
args []string,
printHelp func(),
) error {
if f.Help {
printHelp()
return nil
}
Comment thread
UmanShahzad marked this conversation as resolved.

Comment thread
UmanShahzad marked this conversation as resolved.
actionFunc := func(input string, inputType INPUT_TYPE) error {
switch inputType {
case INPUT_TYPE_IP:
ActionForIPUpper(input)
case INPUT_TYPE_IP_RANGE:
ActionForRangeUpper(input)
case INPUT_TYPE_CIDR:
ActionForCIDRUpper(input)
default:
return ErrNotIP
}
return nil
}
err := GetInputFrom(args, true, true, actionFunc)
if err != nil {
fmt.Println(err)
}
Comment thread
UmanShahzad marked this conversation as resolved.

Comment thread
UmanShahzad marked this conversation as resolved.
return nil
}

func ActionForIPUpper(input string) {
fmt.Println(input)
}

func ActionForRangeUpper(input string) {
ipRange, err := IPRangeStrFromStr(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ipRange.End)
}

func ActionForCIDRUpper(input string) {
ipRange, err := IPRangeStrFromCIDR(input)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ipRange.End)
}