Skip to content

Commit 30cee54

Browse files
authored
Merge pull request #66 from rm-Umar/master
Added range2ip
2 parents fde6c1e + 75654fa commit 30cee54

File tree

18 files changed

+393
-26
lines changed

18 files changed

+393
-26
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ ipinfo/dist/
44
grepip/dist/
55
cidr2range/dist/
66
range2cidr/dist/
7+
range2ip/dist/
78
!ipinfo/dist/DEBIAN/
89
!grepip/dist/DEBIAN/
910
!cidr2range/dist/DEBIAN/
1011
!range2cidr/dist/DEBIAN/
12+
!range2ip/dist/DEBIAN/

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ Replace `<path>` with the required location.
134134

135135
## Additional CLIs
136136

137-
The `ipinfo` CLI has some subcommands like `grepip`, `cidr2range` and
138-
`range2cidr` which are also shipped as standalone binaries.
137+
The `ipinfo` CLI has some subcommands like `grepip`, `cidr2range`,
138+
`range2cidr` and `range2ip` which are also shipped as standalone binaries.
139139

140140
These binaries are available via all the **same installation methods** as
141141
mentioned above for `ipinfo`, except you must change only the name to the name
@@ -148,6 +148,7 @@ Currently these subcommands are separately shipped:
148148
| grepip | [1.2.0](https://github.com/ipinfo/cli/releases/tag/grepip-1.2.0) |
149149
| cidr2range | [1.2.0](https://github.com/ipinfo/cli/releases/tag/cidr2range-1.2.0) |
150150
| range2cidr | [1.2.0](https://github.com/ipinfo/cli/releases/tag/range2cidr-1.2.0) |
151+
| range2ip | [1.0.0](https://github.com/ipinfo/cli/releases/tag/range2ip-1.0.0) |
151152

152153
## Quick Start
153154

ipinfo/cmd_default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Commands:
2727
grepip grep for IPs matching criteria from any source.
2828
cidr2range convert CIDRs to IP ranges.
2929
range2cidr convert IP ranges to CIDRs.
30+
range2ip convert IP ranges to individual IPs within those ranges.
3031
cache manage the cache.
3132
login save an API token session.
3233
logout delete your current API token session.

ipinfo/cmd_range2ip.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/ipinfo/cli/lib"
7+
"github.com/ipinfo/cli/lib/complete"
8+
"github.com/ipinfo/cli/lib/complete/predict"
9+
"github.com/spf13/pflag"
10+
)
11+
12+
var completionsRange2IP = &complete.Command{
13+
Flags: map[string]complete.Predictor{
14+
"-h": predict.Nothing,
15+
"--help": predict.Nothing,
16+
},
17+
}
18+
19+
func printHelpRange2IP() {
20+
fmt.Printf(
21+
`Usage: %s range2ip [<opts>] <ip-range | filepath>
22+
23+
Description:
24+
Accepts IP ranges and file paths to files containing IP ranges, converting
25+
them all to individual IPs within those ranges.
26+
27+
$ %[1]s range2ip 8.8.8.0-8.8.8.255
28+
29+
IP ranges can be of the form "<start><sep><end>" where "<sep>" can be "," or
30+
"-", and "<start>" and "<end>" can be any 2 IPs; order does not matter.
31+
32+
33+
Options:
34+
--help, -h
35+
show help.
36+
`, progBase)
37+
}
38+
39+
func cmdRange2IP() error {
40+
f := lib.CmdRange2IPFlags{}
41+
f.Init()
42+
pflag.Parse()
43+
44+
return lib.CmdRange2IP(f, pflag.Args()[1:], printHelpRange2IP)
45+
}

ipinfo/completions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var completions = &complete.Command{
1515
"grepip": completionsGrepIP,
1616
"cidr2range": completionsCIDR2Range,
1717
"range2cidr": completionsRange2CIDR,
18+
"range2ip": completionsRange2IP,
1819
"cache": completionsCache,
1920
"login": completionsLogin,
2021
"logout": completionsLogout,

ipinfo/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func main() {
5757
err = cmdCIDR2Range()
5858
case cmd == "range2cidr":
5959
err = cmdRange2CIDR()
60+
case cmd == "range2ip":
61+
err = cmdRange2IP()
6062
case cmd == "cache":
6163
err = cmdCache()
6264
case cmd == "login":

lib/cmd_range2ip.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package lib
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/pflag"
7+
)
8+
9+
// CmdRange2IPFlags are flags expected by CmdRange2IP.
10+
type CmdRange2IPFlags struct {
11+
Help bool
12+
}
13+
14+
// Init initializes the common flags available to CmdRange2IP with sensible
15+
// defaults.
16+
//
17+
// pflag.Parse() must be called to actually use the final flag values.
18+
func (f *CmdRange2IPFlags) Init() {
19+
pflag.BoolVarP(
20+
&f.Help,
21+
"help", "h", false,
22+
"show help.",
23+
)
24+
}
25+
26+
func CmdRange2IP(f CmdRange2IPFlags, args []string, printHelp func()) error {
27+
if f.Help {
28+
printHelp()
29+
return nil
30+
}
31+
32+
// require args and/or stdin.
33+
stat, _ := os.Stdin.Stat()
34+
isStdin := (stat.Mode() & os.ModeCharDevice) == 0
35+
if len(args) == 0 && !isStdin {
36+
printHelp()
37+
return nil
38+
}
39+
40+
return IPListWriteFrom(args, true, true, true, false, true)
41+
}

lib/ip_list_write.go

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func IPListWriteFrom(
3939
}
4040

4141
if isPiped || isTyping || stat.Size() > 0 {
42-
IPListWriteFromStdin()
42+
IPListWriteFromStdin(ip, iprange, cidr)
4343
}
4444
}
4545

@@ -63,11 +63,10 @@ func IPListWriteFrom(
6363
}
6464

6565
if file && FileExists(input) {
66-
if err := IPListWriteFromFile(input); err == nil {
66+
if err := IPListWriteFromFile(input, ip, iprange, cidr); err == nil {
6767
continue
6868
}
6969
}
70-
7170
return ErrInvalidInput
7271
}
7372

@@ -80,8 +79,8 @@ func IPListWriteFromAllSrcs(inputs []string) error {
8079
return IPListWriteFrom(inputs, true, true, true, true, true)
8180
}
8281

83-
// IPListFromCIDRWrite is the same as IPListFromCIDR with O(1) memory by discarding
84-
// IPs after printing.
82+
// IPListFromCIDRWrite is the same as IPListFromCIDR with O(1) memory
83+
// by discarding IPs after printing.
8584
func IPListWriteFromCIDR(cidrStr string) error {
8685
_, ipnet, err := net.ParseCIDR(cidrStr)
8786
if err != nil {
@@ -161,25 +160,37 @@ func IPListWriteFromIPRangeStr(rStr string) error {
161160

162161
// IPListWriteFromReader returns a list of IPs after reading from a reader; the
163162
// reader should have IPs per-line.
164-
func IPListWriteFromReader(r io.Reader) {
163+
func IPListWriteAllFromReader(r io.Reader) {
164+
IPListWriteFromReader(r, true, true, true)
165+
}
166+
167+
// IPListWriteFromReader returns a list of IPs after reading from a reader
168+
// from selected sources; the reader should have IPs per-line.
169+
func IPListWriteFromReader(
170+
r io.Reader,
171+
ip bool,
172+
iprange bool,
173+
cidr bool) {
165174
scanner := bufio.NewScanner(r)
166175
for scanner.Scan() {
167-
ipStr := strings.TrimSpace(scanner.Text())
168-
if ipStr == "" {
176+
input := strings.TrimSpace(scanner.Text())
177+
if input == "" {
169178
break
170179
}
171180

172-
if err := IPListWriteFromIPRangeStr(ipStr); err == nil {
173-
continue
181+
if iprange {
182+
if err := IPListWriteFromIPRangeStr(input); err == nil {
183+
continue
184+
}
174185
}
175186

176-
if StrIsIPStr(ipStr) {
177-
fmt.Println(ipStr)
187+
if ip && StrIsIPStr(input) {
188+
fmt.Println(input)
178189
continue
179190
}
180191

181-
if StrIsCIDRStr(ipStr) {
182-
if err := IPListWriteFromCIDR(ipStr); err == nil {
192+
if cidr && StrIsCIDRStr(input) {
193+
if err := IPListWriteFromCIDR(input); err == nil {
183194
continue
184195
}
185196
}
@@ -190,25 +201,56 @@ func IPListWriteFromReader(r io.Reader) {
190201

191202
// IPListWriteFromStdin returns a list of IPs from a stdin; the IPs should be 1
192203
// per line.
193-
func IPListWriteFromStdin() {
194-
IPListWriteFromReader(os.Stdin)
204+
func IPListWriteAllFromStdin() {
205+
IPListWriteAllFromReader(os.Stdin)
206+
}
207+
208+
// IPListWriteFromStdin returns a list of IPs from a stdin from selected
209+
// sources; the IPs should be 1 per line.
210+
func IPListWriteFromStdin(
211+
ip bool,
212+
iprange bool,
213+
cidr bool) {
214+
IPListWriteFromReader(os.Stdin, ip, iprange, cidr)
195215
}
196216

197217
// IPListWriteFromFile returns a list of IPs found in a file.
198-
func IPListWriteFromFile(pathToFile string) error {
218+
func IPListWriteAllFromFile(pathToFile string) error {
219+
return IPListWriteFromFile(pathToFile, true, true, true)
220+
}
221+
222+
// IPListWriteFromSrcFile returns a list of IPs from selected sources found
223+
// in a file.
224+
func IPListWriteFromFile(
225+
pathToFile string,
226+
ip bool,
227+
iprange bool,
228+
cidr bool,
229+
) error {
199230
f, err := os.Open(pathToFile)
200231
if err != nil {
201232
return err
202233
}
203-
204-
IPListWriteFromReader(f)
234+
IPListWriteFromReader(f, ip, iprange, cidr)
205235
return nil
206236
}
207237

208-
// IPListWriteFromFiles returns a list of IPs found in a list of files.
209-
func IPListWriteFromFiles(paths []string) error {
238+
// IPListWriteFromFiles returns a list of IPs found in a list of files from
239+
// all sources.
240+
func IPListWriteAllFromFiles(paths []string) error {
241+
return IPListWriteFromFiles(paths, true, true, true)
242+
}
243+
244+
// IPListWriteFromFiles returns a list of IPs found in a list of files from
245+
// select sources.
246+
func IPListWriteFromFiles(
247+
paths []string,
248+
ip bool,
249+
iprange bool,
250+
cidr bool,
251+
) error {
210252
for _, p := range paths {
211-
if err := IPListWriteFromFile(p); err != nil {
253+
if err := IPListWriteFromFile(p, ip, iprange, cidr); err != nil {
212254
return err
213255
}
214256
}

range2ip/build-all-platforms.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Build binary for all platforms for version $1.
4+
5+
set -e
6+
7+
DIR=`dirname $0`
8+
ROOT=$DIR/..
9+
10+
VSN=$1
11+
12+
$ROOT/scripts/build-all-platforms.sh "range2ip" $VSN

range2ip/build.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
# Build local binary.
4+
5+
set -e
6+
7+
DIR=`dirname $0`
8+
ROOT=$DIR/..
9+
10+
$ROOT/scripts/build.sh "range2ip"

0 commit comments

Comments
 (0)