Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 5 additions & 5 deletions cmd/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

var aggregateCmd = &cobra.Command{
Aliases: []string{"agg"},
Use: "aggregate <prefix> [<prefix>...]",
Short: "Aggregate the specified prefixes",
Aliases: []string{"agg"},
Use: "aggregate <prefix> [<prefix>...]",
Short: "Aggregate the specified prefixes",
SilenceErrors: true,
Args: cobra.MinimumNArgs(1),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var ps []ipaddr.Prefix
var agg []string
Expand All @@ -31,7 +31,7 @@ var aggregateCmd = &cobra.Command{
agg = append(agg, p.String())
}

fmt.Printf("Prefixes: %s\n", strings.Join(agg, ", "))
fmt.Printf("> %s%s\n%s", Purple, strings.Join(agg, ", "), Reset)
return nil
},
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

var excludeCmd = &cobra.Command{
Aliases: []string{"exc"},
Use: "exclude <prefix> <excluded>",
Short: "Exclude the specified prefix from the main prefix",
Aliases: []string{"exc"},
Use: "exclude <prefix> <excluded>",
Short: "Exclude the specified prefix from the main prefix",
SilenceErrors: true,
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
var exc []string

Expand All @@ -30,7 +30,7 @@ var excludeCmd = &cobra.Command{
exc = append(exc, sp.String())
}

fmt.Printf("Prefixes: %s\n", strings.Join(exc, ", "))
fmt.Printf("> %s%s%s\n", Purple, strings.Join(exc, ", "), Reset)
return nil
},
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/overlap.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package cmd

import (
"fmt"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/cobra"
)

var overlapCmd = &cobra.Command{
Aliases: []string{"olp"},
Use: "overlap <prefix> <overlapping>",
Short: "Check if the specified prefix overlaps with the main prefix",
Aliases: []string{"olp"},
Use: "overlap <prefix> <overlapping>",
Short: "Check if the specified prefix overlaps with the main prefix",
SilenceErrors: true,
SilenceUsage: true,
Args: cobra.ExactArgs(2),
SilenceUsage: true,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
_, p := ParseCIDR(args[0])
if p == nil {
Expand Down
40 changes: 27 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@ import (
"github.com/spf13/cobra"
)

var (
Reset = "\033[0m"
Background = "\033[38;5;59m"
CurrentLine = "\033[38;5;60m"
Foreground = "\033[38;5;231m"
Comment = "\033[38;5;103m"
Cyan = "\033[38;5;159m"
Green = "\033[38;5;120m"
Orange = "\033[38;5;222m"
Pink = "\033[38;5;212m"
Purple = "\033[38;5;183m"
Red = "\033[38;5;210m"
Yellow = "\033[38;5;229m"
)

var (
compressIPv6 bool
verbose bool
verbose bool
)

var rootCmd = &cobra.Command{
Version: "0.1.0",
Use: "ipcalc [flags] <prefix> [prefix...]",
Use: "ipcalc [flags] <prefix> [prefix...]",
Long: `ipcalc - IPv6-enabled CIDR calculator

Default action is to show the prefixes details`,
DisableFlagsInUseLine: true,
SilenceErrors: true,
SilenceErrors: true,
//SilenceUsage: true,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -40,22 +55,22 @@ Default action is to show the prefixes details`,
sb.WriteByte('\n')
}

fmt.Fprintf(sb, "Prefix: %s\n", p)
fmt.Fprintf(sb, " Number of addresses: %d\n", p.NumNodes())
fmt.Fprintf(sb, "> %s%s\n\n%s", Purple, p, Reset)
fmt.Fprintf(sb, " Number of addresses: %s%d%s\n", Pink, p.NumNodes(), Reset)
sb.WriteByte('\n')
fmt.Fprintf(sb, " Netmask: %s\n", Explode(p.Mask))
fmt.Fprintf(sb, " Wildcard: %s\n", Explode(p.Hostmask()))
fmt.Fprintf(sb, " Netmask: %s%s%s\n", Yellow, Explode(p.Mask), Reset)
fmt.Fprintf(sb, " Wildcard: %s%s%s\n", Yellow, Explode(p.Hostmask()), Reset)
sb.WriteByte('\n')
fmt.Fprintf(sb, " First: %s\n", Explode(p.IP))
if ! bytes.Equal(ip, p.IP) {
fmt.Fprintf(sb, " First: %s%s%s\n", Green, Explode(p.IP), Reset)
if !bytes.Equal(ip, p.IP) {
fmt.Fprintf(sb, " Input: %s\n", Explode(ip))
}
fmt.Fprintf(sb, " Last: %s\n", Explode(p.Last()))
fmt.Fprintf(sb, " Last: %s%s%s\n", Green, Explode(p.Last()), Reset)

if verbose {
sb.WriteByte('\n')
fmt.Fprintf(sb, " First: %s\n", Bin(p.IP, p.Len()))
if ! bytes.Equal(ip, p.IP) {
if !bytes.Equal(ip, p.IP) {
fmt.Fprintf(sb, " Input: %s\n", Bin(ip, p.Len()))
}
fmt.Fprintf(sb, " Last: %s\n", Bin(p.Last(), p.Len()))
Expand Down Expand Up @@ -121,7 +136,7 @@ func Bin(ip []byte, split int) string {
func ParseCIDR(s string) (net.IP, *ipaddr.Prefix) {
cidrMaskRegex := regexp.MustCompile(`/\d+$`)

if ! cidrMaskRegex.MatchString(s) {
if !cidrMaskRegex.MatchString(s) {
i := strings.Index(s, "/")
if i < 0 {
return nil, nil
Expand Down Expand Up @@ -169,4 +184,3 @@ func Explode(ip []byte) string {

return sb.String()
}

12 changes: 6 additions & 6 deletions cmd/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

var subnetCmd = &cobra.Command{
Aliases: []string{"sub"},
Use: "subnet <prefix> <n>",
Short: "Split the specified prefix into \"n\" sub-networks",
Aliases: []string{"sub"},
Use: "subnet <prefix> <n>",
Short: "Split the specified prefix into \"n\" sub-networks",
SilenceErrors: true,
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
var sub []string

Expand All @@ -29,15 +29,15 @@ var subnetCmd = &cobra.Command{
}

log := math.Log2(float64(n))
if n < 2 || log - float64(int(log)) != 0 {
if n < 2 || log-float64(int(log)) != 0 {
return fmt.Errorf("\"n\" must be a power of 2")
}

for _, sp := range p.Subnets(int(log)) {
sub = append(sub, sp.String())
}

fmt.Printf("Prefixes: %s\n", strings.Join(sub, ", "))
fmt.Printf("> %s%s%s\n", Purple, strings.Join(sub, ", "), Reset)
return nil
},
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

var summarizeCmd = &cobra.Command{
Aliases: []string{"sum"},
Use: "summarize <first address> <last address>",
Short: "Summarize the specified address range",
Aliases: []string{"sum"},
Use: "summarize <first address> <last address>",
Short: "Summarize the specified address range",
SilenceErrors: true,
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
var sum []string

Expand All @@ -36,7 +36,7 @@ var summarizeCmd = &cobra.Command{
return fmt.Errorf("unable to summarize the specified address range")
}

fmt.Printf("Prefixes: %s\n", strings.Join(sum, ", "))
fmt.Printf("> %s%s%s\n", Purple, strings.Join(sum, ", "), Reset)
return nil
},
}
Expand Down
18 changes: 8 additions & 10 deletions cmd/supernet.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package cmd

import (
"fmt"

"fmt"

"github.com/mikioh/ipaddr"
"github.com/spf13/cobra"

"github.com/mikioh/ipaddr"
"github.com/spf13/cobra"
)

var supernetCmd = &cobra.Command{
Aliases: []string{"sup"},
Use: "supernet <prefix> [<prefix>...]",
Short: "Find the shortest common prefix for the specified prefixes",
Aliases: []string{"sup"},
Use: "supernet <prefix> [<prefix>...]",
Short: "Find the shortest common prefix for the specified prefixes",
SilenceErrors: true,
Args: cobra.MinimumNArgs(1),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var ps []ipaddr.Prefix

Expand All @@ -27,7 +25,7 @@ var supernetCmd = &cobra.Command{
ps = append(ps, *p)
}

fmt.Printf("Prefix: %s\n", ipaddr.Supernet(ps))
fmt.Printf("> %s%s%s\n", Purple, ipaddr.Supernet(ps), Reset)
return nil
},
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module github.com/asenci/ipcalc

go 1.16

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mikioh/ipaddr v0.0.0-20180707143206-d9961065b564
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/mikioh/ipaddr v0.0.0-20180707143206-d9961065b564 h1:31EzafDxpf4Yk99gxBYrXueI5j9fS06cDR5kM+W3pig=
github.com/mikioh/ipaddr v0.0.0-20180707143206-d9961065b564/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
Expand Down