-
Notifications
You must be signed in to change notification settings - Fork 154
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 #121 from ipinfo/umar/splitcidr
splitcidr
- Loading branch information
Showing
22 changed files
with
534 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ cidr2ip/dist/ | |
range2cidr/dist/ | ||
range2ip/dist/ | ||
randip/dist/ | ||
splitcidr/dist/ |
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
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,40 @@ | ||
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 completionsSplitCIDR = &complete.Command{ | ||
Flags: map[string]complete.Predictor{ | ||
"-h": predict.Nothing, | ||
"--help": predict.Nothing, | ||
}, | ||
} | ||
|
||
func printHelpSplitCIDR() { | ||
fmt.Printf( | ||
`Usage: %s splitcidr <cidr> <split> | ||
Description: | ||
Splits a larger CIDR into smaller CIDRs. | ||
$ %[1]s splitcidr 8.8.8.0/24 25 | ||
Options: | ||
--help, -h | ||
show help. | ||
`, progBase) | ||
} | ||
|
||
func cmdSplitCIDR() error { | ||
f := lib.CmdSplitCIDRFlags{} | ||
f.Init() | ||
pflag.Parse() | ||
|
||
return lib.CmdSplitCIDR(f, pflag.Args()[1:], printHelpSplitCIDR) | ||
} |
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
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,84 @@ | ||
package lib | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
type CmdSplitCIDRFlags struct { | ||
Help bool | ||
} | ||
|
||
// Init initializes the common flags available to CmdSplitCIDR with sensible | ||
// defaults. | ||
// | ||
// pflag.Parse() must be called to actually use the final flag values. | ||
func (f *CmdSplitCIDRFlags) Init() { | ||
pflag.BoolVarP( | ||
&f.Help, | ||
"help", "h", false, | ||
"show help.", | ||
) | ||
} | ||
|
||
// CmdSplitCIDR is the common core logic for the splitcidr command. | ||
func CmdSplitCIDR( | ||
f CmdSplitCIDRFlags, | ||
args []string, | ||
printHelp func(), | ||
) error { | ||
if f.Help { | ||
printHelp() | ||
return nil | ||
} | ||
|
||
if len(args) != 2 { | ||
printHelp() | ||
return nil | ||
} | ||
|
||
cidrString := args[0] | ||
splitString := args[1] | ||
ip, _, err := net.ParseCIDR(cidrString) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
split, err := strconv.Atoi(splitString) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
if ip.To4() != nil { | ||
ipsubnet, err := IPSubnetFromCidr(cidrString) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
subs, err := ipsubnet.SplitCIDR(split) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, s := range subs { | ||
fmt.Println(s.ToCIDR()) | ||
} | ||
} else { | ||
ipsubnet, err := IP6SubnetFromCidr(cidrString) | ||
if err != nil { | ||
return err | ||
} | ||
subs, err := ipsubnet.SplitCIDR(split) | ||
if err != nil { | ||
return err | ||
} | ||
for _, s := range subs { | ||
fmt.Println(s.ToCIDR()) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
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 |
---|---|---|
|
@@ -14,4 +14,5 @@ gofmt -w \ | |
$ROOT/range2cidr \ | ||
$ROOT/range2ip \ | ||
$ROOT/cidr2ip \ | ||
$ROOT/splitcidr \ | ||
$ROOT/randip |
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 |
---|---|---|
|
@@ -11,4 +11,5 @@ golint \ | |
./range2cidr/... \ | ||
./range2ip/... \ | ||
./cidr2ip/... \ | ||
./splitcidr/... \ | ||
./randip/... |
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,6 @@ | ||
# syntax=docker/dockerfile:1 | ||
FROM alpine:latest | ||
|
||
WORKDIR /splitcidr | ||
COPY build/splitcidr ./ | ||
ENTRYPOINT ["/splitcidr/splitcidr"] |
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,12 @@ | ||
#!/bin/bash | ||
|
||
# Build binary for all platforms for version $1. | ||
|
||
set -e | ||
|
||
DIR=`dirname $0` | ||
ROOT=$DIR/.. | ||
|
||
VSN=$1 | ||
|
||
$ROOT/scripts/build-all-platforms.sh "splitcidr" $VSN |
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,10 @@ | ||
#!/bin/bash | ||
|
||
# Build local binary. | ||
|
||
set -e | ||
|
||
DIR=`dirname $0` | ||
ROOT=$DIR/.. | ||
|
||
$ROOT/scripts/build.sh "splitcidr" |
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,23 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/ipinfo/cli/lib/complete" | ||
"github.com/ipinfo/cli/lib/complete/predict" | ||
) | ||
|
||
var completions = &complete.Command{ | ||
Flags: map[string]complete.Predictor{ | ||
"-v": predict.Nothing, | ||
"--version": predict.Nothing, | ||
"-h": predict.Nothing, | ||
"--help": predict.Nothing, | ||
"--completions-install": predict.Nothing, | ||
"--completions-bash": predict.Nothing, | ||
"--completions-zsh": predict.Nothing, | ||
"--completions-fish": predict.Nothing, | ||
}, | ||
} | ||
|
||
func handleCompletions() { | ||
completions.Complete(progBase) | ||
} |
Oops, something went wrong.