diff --git a/cmd/aggregate.go b/cmd/aggregate.go index 95b2d7b..3f6f235 100644 --- a/cmd/aggregate.go +++ b/cmd/aggregate.go @@ -9,11 +9,11 @@ import ( ) var aggregateCmd = &cobra.Command{ - Aliases: []string{"agg"}, - Use: "aggregate [...]", - Short: "Aggregate the specified prefixes", + Aliases: []string{"agg"}, + Use: "aggregate [...]", + 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 @@ -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 }, } diff --git a/cmd/exclude.go b/cmd/exclude.go index f88b47f..32f0bef 100644 --- a/cmd/exclude.go +++ b/cmd/exclude.go @@ -8,11 +8,11 @@ import ( ) var excludeCmd = &cobra.Command{ - Aliases: []string{"exc"}, - Use: "exclude ", - Short: "Exclude the specified prefix from the main prefix", + Aliases: []string{"exc"}, + Use: "exclude ", + 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 @@ -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 }, } diff --git a/cmd/overlap.go b/cmd/overlap.go index 7845e61..0b6abd8 100644 --- a/cmd/overlap.go +++ b/cmd/overlap.go @@ -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 ", - Short: "Check if the specified prefix overlaps with the main prefix", + Aliases: []string{"olp"}, + Use: "overlap ", + 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 { diff --git a/cmd/root.go b/cmd/root.go index e866963..90e5383 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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...]", + Use: "ipcalc [flags] [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 { @@ -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())) @@ -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 @@ -169,4 +184,3 @@ func Explode(ip []byte) string { return sb.String() } - diff --git a/cmd/subnet.go b/cmd/subnet.go index d8b0801..02c6fe1 100644 --- a/cmd/subnet.go +++ b/cmd/subnet.go @@ -10,11 +10,11 @@ import ( ) var subnetCmd = &cobra.Command{ - Aliases: []string{"sub"}, - Use: "subnet ", - Short: "Split the specified prefix into \"n\" sub-networks", + Aliases: []string{"sub"}, + Use: "subnet ", + 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 @@ -29,7 +29,7 @@ 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") } @@ -37,7 +37,7 @@ var subnetCmd = &cobra.Command{ 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 }, } diff --git a/cmd/summarize.go b/cmd/summarize.go index ce046d8..cb62ac1 100644 --- a/cmd/summarize.go +++ b/cmd/summarize.go @@ -10,11 +10,11 @@ import ( ) var summarizeCmd = &cobra.Command{ - Aliases: []string{"sum"}, - Use: "summarize ", - Short: "Summarize the specified address range", + Aliases: []string{"sum"}, + Use: "summarize ", + 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 @@ -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 }, } diff --git a/cmd/supernet.go b/cmd/supernet.go index 3133ef2..4c1d5c4 100644 --- a/cmd/supernet.go +++ b/cmd/supernet.go @@ -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 [...]", - Short: "Find the shortest common prefix for the specified prefixes", + Aliases: []string{"sup"}, + Use: "supernet [...]", + 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 @@ -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 }, } diff --git a/go.mod b/go.mod index ffc6d62..9998afa 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b291549..f09b55a 100644 --- a/go.sum +++ b/go.sum @@ -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=