Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check command #11

Closed
wants to merge 2 commits into from
Closed
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
26 changes: 26 additions & 0 deletions cmd/check.go
Original file line number Diff line number Diff line change
@@ -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}$"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to a function so that writing unit test is possible?

Also, how about supporting multiple UUIDs in the argument? And giving responses to each as true false? I leave that up to you.

re := regexp.MustCompile(uuidPattern)
if re.MatchString(args[0]) {
fmt.Println("valid")
return
}
fmt.Println("invalid")
},
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func init() {

rootCmd.AddCommand(v4Cmd)
rootCmd.AddCommand(empty)
rootCmd.AddCommand(check)

cobra.OnInitialize(initConfig)

Expand Down