Skip to content

Commit bccb174

Browse files
committed
feat: Add uuid CLI
```bash go install github.com/cmackenzie1/go-uuid/cmd/uuid uuid 5f70a1c2-de1b-4e23-b8f8-d86889ac88a1 ```
1 parent b162e2a commit bccb174

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ A simple, stdlib only, go module for generating UUIDs (**U**niversally **U**niqu
77

88
## Installation
99

10+
### CLI
11+
12+
```bash
13+
go install github.com/cmackenzie1/go-uuid/cmd/uuid
14+
15+
# run the command
16+
uuid
17+
5f70a1c2-de1b-4e23-b8f8-d86889ac88a1
18+
```
19+
20+
### Package
21+
1022
```bash
1123
go get github.com/cmackenzie1/go-uuid
1224
```

cmd/uuid/main.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"strings"
8+
9+
"github.com/cmackenzie1/go-uuid"
10+
)
11+
12+
func run(version int, count int, uppercase bool) error {
13+
for i := 0; i < count; i++ {
14+
var id uuid.UUID
15+
var err error
16+
if version == 4 {
17+
id, err = uuid.NewV4()
18+
} else {
19+
id, err = uuid.NewV7()
20+
}
21+
if err != nil {
22+
return err
23+
}
24+
if uppercase {
25+
fmt.Println(strings.ToUpper(id.String()))
26+
} else {
27+
fmt.Println(id)
28+
}
29+
}
30+
return nil
31+
}
32+
33+
func main() {
34+
v := flag.Int("v", 4, "UUID version to generate.Supported versions are 4 and 7.")
35+
n := flag.Int("c", 1, "Number of UUIDs to generate.")
36+
u := flag.Bool("u", false, "Print UUIDs in uppercase.")
37+
flag.Parse()
38+
39+
if *v != 4 && *v != 7 {
40+
fmt.Printf("Unsupported UUID version: %d\n", *v)
41+
os.Exit(1)
42+
}
43+
44+
if *n < 1 {
45+
fmt.Printf("Number of UUIDs to generate must be greater than 0.\n")
46+
os.Exit(1)
47+
}
48+
49+
if err := run(*v, *n, *u); err != nil {
50+
fmt.Println(err)
51+
os.Exit(1)
52+
}
53+
}

go.sum

Whitespace-only changes.

0 commit comments

Comments
 (0)