Skip to content

Commit

Permalink
feat: Add uuid CLI
Browse files Browse the repository at this point in the history
```bash
go install github.com/cmackenzie1/go-uuid/cmd/uuid

uuid
5f70a1c2-de1b-4e23-b8f8-d86889ac88a1
```
  • Loading branch information
cmackenzie1 committed Nov 15, 2024
1 parent b162e2a commit debf20d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
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.

0 comments on commit debf20d

Please sign in to comment.