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

feat: Add uuid CLI #11

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
53 changes: 53 additions & 0 deletions cmd/uuid/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Empty file added go.sum
Empty file.
Loading