From 7cd775242e2f0cae26d2cd31c5e1b87f7afc1a93 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Wed, 17 Nov 2021 14:40:30 +0500 Subject: [PATCH 1/3] Added prips binary --- README.md | 3 +- prips/build-all-platforms.sh | 12 ++++ prips/build.sh | 10 +++ prips/completions.go | 23 +++++++ prips/deb.sh | 14 ++++ prips/dist/DEBIAN/control | 11 +++ prips/macos.sh | 16 +++++ prips/main.go | 129 +++++++++++++++++++++++++++++++++++ prips/release.sh | 12 ++++ scripts/fmt.sh | 1 + scripts/lint.sh | 1 + 11 files changed, 231 insertions(+), 1 deletion(-) create mode 100755 prips/build-all-platforms.sh create mode 100755 prips/build.sh create mode 100644 prips/completions.go create mode 100755 prips/deb.sh create mode 100644 prips/dist/DEBIAN/control create mode 100755 prips/macos.sh create mode 100644 prips/main.go create mode 100755 prips/release.sh diff --git a/README.md b/README.md index cac4f984..a1007d2c 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Replace `` with the required location. ## Additional CLIs -The `ipinfo` CLI has some subcommands like `grepip`, `cidr2range`, `cidr2ip`, +The `ipinfo` CLI has some subcommands like `grepip`, `prips`, `cidr2range`, `cidr2ip`, `range2cidr`, `range2ip` and `randip` which are also shipped as standalone binaries. These binaries are available via all the **same installation methods** as @@ -146,6 +146,7 @@ Currently these subcommands are separately shipped: | CLI | Version | | ---------- | ------- | | grepip | [1.2.1](https://github.com/ipinfo/cli/releases/tag/grepip-1.2.1) | +| prips | [1.0.1](https://github.com/ipinfo/cli/releases/tag/prips-1.0.1) | | cidr2range | [1.2.0](https://github.com/ipinfo/cli/releases/tag/cidr2range-1.2.0) | | cidr2ip | [1.0.0](https://github.com/ipinfo/cli/releases/tag/cidr2ip-1.0.0) | | range2cidr | [1.2.0](https://github.com/ipinfo/cli/releases/tag/range2cidr-1.2.0) | diff --git a/prips/build-all-platforms.sh b/prips/build-all-platforms.sh new file mode 100755 index 00000000..eb8005b1 --- /dev/null +++ b/prips/build-all-platforms.sh @@ -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 "prips" $VSN diff --git a/prips/build.sh b/prips/build.sh new file mode 100755 index 00000000..1ca98449 --- /dev/null +++ b/prips/build.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Build local binary. + +set -e + +DIR=`dirname $0` +ROOT=$DIR/.. + +$ROOT/scripts/build.sh "prips" diff --git a/prips/completions.go b/prips/completions.go new file mode 100644 index 00000000..29d9822a --- /dev/null +++ b/prips/completions.go @@ -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) +} diff --git a/prips/deb.sh b/prips/deb.sh new file mode 100755 index 00000000..77d3fa6d --- /dev/null +++ b/prips/deb.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +VSN=1.0.0 + +curl -LO https://github.com/ipinfo/cli/releases/download/prips-${VSN}/prips_${VSN}.deb +sudo dpkg -i prips_${VSN}.deb +rm prips_${VSN}.deb + +echo +echo 'You can now run `prips`'. + +if [ -f "$0" ]; then + rm $0 +fi diff --git a/prips/dist/DEBIAN/control b/prips/dist/DEBIAN/control new file mode 100644 index 00000000..89b538a3 --- /dev/null +++ b/prips/dist/DEBIAN/control @@ -0,0 +1,11 @@ +Source: prips +Section: utils +Version: 1.0.0 +Priority: optional +Maintainer: IPinfo +Vcs-Git: https://github.com/ipinfo/cli +Vcs-browser: https://github.com/ipinfo/cli +Homepage: https://ipinfo.io +Package: prips +Architecture: amd64 +Description: A tool for printing IPs. diff --git a/prips/macos.sh b/prips/macos.sh new file mode 100755 index 00000000..d31347e1 --- /dev/null +++ b/prips/macos.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +VSN=1.0.0 +PLAT=darwin_amd64 + +curl -LO https://github.com/ipinfo/cli/releases/download/prips-${VSN}/prips_${VSN}_${PLAT}.tar.gz +tar -xf prips_${VSN}_${PLAT}.tar.gz +rm prips_${VSN}_${PLAT}.tar.gz +mv prips_${VSN}_${PLAT} /usr/local/bin/prips + +echo +echo 'You can now run `prips`'. + +if [ -f "$0" ]; then + rm $0 +fi diff --git a/prips/main.go b/prips/main.go new file mode 100644 index 00000000..474a2ed0 --- /dev/null +++ b/prips/main.go @@ -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 [] + +Description: + Accepts CIDRs (e.g. 8.8.8.0/24) and IP ranges (e.g. 8.8.8.0-8.8.8.255). + +Examples: + # List all IPs in a CIDR. + $ %[1]s 8.8.8.0/24 + + # List all IPs in multiple CIDRs. + $ %[1]s 8.8.8.0/24 8.8.2.0/24 8.8.1.0/24 + + # List all IPs in an IP range. + $ %[1]s 8.8.8.0-8.8.8.255 + + # List all IPs in multiple CIDRs and IP ranges. + $ %[1]s 1.1.1.0/30 8.8.8.0-8.8.8.255 2.2.2.0/30 7.7.7.0,7.7.7.10 + + # List all IPs from stdin input (newline-separated). + $ echo '1.1.1.0/30\n8.8.8.0-8.8.8.255\n7.7.7.0,7.7.7.10' | %[1]s + +Options: + --help, -h + show help. +`, progBase) +} + +func cmd() error { + var fVsn bool + var fCompletionsInstall bool + var fCompletionsBash bool + var fCompletionsZsh bool + var fCompletionsFish bool + + f := lib.CmdPripsFlags{} + 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.CmdPrips(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) + } +} diff --git a/prips/release.sh b/prips/release.sh new file mode 100755 index 00000000..427749bb --- /dev/null +++ b/prips/release.sh @@ -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 "prips" $VSN diff --git a/scripts/fmt.sh b/scripts/fmt.sh index 9250b6cd..047a48c4 100755 --- a/scripts/fmt.sh +++ b/scripts/fmt.sh @@ -9,6 +9,7 @@ gofmt -w \ $ROOT/lib \ $ROOT/ipinfo \ $ROOT/grepip \ + $ROOT/prips \ $ROOT/cidr2range \ $ROOT/range2cidr \ $ROOT/range2ip \ diff --git a/scripts/lint.sh b/scripts/lint.sh index d94728df..20920f3d 100755 --- a/scripts/lint.sh +++ b/scripts/lint.sh @@ -6,6 +6,7 @@ golint \ ./lib/... \ ./ipinfo/... \ ./grepip/... \ + ./prips/... \ ./cidr2range/... \ ./range2cidr/... \ ./range2ip/... \ From 8d078843713056ee63e88e7778590e8a32077aff Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Wed, 17 Nov 2021 19:55:58 +0500 Subject: [PATCH 2/3] Added prips binary --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a1007d2c..b0098aa1 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ Currently these subcommands are separately shipped: | CLI | Version | | ---------- | ------- | | grepip | [1.2.1](https://github.com/ipinfo/cli/releases/tag/grepip-1.2.1) | -| prips | [1.0.1](https://github.com/ipinfo/cli/releases/tag/prips-1.0.1) | +| prips | [1.0.1](https://github.com/ipinfo/cli/releases/tag/prips-1.0.0) | | cidr2range | [1.2.0](https://github.com/ipinfo/cli/releases/tag/cidr2range-1.2.0) | | cidr2ip | [1.0.0](https://github.com/ipinfo/cli/releases/tag/cidr2ip-1.0.0) | | range2cidr | [1.2.0](https://github.com/ipinfo/cli/releases/tag/range2cidr-1.2.0) | From c2f3b24a8b66866f4ca56fbd01c379bbcfbea1a2 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Wed, 17 Nov 2021 21:14:49 +0500 Subject: [PATCH 3/3] Added prips binary --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b0098aa1..e65b31c6 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ Currently these subcommands are separately shipped: | CLI | Version | | ---------- | ------- | | grepip | [1.2.1](https://github.com/ipinfo/cli/releases/tag/grepip-1.2.1) | -| prips | [1.0.1](https://github.com/ipinfo/cli/releases/tag/prips-1.0.0) | +| prips | [1.0.0](https://github.com/ipinfo/cli/releases/tag/prips-1.0.0) | | cidr2range | [1.2.0](https://github.com/ipinfo/cli/releases/tag/cidr2range-1.2.0) | | cidr2ip | [1.0.0](https://github.com/ipinfo/cli/releases/tag/cidr2ip-1.0.0) | | range2cidr | [1.2.0](https://github.com/ipinfo/cli/releases/tag/range2cidr-1.2.0) |