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 new file mode 100644 index 0000000..6b1908e --- /dev/null +++ b/cmd/check.go @@ -0,0 +1,39 @@ +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 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. +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 { + if Check(arg) { + fmt.Printf("%s is a valid UUID\n", arg) + } else { + fmt.Printf("%s is not a valid UUID\n", arg) + } + } + }, +} + +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 +} diff --git a/cmd/check_test.go b/cmd/check_test.go new file mode 100644 index 0000000..dd6b3fb --- /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("should return false", 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) + } + }) + +} 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)