-
Notifications
You must be signed in to change notification settings - Fork 157
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 #68 from rm-Umar/master
Added cidr2ip
- Loading branch information
Showing
16 changed files
with
319 additions
and
3 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,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 "cidr2ip" $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 "cidr2ip" |
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) | ||
} |
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,14 @@ | ||
#!/bin/sh | ||
|
||
VSN=1.0.0 | ||
|
||
curl -LO https://github.com/ipinfo/cli/releases/download/cidr2ip-${VSN}/cidr2ip_${VSN}.deb | ||
sudo dpkg -i cidr2ip_${VSN}.deb | ||
rm cidr2ip_${VSN}.deb | ||
|
||
echo | ||
echo 'You can now run `cidr2ip`'. | ||
|
||
if [ -f "$0" ]; then | ||
rm $0 | ||
fi |
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,11 @@ | ||
Source: cidr2ip | ||
Section: utils | ||
Version: 1.0.0 | ||
Priority: optional | ||
Maintainer: IPinfo <[email protected]> | ||
Vcs-Git: https://github.com/ipinfo/cli | ||
Vcs-browser: https://github.com/ipinfo/cli | ||
Homepage: https://ipinfo.io | ||
Package: cidr2ip | ||
Architecture: amd64 | ||
Description: A simple tool for converting from CIDRs to to individual IPs within those CIDRs. |
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,16 @@ | ||
#!/bin/sh | ||
|
||
VSN=1.0.0 | ||
PLAT=darwin_amd64 | ||
|
||
curl -LO https://github.com/ipinfo/cli/releases/download/cidr2ip-${VSN}/cidr2ip_${VSN}_${PLAT}.tar.gz | ||
tar -xf cidr2ip_${VSN}_${PLAT}.tar.gz | ||
rm cidr2ip_${VSN}_${PLAT}.tar.gz | ||
mv cidr2ip_${VSN}_${PLAT} /usr/local/bin/cidr2ip | ||
|
||
echo | ||
echo 'You can now run `cidr2ip`'. | ||
|
||
if [ -f "$0" ]; then | ||
rm $0 | ||
fi |
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,129 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/fatih/color" | ||
"github.com/ipinfo/cli/lib" | ||
"github.com/ipinfo/cli/lib/complete/install" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
var progBase = filepath.Base(os.Args[0]) | ||
var version = "1.0.0" | ||
|
||
func printHelp() { | ||
fmt.Printf( | ||
`Usage: %s [<opts>] <cidrs | filepath> | ||
Description: | ||
Accepts CIDRs and file paths to files containing CIDRs, converting | ||
them all to individual IPs within those CIDRs. | ||
$ %[1]s 8.8.8.0/24 | ||
Options: | ||
General: | ||
--version, -v | ||
show binary release number. | ||
--help, -h | ||
show help. | ||
Completions: | ||
--completions-install | ||
attempt completions auto-installation for any supported shell. | ||
--completions-bash | ||
output auto-completion script for bash for manual installation. | ||
--completions-zsh | ||
output auto-completion script for zsh for manual installation. | ||
--completions-fish | ||
output auto-completion script for fish for manual installation. | ||
`, progBase) | ||
} | ||
|
||
func cmd() error { | ||
var fVsn bool | ||
var fCompletionsInstall bool | ||
var fCompletionsBash bool | ||
var fCompletionsZsh bool | ||
var fCompletionsFish bool | ||
|
||
f := lib.CmdCIDR2IPFlags{} | ||
f.Init() | ||
pflag.BoolVarP( | ||
&fVsn, | ||
"version", "v", false, | ||
"print binary release number.", | ||
) | ||
pflag.BoolVarP( | ||
&fCompletionsInstall, | ||
"completions-install", "", false, | ||
"attempt completions auto-installation for any supported shell.", | ||
) | ||
pflag.BoolVarP( | ||
&fCompletionsBash, | ||
"completions-bash", "", false, | ||
"output auto-completion script for bash for manual installation.", | ||
) | ||
pflag.BoolVarP( | ||
&fCompletionsZsh, | ||
"completions-zsh", "", false, | ||
"output auto-completion script for zsh for manual installation.", | ||
) | ||
pflag.BoolVarP( | ||
&fCompletionsFish, | ||
"completions-fish", "", false, | ||
"output auto-completion script for fish for manual installation.", | ||
) | ||
pflag.Parse() | ||
|
||
if fVsn { | ||
fmt.Println(version) | ||
return nil | ||
} | ||
|
||
if fCompletionsInstall { | ||
return install.Install(progBase) | ||
} | ||
if fCompletionsBash { | ||
installStr, err := install.BashCmd(progBase) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(installStr) | ||
return nil | ||
} | ||
if fCompletionsZsh { | ||
installStr, err := install.ZshCmd(progBase) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(installStr) | ||
return nil | ||
} | ||
if fCompletionsFish { | ||
installStr, err := install.FishCmd(progBase) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(installStr) | ||
return nil | ||
} | ||
|
||
return lib.CmdCIDR2IP(f, pflag.Args(), printHelp) | ||
} | ||
|
||
func main() { | ||
// obey NO_COLOR env var. | ||
if os.Getenv("NO_COLOR") != "" { | ||
color.NoColor = true | ||
} | ||
|
||
handleCompletions() | ||
|
||
if err := cmd(); err != nil { | ||
fmt.Printf("err: %v\n", err) | ||
} | ||
} |
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 and upload (to GitHub) for all platforms for version $1. | ||
|
||
set -e | ||
|
||
DIR=`dirname $0` | ||
ROOT=$DIR/.. | ||
|
||
VSN=$1 | ||
|
||
$ROOT/scripts/release.sh "cidr2ip" $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,41 @@ | ||
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 completionsCIDR2IP = &complete.Command{ | ||
Flags: map[string]complete.Predictor{ | ||
"-h": predict.Nothing, | ||
"--help": predict.Nothing, | ||
}, | ||
} | ||
|
||
func printHelpCIDR2IP() { | ||
fmt.Printf( | ||
`Usage: %s cidr2ip [<opts>] <cidrs | filepath> | ||
Description: | ||
Accepts CIDRs and file paths to files containing CIDRs, converting | ||
them all to individual IPs within those CIDRs. | ||
$ %[1]s cidr2ip 8.8.8.0/24 | ||
Options: | ||
--help, -h | ||
show help. | ||
`, progBase) | ||
} | ||
|
||
func cmdCIDR2IP() error { | ||
f := lib.CmdCIDR2IPFlags{} | ||
f.Init() | ||
pflag.Parse() | ||
|
||
return lib.CmdCIDR2IP(f, pflag.Args()[1:], printHelpCIDR2IP) | ||
} |
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
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,41 @@ | ||
package lib | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
// CmdCIDR2IPFlags are flags expected by CmdCIDR2IP. | ||
type CmdCIDR2IPFlags struct { | ||
Help bool | ||
} | ||
|
||
// Init initializes the common flags available to CmdCIDR2IP with sensible | ||
// defaults. | ||
// | ||
// pflag.Parse() must be called to actually use the final flag values. | ||
func (f *CmdCIDR2IPFlags) Init() { | ||
pflag.BoolVarP( | ||
&f.Help, | ||
"help", "h", false, | ||
"show help.", | ||
) | ||
} | ||
|
||
func CmdCIDR2IP(f CmdCIDR2IPFlags, args []string, printHelp func()) error { | ||
if f.Help { | ||
printHelp() | ||
return nil | ||
} | ||
|
||
// require args and/or stdin. | ||
stat, _ := os.Stdin.Stat() | ||
isStdin := (stat.Mode() & os.ModeCharDevice) == 0 | ||
if len(args) == 0 && !isStdin { | ||
printHelp() | ||
return nil | ||
} | ||
|
||
return IPListWriteFrom(args, true, false, false, true, true) | ||
} |
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 @@ gofmt -w \ | |
$ROOT/grepip \ | ||
$ROOT/cidr2range \ | ||
$ROOT/range2cidr \ | ||
$ROOT/range2ip | ||
$ROOT/range2ip \ | ||
$ROOT/cidr2ip |
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 |
---|---|---|
|
@@ -8,4 +8,5 @@ golint \ | |
./grepip/... \ | ||
./cidr2range/... \ | ||
./range2cidr/... \ | ||
./range2ip/... | ||
./range2ip/... \ | ||
./cidr2ip/... |