From c16d90fe939dfcb4d4326dd3c803ee8920da6ab6 Mon Sep 17 00:00:00 2001 From: Norbinsh Date: Sun, 4 Oct 2020 22:08:13 +0300 Subject: [PATCH 1/6] introduce the new check command --- cmd/root.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/root.go b/cmd/root.go index 1ba8c93..8e8c15f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -52,6 +52,7 @@ func init() { rootCmd.AddCommand(v4Cmd) rootCmd.AddCommand(empty) + rootCmd.AddCommand(check) cobra.OnInitialize(initConfig) From 740c8a9f1d61d6892656016204143c0188e097ed Mon Sep 17 00:00:00 2001 From: Norbinsh Date: Sun, 4 Oct 2020 22:09:15 +0300 Subject: [PATCH 2/6] support the new check command --- cmd/check.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cmd/check.go diff --git a/cmd/check.go b/cmd/check.go new file mode 100644 index 0000000..22ce7cd --- /dev/null +++ b/cmd/check.go @@ -0,0 +1,26 @@ +package cmd + +import ( + "fmt" + "regexp" + + "github.com/spf13/cobra" +) + +// check represents a command validating whether the provided input is a valid UUID +var check = &cobra.Command{ + Use: "check", + Short: "Checks whether the provided input is a valid UUID", + Long: `Checks whether the provided input is a valid UUID, +while using regular expression pattern matching.`, + Args: cobra.MinimumNArgs(1), + Run: func(cmd *cobra.Command, args []string) { + uuidPattern := "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" + re := regexp.MustCompile(uuidPattern) + if re.MatchString(args[0]) { + fmt.Println("valid") + return + } + fmt.Println("invalid") + }, +} From 8c60088517e74647163c8b22d45ae637a0257238 Mon Sep 17 00:00:00 2001 From: Norbinsh Date: Mon, 5 Oct 2020 08:33:13 +0300 Subject: [PATCH 3/6] move logic to a separate function, and support checking multiple arguments --- cmd/check.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd/check.go b/cmd/check.go index 22ce7cd..e593727 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -10,17 +10,26 @@ import ( // check represents a command validating whether the provided input is a valid UUID var check = &cobra.Command{ Use: "check", - Short: "Checks whether the provided input is a valid UUID", - Long: `Checks whether the provided input is a valid UUID, + Short: "Checks whether each of the provided arguments is a valid UUID", + Long: `Checks whether each of the provided arguments is a valid UUID, while using regular expression pattern matching.`, Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - uuidPattern := "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" - re := regexp.MustCompile(uuidPattern) - if re.MatchString(args[0]) { - fmt.Println("valid") - return + for _, arg := range args { + if Check(arg) { + fmt.Printf("%s is a valid UUID\n", arg) + } else { + fmt.Printf("%s is not a valid UUID\n", arg) + } } - fmt.Println("invalid") }, } + +func Check(uuid string) bool { + uuidPattern := "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" + re := regexp.MustCompile(uuidPattern) + if re.MatchString(uuid) { + return true + } + return false +} From 5d92bf38841c7f6731a929f30ddc25dfde0c5039 Mon Sep 17 00:00:00 2001 From: Norbinsh Date: Mon, 5 Oct 2020 08:33:25 +0300 Subject: [PATCH 4/6] add simple test case --- cmd/check_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 cmd/check_test.go diff --git a/cmd/check_test.go b/cmd/check_test.go new file mode 100644 index 0000000..c6ce393 --- /dev/null +++ b/cmd/check_test.go @@ -0,0 +1,25 @@ +package cmd + +import "testing" + +func TestCheck(t *testing.T) { + + t.Run("should return true", func(t *testing.T) { + input := "c923d13a-cee4-4182-af57-438f8b032fb6" // valid UUID + got := Check(input) + expect := true + if got != expect { + t.Errorf("expected %s to be %t but got %t", input, expect, got) + } + }) + + t.Run("collection of any size", func(t *testing.T) { + input := "c93a-cee4-4182-af57-438f2fb6" // not a valid UUID + got := Check(input) + expect := false + if got != expect { + t.Errorf("expected %s to be %t but got %t", input, expect, got) + } + }) + +} From 97f15bfce3ec0ae1e85357874c7e689a4c3cb68d Mon Sep 17 00:00:00 2001 From: Norbinsh Date: Mon, 5 Oct 2020 08:36:53 +0300 Subject: [PATCH 5/6] update docs --- README.md | 1 + cmd/check.go | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 30cb601..d232072 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,5 @@ Run the following command ### Commands: - uuid v4 + - uuid check diff --git a/cmd/check.go b/cmd/check.go index e593727..6b1908e 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -12,7 +12,11 @@ var check = &cobra.Command{ Use: "check", Short: "Checks whether each of the provided arguments is a valid UUID", Long: `Checks whether each of the provided arguments is a valid UUID, -while using regular expression pattern matching.`, +while using regular expression pattern matching. +you can pass one or more UUIDs to be checked: + +example usage: +uuid check d50cddd9-71f3-47f0-a407-1bf7581874fc`, Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { for _, arg := range args { From 183ad62d9487530d3f3209bac82e89a071369beb Mon Sep 17 00:00:00 2001 From: Norbinsh Date: Mon, 5 Oct 2020 08:45:18 +0300 Subject: [PATCH 6/6] correct test name --- cmd/check_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/check_test.go b/cmd/check_test.go index c6ce393..dd6b3fb 100644 --- a/cmd/check_test.go +++ b/cmd/check_test.go @@ -13,7 +13,7 @@ func TestCheck(t *testing.T) { } }) - t.Run("collection of any size", func(t *testing.T) { + t.Run("should return false", func(t *testing.T) { input := "c93a-cee4-4182-af57-438f2fb6" // not a valid UUID got := Check(input) expect := false