From debf20d7afeb69b592f45f2e7d7d8d65ee1b6059 Mon Sep 17 00:00:00 2001 From: Cole MacKenzie Date: Thu, 14 Nov 2024 16:45:54 -0800 Subject: [PATCH] feat: Add `uuid` CLI ```bash go install github.com/cmackenzie1/go-uuid/cmd/uuid uuid 5f70a1c2-de1b-4e23-b8f8-d86889ac88a1 ``` --- README.md | 12 +++++++++++ cmd/uuid/main.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ go.sum | 0 3 files changed, 65 insertions(+) create mode 100644 cmd/uuid/main.go create mode 100644 go.sum diff --git a/README.md b/README.md index efbf5a3..207c717 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,18 @@ A simple, stdlib only, go module for generating UUIDs (**U**niversally **U**niqu ## Installation +### CLI + +```bash +go install github.com/cmackenzie1/go-uuid/cmd/uuid + +# run the command +uuid +5f70a1c2-de1b-4e23-b8f8-d86889ac88a1 +``` + +### Package + ```bash go get github.com/cmackenzie1/go-uuid ``` diff --git a/cmd/uuid/main.go b/cmd/uuid/main.go new file mode 100644 index 0000000..ae9cd95 --- /dev/null +++ b/cmd/uuid/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "flag" + "fmt" + "os" + "strings" + + "github.com/cmackenzie1/go-uuid" +) + +func run(version int, count int, uppercase bool) error { + for i := 0; i < count; i++ { + var id uuid.UUID + var err error + if version == 4 { + id, err = uuid.NewV4() + } else { + id, err = uuid.NewV7() + } + if err != nil { + return err + } + if uppercase { + fmt.Println(strings.ToUpper(id.String())) + } else { + fmt.Println(id) + } + } + return nil +} + +func main() { + v := flag.Int("v", 4, "UUID version to generate.Supported versions are 4 and 7.") + n := flag.Int("c", 1, "Number of UUIDs to generate.") + u := flag.Bool("u", false, "Print UUIDs in uppercase.") + flag.Parse() + + if *v != 4 && *v != 7 { + fmt.Printf("Unsupported UUID version: %d\n", *v) + os.Exit(1) + } + + if *n < 1 { + fmt.Printf("Number of UUIDs to generate must be greater than 0.\n") + os.Exit(1) + } + + if err := run(*v, *n, *u); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29