Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP calculator #154

Merged
merged 53 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
860f3d4
Implement IP to Integer Conversion as Part of Issue #108
Harisabdullah Jul 24, 2023
b0ceb39
This commit is a part of Issue #108, which involves implementing the …
Harisabdullah Jul 24, 2023
7fdd0be
This commit is a part of Issue #108, which involves implementing the …
Harisabdullah Jul 24, 2023
a184ba5
This commit is a part of Issue #108, which involves implementing the …
Harisabdullah Jul 24, 2023
17af602
This commit is a part of Issue #108, which involves implementing the …
Harisabdullah Jul 24, 2023
27e310e
This commit is a part of Issue #108, which involves implementing the …
Harisabdullah Jul 24, 2023
bdabecf
This commit is a part of Issue #108, which involves implementing the …
Harisabdullah Jul 24, 2023
edd3de9
refactoring
Harisabdullah Jul 24, 2023
08801dd
Update ipinfo/cmd_calc.go
Harisabdullah Jul 24, 2023
530454e
Update ipinfo/cmd_calc.go
Harisabdullah Jul 24, 2023
323b2a4
Update ipinfo/cmd_calc.go
Harisabdullah Jul 24, 2023
2f51394
Update ipinfo/cmd_calc.go
Harisabdullah Jul 24, 2023
5fa615a
Update ipinfo/cmd_calc_infix.go
Harisabdullah Jul 24, 2023
444621e
Update ipinfo/cmd_calc_infix.go
Harisabdullah Jul 24, 2023
dfd3ef8
Update ipinfo/cmd_calc_infix.go
Harisabdullah Jul 24, 2023
ba19d99
Moving 'calc', 'n2ip', 'n2ip6', and 'ip2n' in hep message
Harisabdullah Jul 24, 2023
49ee322
Merge remote-tracking branch 'origin/haris/be-987' into haris/be-987
Harisabdullah Jul 24, 2023
8ce8504
Update ipinfo/cmd_ip2n.go
Harisabdullah Jul 24, 2023
82f406c
Update ipinfo/cmd_ip2n.go
Harisabdullah Jul 24, 2023
92068c9
Update ipinfo/cmd_n2ip.go
Harisabdullah Jul 24, 2023
1a8a3b1
Fixed repetition of progbase, and removed unused flags
Harisabdullah Jul 24, 2023
fb5dd9d
Relocated 'calc', 'ip2n', 'n2ip', and 'n2ip6'
Harisabdullah Jul 24, 2023
462a1b5
making flags work
Harisabdullah Jul 24, 2023
4f447d7
Merge remote-tracking branch 'origin/haris/be-987' into haris/be-987
Harisabdullah Jul 24, 2023
a7d3129
Moved Logic to lib
Harisabdullah Jul 24, 2023
6129a41
Moved Logic to lib
Harisabdullah Jul 24, 2023
114eeb1
sorting imports
Harisabdullah Jul 24, 2023
efffdd4
1) Moving logic to ```lib/```
Harisabdullah Jul 25, 2023
c5dc299
1) Fixed a parsing from string to float which was causing information…
Harisabdullah Jul 25, 2023
095ffd2
removed print statement
Harisabdullah Jul 25, 2023
992a32b
Fixed a bug where calc was not accepting alphabets for ipv6
Harisabdullah Jul 25, 2023
b5346c9
Updated Comments
Harisabdullah Jul 26, 2023
5f2b34d
Refactor: Moved logic to ```lib/```
Harisabdullah Jul 26, 2023
92bb8f2
- Declared constant for precision while parsing string to float
Harisabdullah Jul 27, 2023
378ac63
- Renamed ```lib/calc_infix.go``` to ```lib/cmd_calc.go```
Harisabdullah Jul 31, 2023
50a58ac
Moved ```StrIsIPv4Str``` and ```StrIsIPv6Str``` to ```lib/ip_str.go```
Harisabdullah Jul 31, 2023
a28331d
Moved ```StrIsIPv4Str``` and ```StrIsIPv6Str``` to ```lib/ip_str.go```
Harisabdullah Jul 31, 2023
7e652c1
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
52a91f1
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
6ac24da
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
c012c93
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
addd2dd
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
2f77e43
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
5e55a89
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
aa50cae
Suggested changes applied
Harisabdullah Jul 31, 2023
8586480
Update lib/cmd_calc.go
Harisabdullah Jul 31, 2023
360c610
* Nested `n2ip, n2ip6, and ip2n` sub commands inside `tool` sub command
Harisabdullah Aug 1, 2023
8efc978
Merge remote-tracking branch 'origin/haris/be-987' into haris/be-987
Harisabdullah Aug 1, 2023
55fa316
Removed unnecessary color flags
Harisabdullah Aug 1, 2023
5bd8496
Update lib/cmd_tool_n2ip.go
Harisabdullah Aug 3, 2023
ab8fbb0
- Regex is now global to avoid redundant compilation
Harisabdullah Aug 3, 2023
2644c1f
Update lib/cmd_tool_n2ip6.go
Harisabdullah Aug 3, 2023
7fc3cc6
`MustCompilePOSIX`instead of `MustCompile`.
Harisabdullah Aug 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions ipinfo/cmd_calc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 completionsCalc = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
},
}

// printHelpCalc prints the help message for the "calc" command.
func printHelpCalc() {
fmt.Printf(
`Usage: %s calc <expression> [<opts>]

Description:
Evaluate a mathematical expression and print the result.

Examples:
%[1]s calc "2*2828-1"
%[1]s calc "190.87.89.1*2"
%[1]s calc "2001:0db8:85a3:0000:0000:8a2e:0370:7334*6"

Options:
General:
--help, -h
show help.
`, progBase)
}

// cmdCalc is the handler for the "calc" command.
func cmdCalc() error {
Harisabdullah marked this conversation as resolved.
Show resolved Hide resolved
f := lib.CmdCalcFlags{}
f.Init()
pflag.Parse()

Harisabdullah marked this conversation as resolved.
Show resolved Hide resolved
return lib.CmdCalc(f, pflag.Args()[1:], printHelpCalc)
}
1 change: 1 addition & 0 deletions ipinfo/cmd_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Commands:
randip Generates random IPs.
splitcidr splits a larger CIDR into smaller CIDRs.
mmdb read, import and export mmdb files.
calc evaluates a mathematical expression that may contain IP addresses.
tool misc. tools related to IPs, IP ranges and CIDRs.
download download free ipinfo database files.
cache manage the cache.
Expand Down
12 changes: 12 additions & 0 deletions ipinfo/cmd_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
var completionsTool = &complete.Command{
Sub: map[string]*complete.Command{
"aggregate": completionsToolAggregate,
"ip2n": completionsToolIP2n,
"n2ip": completionsToolN2IP,
"n2ip6": completionsToolN2IP6,
},
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
Expand All @@ -26,6 +29,9 @@ func printHelpTool() {

Commands:
aggregate aggregate IPs, IP ranges, and CIDRs.
ip2n converts an IPv4 or IPv6 address to its decimal representation.
n2ip evaluates a mathematical expression and converts it to an IPv4 or IPv6.
n2ip6 evaluates a mathematical expression and converts it to an IPv6.

Options:
--help, -h
Expand Down Expand Up @@ -56,6 +62,12 @@ func cmdTool() error {
switch {
case cmd == "aggregate":
err = cmdToolAggregate()
case cmd == "ip2n":
err = cmdToolIP2n()
case cmd == "n2ip":
err = cmdToolN2IP()
case cmd == "n2ip6":
err = cmdToolN2IP6()
default:
err = toolHelp()
}
Expand Down
47 changes: 47 additions & 0 deletions ipinfo/cmd_tool_ip2n.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 completionsToolIP2n = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
},
}

// printHelpToolIp2n prints the help message for the "ip2n" command.
func printHelpToolIp2n() {
fmt.Printf(
`Usage: %s tool ip2n <ip>

Description:
Converts an IPv4 or IPv6 address to its decimal representation.

Examples:
%[1]s ip2n "190.87.89.1"
%[1]s ip2n "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
%[1]s ip2n "2001:0db8:85a3::8a2e:0370:7334"
%[1]s ip2n "::7334"
%[1]s ip2n "7334::"

Options:
General:
--help, -h
show help.
`, progBase)
}

// cmdToolIP2n is the handler for the "ip2n" command.
func cmdToolIP2n() error {
f := lib.CmdToolIP2nFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolIP2n(f, pflag.Args()[2:], printHelpToolIp2n)
}
53 changes: 53 additions & 0 deletions ipinfo/cmd_tool_n2ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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"
)

// cmdToolN2IP is the handler for the "n2ip" command.
var completionsToolN2IP = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
"-6": predict.Set(predictReadFmts),
"--ipv6": predict.Set(predictReadFmts),
},
}

// printHelpToolN2IP prints the help message for the "n2ip" command.
func printHelpToolN2IP() {
fmt.Printf(
`Usage: %s n2ip tool [<opts>] <number>

Description:
Converts a given numeric representation to its corresponding IPv4 or IPv6 address,
and can also evaluate a mathematical expression for conversion.

Examples:
%[1]s n2ip "4294967295 + 87"
%[1]s n2ip "4294967295" --ipv6
%[1]s n2ip -6 "201523715"
%[1]s n2ip "51922968585348276285304963292200960"
%[1]s n2ip "a:: - 4294967295"

Options:
General:
--help, -h
show help.
--ipv6, -6
force conversion to IPv6 address
`, progBase)
}

// cmdToolN2IP is the handler for the "n2ip" command.
func cmdToolN2IP() error {
f := lib.CmdToolN2IPFlags{}
f.Init()
pflag.Parse()

return lib.CmdToolN2IP(f, pflag.Args()[2:], printHelpToolN2IP)
}
48 changes: 48 additions & 0 deletions ipinfo/cmd_tool_n2ip6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 completionsToolN2IP6 = &complete.Command{
Flags: map[string]complete.Predictor{
"-h": predict.Nothing,
"--help": predict.Nothing,
},
}

// printHelpToolN2IP6 prints the help message for the "n2ip6" command.
func printHelpToolN2IP6() {
fmt.Printf(
`Usage: %s tool n2ip6 [<opts>] <number>

Description:
Converts a given numeric representation to its corresponding IPv6 address,
and can also evaluate a mathematical expression for conversion.

Examples:
%[1]s n2ip6 "4294967295 + 87"
%[1]s n2ip6 "4294967295"
%[1]s n2ip6 "201523715"
%[1]s n2ip6 "51922968585348276285304963292200960"
%[1]s n2ip6 "a:: - 4294967295"

Options:
General:
--help, -h
show help.
`, progBase)
}

// cmdToolN2IP6 is the handler for the "n2ip6" command.
func cmdToolN2IP6() error {
f := lib.CmdToolN2IP6Flags{}
f.Init()
pflag.Parse()

return lib.CmdToolN2IP6(f, pflag.Args()[2:], printHelpToolN2IP6)
}
1 change: 1 addition & 0 deletions ipinfo/completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var completions = &complete.Command{
"randip": completionsRandIP,
"splitcidr": completionsSplitCIDR,
"mmdb": completionsMmdb,
"calc": completionsCalc,
"tool": completionsTool,
"download": completionsDownload,
"cache": completionsCache,
Expand Down
2 changes: 2 additions & 0 deletions ipinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func main() {
err = cmdSplitCIDR()
case cmd == "mmdb":
err = cmdMmdb()
case cmd == "calc":
err = cmdCalc()
case cmd == "download":
err = cmdDownload()
case cmd == "tool":
Expand Down
Loading