Skip to content

Commit

Permalink
Merge pull request #40 from ipinfo/v2
Browse files Browse the repository at this point in the history
v2
  • Loading branch information
UmanShahzad authored May 26, 2021
2 parents 270c3ea + 6f04663 commit cd5d3c9
Show file tree
Hide file tree
Showing 190 changed files with 26,521 additions and 632 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.vim/
ipinfo/dist/
grepip/dist/
cidr2range/dist/
range2cidr/dist/
!ipinfo/dist/DEBIAN/
!grepip/dist/DEBIAN/
!cidr2range/dist/DEBIAN/
!range2cidr/dist/DEBIAN/
62 changes: 54 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ brew install ipinfo-cli
OR to install the latest `amd64` version without automatic updates:

```bash
curl -Ls https://github.com/ipinfo/cli/releases/download/ipinfo-1.1.5/macos.sh | sh
curl -Ls https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.0/macos.sh | sh
```

### Debian / Ubuntu (amd64)

```bash
curl -Ls https://github.com/ipinfo/cli/releases/download/ipinfo-1.1.5/deb.sh | sh
curl -Ls https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.0/deb.sh | sh
```

OR

```bash
curl -LO https://github.com/ipinfo/cli/releases/download/ipinfo-1.1.5/ipinfo_1.1.5.deb
sudo dpkg -i ipinfo_1.1.5.deb
curl -LO https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.0/ipinfo_2.0.0.deb
sudo dpkg -i ipinfo_2.0.0.deb
```

### Using `go get`
Expand Down Expand Up @@ -92,12 +92,12 @@ After choosing a platform `PLAT` from above, run:

```bash
# for Windows, use ".zip" instead of ".tar.gz"
curl -LO https://github.com/ipinfo/cli/releases/download/ipinfo-1.1.5/ipinfo_1.1.5_${PLAT}.tar.gz
curl -LO https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.0/ipinfo_2.0.0_${PLAT}.tar.gz
# OR
wget https://github.com/ipinfo/cli/releases/download/ipinfo-1.1.5/ipinfo_1.1.5_${PLAT}.tar.gz
wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.0/ipinfo_2.0.0_${PLAT}.tar.gz

tar -xvf ipinfo_1.1.5_${PLAT}.tar.gz
mv ipinfo_1.1.5_${PLAT} /usr/local/bin/ipinfo
tar -xvf ipinfo_2.0.0_${PLAT}.tar.gz
mv ipinfo_2.0.0_${PLAT} /usr/local/bin/ipinfo
```

### Using `git`
Expand Down Expand Up @@ -198,6 +198,52 @@ or `--help` message for each command. For example:
ipinfo 8.8.8.8 --help
```

## Auto-Completion

Auto-completion is supported for at least the following shells:

```
bash
zsh
fish
```

NOTE: it may work for other shells as well because the implementation is in
Golang and is not necessarily shell-specific.

### Installation

Installing auto-completions is as simple as running one command (works for
`bash`, `zsh` and `fish` shells):

```bash
ipinfo completion install
```

If you want to customize the installation process (e.g. in case the
auto-installation doesn't work as expected), you can request the actual
completion script for each shell:

```bash
# get bash completion script
ipinfo completion bash

# get zsh completion script
ipinfo completion zsh

# get fish completion script
ipinfo completion fish
```

### Shell not listed?

If your shell is not listed here, you can open an issue.

Note that as long as the `COMP_LINE` environment variable is provided to the
binary itself, it will output completion results. So if your shell provides a
way to pass `COMP_LINE` on auto-completion attempts to a binary, then have your
shell do that with the `ipinfo` binary itself (or any of our binaries).

## Data

The amount of data you get back per lookup depends upon how much data you have
Expand Down
12 changes: 12 additions & 0 deletions cidr2range/build-all-platforms.sh
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 "cidr2range" $VSN
10 changes: 10 additions & 0 deletions cidr2range/build.sh
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 "cidr2range"
23 changes: 23 additions & 0 deletions cidr2range/completions.go
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)
}
14 changes: 14 additions & 0 deletions cidr2range/deb.sh
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/cidr2range-${VSN}/cidr2range_${VSN}.deb
sudo dpkg -i cidr2range_${VSN}.deb
rm cidr2range_${VSN}.deb

echo
echo 'You can now run `cidr2range`'.

if [ -f "$0" ]; then
rm $0
fi
11 changes: 11 additions & 0 deletions cidr2range/dist/DEBIAN/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Source: cidr2range
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: cidr2range
Architecture: amd64
Description: A simple tool for converting from CIDRs to IP ranges.
16 changes: 16 additions & 0 deletions cidr2range/macos.sh
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/cidr2range-${VSN}/cidr2range_${VSN}_${PLAT}.tar.gz
tar -xf cidr2range_${VSN}_${PLAT}.tar.gz
rm cidr2range_${VSN}_${PLAT}.tar.gz
mv cidr2range_${VSN}_${PLAT} /usr/local/bin/cidr2range

echo
echo 'You can now run `cidr2range`'.

if [ -f "$0" ]; then
rm $0
fi
146 changes: 146 additions & 0 deletions cidr2range/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
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>] <cidr | filepath>
Description:
Accepts CIDRs and file paths to files containing CIDRs, converting them all
to IP ranges.
If a file is input, it is assumed that the CIDR to convert is the first entry
of each line (separated by '\n'). All other data remains the same.
The IP range output is of the form "<start>-<end>" where "<start>" comes
before or is equal to "<end>" in numeric value.
Examples:
# Get the range for CIDR 1.1.1.0/30.
$ %[1]s 1.1.1.0/30
# Convert CIDR entries to IP ranges in 2 files.
$ %[1]s /path/to/file1.txt /path/to/file2.txt
# Convert CIDR entries to IP ranges from stdin.
$ cat /path/to/file1.txt | %[1]s
# Convert CIDR entries to IP ranges from stdin and a file.
$ cat /path/to/file1.txt | %[1]s /path/to/file2.txt
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.CmdCIDR2RangeFlags{}
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.CmdCIDR2Range(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)
}
}
12 changes: 12 additions & 0 deletions cidr2range/release.sh
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 "cidr2range" $VSN
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ module github.com/ipinfo/cli
go 1.16

require (
github.com/hashicorp/go-multierror v1.1.1
github.com/posener/script v1.1.5
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.10.0
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/ipinfo/go/v2 v2.4.0
github.com/jszwec/csvutil v1.4.0
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210420205809-ac73e9fd8988 // indirect
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit cd5d3c9

Please sign in to comment.