From 013a3cf990043052842a7478171bd88eef56be29 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Wed, 4 Oct 2023 15:19:33 +0500 Subject: [PATCH 1/2] CLI: ipinfo tool prefix Masked / Bits / IsValid --- ipinfo/cmd_tool_prefix.go | 16 ++++++++-- ipinfo/cmd_tool_prefix_bits.go | 49 +++++++++++++++++++++++++++++++ ipinfo/cmd_tool_prefix_isValid.go | 49 +++++++++++++++++++++++++++++++ ipinfo/cmd_tool_prefix_masked.go | 49 +++++++++++++++++++++++++++++++ lib/cmd_tool_prefix_addr.go | 2 ++ lib/cmd_tool_prefix_bits.go | 43 +++++++++++++++++++++++++++ lib/cmd_tool_prefix_masked.go | 43 +++++++++++++++++++++++++++ lib/cmd_tool_prefix_valid.go | 43 +++++++++++++++++++++++++++ 8 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 ipinfo/cmd_tool_prefix_bits.go create mode 100644 ipinfo/cmd_tool_prefix_isValid.go create mode 100644 ipinfo/cmd_tool_prefix_masked.go create mode 100644 lib/cmd_tool_prefix_bits.go create mode 100644 lib/cmd_tool_prefix_masked.go create mode 100644 lib/cmd_tool_prefix_valid.go diff --git a/ipinfo/cmd_tool_prefix.go b/ipinfo/cmd_tool_prefix.go index 675a952d..21d3017f 100644 --- a/ipinfo/cmd_tool_prefix.go +++ b/ipinfo/cmd_tool_prefix.go @@ -11,7 +11,10 @@ import ( var completionsToolPrefix = &complete.Command{ Sub: map[string]*complete.Command{ - "addr": completionsToolPrefixAddr, + "addr": completionsToolPrefixAddr, + "bits": completionsToolPrefixBits, + "masked": completionsToolPrefixMasked, + "is_valid": completionsToolPrefixIsValid, }, Flags: map[string]complete.Predictor{ "-h": predict.Nothing, @@ -24,7 +27,10 @@ func printHelpToolPrefix() { `Usage: %s tool prefix [] [] Commands: - addr returns the base IP address of a prefix. + addr returns the base IP address of a prefix. + bits returns the length of a prefix and reports -1 if invalid. + masked returns canonical form of a prefix, masking off non-high bits, and returns the zero if invalid. + is_valid reports whether a prefix is valid. Options: --help, -h @@ -55,6 +61,12 @@ func cmdToolPrefix() error { switch { case cmd == "addr": err = cmdToolPrefixAddr() + case cmd == "bits": + err = cmdToolPrefixBits() + case cmd == "masked": + err = cmdToolPrefixMasked() + case cmd == "is_valid": + err = cmdToolPrefixIsValid() default: err = toolPrefixHelp() } diff --git a/ipinfo/cmd_tool_prefix_bits.go b/ipinfo/cmd_tool_prefix_bits.go new file mode 100644 index 00000000..9b97b703 --- /dev/null +++ b/ipinfo/cmd_tool_prefix_bits.go @@ -0,0 +1,49 @@ +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 completionsToolPrefixBits = &complete.Command{ + Flags: map[string]complete.Predictor{ + "-h": predict.Nothing, + "--help": predict.Nothing, + }, +} + +func printHelpToolPrefixBits() { + fmt.Printf( + `Usage: %s tool prefix bits + +Description: + Returns the length of a prefix and reports -1 if invalid. + +Examples: + # CIDR Valid Examples. + $ %[1]s tool prefix bits 192.168.0.0/16 + $ %[1]s tool prefix bits 10.0.0.0/8 + $ %[1]s tool prefix bits 2001:0db8:1234::/48 + $ %[1]s tool prefix bits 2606:2800:220:1::/64 + + # CIDR Invalid Examples. + $ %[1]s tool prefix bits 192.168.0.0/40 + $ %[1]s tool prefix bits 2001:0db8:1234::/129 + +Options: + --help, -h + show help. +`, progBase) +} + +func cmdToolPrefixBits() (err error) { + f := lib.CmdToolPrefixBitsFlags{} + f.Init() + pflag.Parse() + + return lib.CmdToolPrefixBits(f, pflag.Args()[3:], printHelpToolPrefixBits) +} diff --git a/ipinfo/cmd_tool_prefix_isValid.go b/ipinfo/cmd_tool_prefix_isValid.go new file mode 100644 index 00000000..4ff19dd8 --- /dev/null +++ b/ipinfo/cmd_tool_prefix_isValid.go @@ -0,0 +1,49 @@ +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 completionsToolPrefixIsValid = &complete.Command{ + Flags: map[string]complete.Predictor{ + "-h": predict.Nothing, + "--help": predict.Nothing, + }, +} + +func printHelpToolPrefixIsValid() { + fmt.Printf( + `Usage: %s tool prefix is_valid + +Description: + Reports whether a prefix is valid. + +Examples: + # CIDR Valid Examples. + $ %[1]s tool prefix is_valid 192.168.0.0/16 + $ %[1]s tool prefix is_valid 10.0.0.0/8 + $ %[1]s tool prefix is_valid 2001:0db8:1234::/48 + $ %[1]s tool prefix is_valid 2606:2800:220:1::/64 + + # CIDR Invalid Examples. + $ %[1]s tool prefix is_valid 192.168.0.0/40 + $ %[1]s tool prefix is_valid 2001:0db8:1234::/129 + +Options: + --help, -h + show help. +`, progBase) +} + +func cmdToolPrefixIsValid() (err error) { + f := lib.CmdToolPrefixIsValidFlags{} + f.Init() + pflag.Parse() + + return lib.CmdToolPrefixIsValid(f, pflag.Args()[3:], printHelpToolPrefixIsValid) +} diff --git a/ipinfo/cmd_tool_prefix_masked.go b/ipinfo/cmd_tool_prefix_masked.go new file mode 100644 index 00000000..2be20e37 --- /dev/null +++ b/ipinfo/cmd_tool_prefix_masked.go @@ -0,0 +1,49 @@ +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 completionsToolPrefixMasked = &complete.Command{ + Flags: map[string]complete.Predictor{ + "-h": predict.Nothing, + "--help": predict.Nothing, + }, +} + +func printHelpToolPrefixMasked() { + fmt.Printf( + `Usage: %s tool prefix masked + +Description: + Returns canonical form of a prefix, masking off non-high bits, and returns the zero if invalid. + +Examples: + # CIDR Valid Examples. + $ %[1]s tool prefix masked 192.168.0.0/16 + $ %[1]s tool prefix masked 10.0.0.0/8 + $ %[1]s tool prefix masked 2001:0db8:1234::/48 + $ %[1]s tool prefix masked 2606:2800:220:1::/64 + + # CIDR Invalid Examples. + $ %[1]s tool prefix masked 192.168.0.0/40 + $ %[1]s tool prefix masked 2001:0db8:1234::/129 + +Options: + --help, -h + show help. +`, progBase) +} + +func cmdToolPrefixMasked() (err error) { + f := lib.CmdToolPrefixMaskedFlags{} + f.Init() + pflag.Parse() + + return lib.CmdToolPrefixMasked(f, pflag.Args()[3:], printHelpToolPrefixMasked) +} diff --git a/lib/cmd_tool_prefix_addr.go b/lib/cmd_tool_prefix_addr.go index 8786961a..f030d6a5 100644 --- a/lib/cmd_tool_prefix_addr.go +++ b/lib/cmd_tool_prefix_addr.go @@ -33,6 +33,8 @@ func CmdToolPrefixAddr(f CmdToolPrefixAddrFlags, args []string, printHelp func() return err } fmt.Printf("%s,%s\n", input, prefix.Addr()) + default: + return ErrInvalidInput } return nil } diff --git a/lib/cmd_tool_prefix_bits.go b/lib/cmd_tool_prefix_bits.go new file mode 100644 index 00000000..ca58a525 --- /dev/null +++ b/lib/cmd_tool_prefix_bits.go @@ -0,0 +1,43 @@ +package lib + +import ( + "fmt" + "net/netip" + + "github.com/spf13/pflag" +) + +type CmdToolPrefixBitsFlags struct { + Help bool +} + +func (f *CmdToolPrefixBitsFlags) Init() { + pflag.BoolVarP( + &f.Help, + "help", "h", false, + "show help.", + ) +} + +func CmdToolPrefixBits(f CmdToolPrefixBitsFlags, args []string, printHelp func()) error { + if f.Help { + printHelp() + return nil + } + + op := func(input string, inputType INPUT_TYPE) error { + switch inputType { + case INPUT_TYPE_CIDR: + prefix, err := netip.ParsePrefix(input) + if err != nil { + return err + } + fmt.Printf("%s,%d\n", input, prefix.Bits()) + default: + return ErrInvalidInput + } + return nil + } + + return GetInputFrom(args, true, true, op) +} diff --git a/lib/cmd_tool_prefix_masked.go b/lib/cmd_tool_prefix_masked.go new file mode 100644 index 00000000..78ca122b --- /dev/null +++ b/lib/cmd_tool_prefix_masked.go @@ -0,0 +1,43 @@ +package lib + +import ( + "fmt" + "net/netip" + + "github.com/spf13/pflag" +) + +type CmdToolPrefixMaskedFlags struct { + Help bool +} + +func (f *CmdToolPrefixMaskedFlags) Init() { + pflag.BoolVarP( + &f.Help, + "help", "h", false, + "show help.", + ) +} + +func CmdToolPrefixMasked(f CmdToolPrefixMaskedFlags, args []string, printHelp func()) error { + if f.Help { + printHelp() + return nil + } + + op := func(input string, inputType INPUT_TYPE) error { + switch inputType { + case INPUT_TYPE_CIDR: + prefix, err := netip.ParsePrefix(input) + if err != nil { + return err + } + fmt.Printf("%s,%s\n", input, prefix.Masked()) + default: + return ErrInvalidInput + } + return nil + } + + return GetInputFrom(args, true, true, op) +} diff --git a/lib/cmd_tool_prefix_valid.go b/lib/cmd_tool_prefix_valid.go new file mode 100644 index 00000000..15b63405 --- /dev/null +++ b/lib/cmd_tool_prefix_valid.go @@ -0,0 +1,43 @@ +package lib + +import ( + "fmt" + "net/netip" + + "github.com/spf13/pflag" +) + +type CmdToolPrefixIsValidFlags struct { + Help bool +} + +func (f *CmdToolPrefixIsValidFlags) Init() { + pflag.BoolVarP( + &f.Help, + "help", "h", false, + "show help.", + ) +} + +func CmdToolPrefixIsValid(f CmdToolPrefixIsValidFlags, args []string, printHelp func()) error { + if f.Help { + printHelp() + return nil + } + + op := func(input string, inputType INPUT_TYPE) error { + switch inputType { + case INPUT_TYPE_CIDR: + prefix, err := netip.ParsePrefix(input) + if err != nil { + return err + } + fmt.Printf("%s,%t\n", input, prefix.IsValid()) + default: + return ErrInvalidInput + } + return nil + } + + return GetInputFrom(args, true, true, op) +} From 6a4160f7c704d8b00c53e42714ca9e9bcd431641 Mon Sep 17 00:00:00 2001 From: Ahmad Date: Wed, 4 Oct 2023 15:41:51 +0500 Subject: [PATCH 2/2] Remove Default case --- lib/cmd_tool_prefix_addr.go | 2 -- lib/cmd_tool_prefix_bits.go | 2 -- lib/cmd_tool_prefix_masked.go | 2 -- lib/cmd_tool_prefix_valid.go | 2 -- 4 files changed, 8 deletions(-) diff --git a/lib/cmd_tool_prefix_addr.go b/lib/cmd_tool_prefix_addr.go index f030d6a5..8786961a 100644 --- a/lib/cmd_tool_prefix_addr.go +++ b/lib/cmd_tool_prefix_addr.go @@ -33,8 +33,6 @@ func CmdToolPrefixAddr(f CmdToolPrefixAddrFlags, args []string, printHelp func() return err } fmt.Printf("%s,%s\n", input, prefix.Addr()) - default: - return ErrInvalidInput } return nil } diff --git a/lib/cmd_tool_prefix_bits.go b/lib/cmd_tool_prefix_bits.go index ca58a525..01da4a54 100644 --- a/lib/cmd_tool_prefix_bits.go +++ b/lib/cmd_tool_prefix_bits.go @@ -33,8 +33,6 @@ func CmdToolPrefixBits(f CmdToolPrefixBitsFlags, args []string, printHelp func() return err } fmt.Printf("%s,%d\n", input, prefix.Bits()) - default: - return ErrInvalidInput } return nil } diff --git a/lib/cmd_tool_prefix_masked.go b/lib/cmd_tool_prefix_masked.go index 78ca122b..090049b2 100644 --- a/lib/cmd_tool_prefix_masked.go +++ b/lib/cmd_tool_prefix_masked.go @@ -33,8 +33,6 @@ func CmdToolPrefixMasked(f CmdToolPrefixMaskedFlags, args []string, printHelp fu return err } fmt.Printf("%s,%s\n", input, prefix.Masked()) - default: - return ErrInvalidInput } return nil } diff --git a/lib/cmd_tool_prefix_valid.go b/lib/cmd_tool_prefix_valid.go index 15b63405..0a1b67e7 100644 --- a/lib/cmd_tool_prefix_valid.go +++ b/lib/cmd_tool_prefix_valid.go @@ -33,8 +33,6 @@ func CmdToolPrefixIsValid(f CmdToolPrefixIsValidFlags, args []string, printHelp return err } fmt.Printf("%s,%t\n", input, prefix.IsValid()) - default: - return ErrInvalidInput } return nil }