diff --git a/LICENSE b/LICENSE index 5dc6826..a83e266 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright (c) 2009,2014 Google Inc. All rights reserved. +Copyright (c) 2021 Pedro Albanese. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are diff --git a/cmd/uuid/main.go b/cmd/uuid/main.go new file mode 100644 index 0000000..6b65834 --- /dev/null +++ b/cmd/uuid/main.go @@ -0,0 +1,54 @@ +// Package main is the main package for the UUID generator application. +package main + +import ( + "bytes" + "flag" + "fmt" + "io" + "os" + "strings" + "time" + + "github.com/google/uuid" +) + +// parse is a command-line flag to parse the given UUID. ('-' for STDIN) +var ( + parse = flag.String("d", "", "Parse given UUID. ('-' for STDIN)") +) + +func main() { + flag.Parse() + var lreader io.Reader + + // If no UUID is given as input, generate a new UUID and print it. + if *parse == "" { + u, _ := uuid.NewUUID() + fmt.Printf("%s\n", u) + } else if *parse != "" { // If a UUID is provided as input, parse and process it. + if *parse == "-" { // If input is "-", read from STDIN. + lreader = os.Stdin + } else { // Otherwise, read from the provided input string. + lreader = strings.NewReader(*parse) + } + + buf := new(bytes.Buffer) + buf.ReadFrom(lreader) + s := buf.String() + s = strings.TrimSuffix(s, "\n") + + // Parse the UUID string to a UUID object. + uuid, _ := uuid.Parse(s) + fmt.Printf("UUID= %s\n", uuid) + + // Get the timestamp information from the parsed UUID. + id := uuid + t := id.Time() + sec, nsec := t.UnixTime() + timeStamp := time.Unix(sec, nsec) + + // Print the timestamp in a formatted date string. + fmt.Printf("DATE= %v \n", timeStamp.Format("2006-01-02 Mon 15:04:05.00000Z -0700")) + } +} diff --git a/go.mod b/go.mod index fc84cd7..ba697e0 100644 --- a/go.mod +++ b/go.mod @@ -1 +1 @@ -module github.com/google/uuid +module github.com/pedroalbanese/uuid