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

CMD: Time-based UUID Generator & Parser #85

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
49 changes: 49 additions & 0 deletions cmd/uuid/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
"os"
"strings"
"time"

"github.com/google/uuid"
)

var (
parse = flag.String("d", "", "Parse given UUID.")
random = flag.Bool("r", false, "Generate Random UUID.")
)

func main() {
flag.Parse()
var lreader io.Reader

if *parse == "" && *random == false {
u, _ := uuid.NewUUID()
fmt.Printf("%s\n", u)
} else if *parse == "" && *random == true {
u := uuid.New()
fmt.Printf("%s\n", u)
} else if *parse != "" {
if *parse == "-" {
lreader = os.Stdin
} else if *parse != "-" {
lreader = strings.NewReader(*parse)
}
buf := new(bytes.Buffer)
buf.ReadFrom(lreader)
s := buf.String()

uuid, _ := uuid.Parse(s)
fmt.Printf("uuid : %s\n", uuid)

id := uuid
t := id.Time()
sec, nsec := t.UnixTime()
timeStamp := time.Unix(sec, nsec)
fmt.Printf("date : %v \n", timeStamp.Format("2006-01-02 Mon 15:04:05.000Z -0700"))
}
}