Skip to content

Commit

Permalink
upd: #3 - Implement UUID generation
Browse files Browse the repository at this point in the history
  • Loading branch information
jtprogru committed Mar 28, 2023
1 parent 8eb2a29 commit f225cc7
Showing 4 changed files with 65 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/passwd.go
Original file line number Diff line number Diff line change
@@ -49,9 +49,9 @@ to quickly create a Cobra application.`,
func init() {
rootCmd.AddCommand(passwdCmd)
r = *rand.New(rand.NewSource(time.Now().UnixMicro()))
passwdCmd.Flags().IntVarP(&passwdLength, "length", "l", 24, "Length of password (default=24)")
passwdCmd.Flags().BoolVar(&passwdDigits, "digits", true, "Present digits in password (default=true)")
passwdCmd.Flags().BoolVar(&passwdSpecials, "specials", true, "Present special symbols in password (default=true)")
passwdCmd.Flags().IntVarP(&passwdLength, "length", "l", 24, "Length of password")
passwdCmd.Flags().BoolVar(&passwdDigits, "digits", true, "Present digits in password")
passwdCmd.Flags().BoolVar(&passwdSpecials, "specials", true, "Present special symbols in password")
}

func getPasswd(passwdLen int, digitFlag bool, specFlag bool) string {
59 changes: 59 additions & 0 deletions cmd/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/google/uuid"
"github.com/spf13/cobra"
)

// uuidCmd represents the uuid command
var (
uuidCmd = &cobra.Command{
Use: "uuid",
Short: "Generate UUID string",
Long: `A UUID is a 16 byte (128 bit) array.
UUIDs may be used as keys to maps or compared directly.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getUUID())
},
}
v4UUID bool
v5UUID bool
nilUUID bool
namespace = uuid.NameSpaceURL
url = []byte("https://jtprog.ru")
)

func init() {
rootCmd.AddCommand(uuidCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// uuidCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
uuidCmd.Flags().BoolVarP(&v4UUID, "v4", "4", true, "Generate UUID4")
uuidCmd.Flags().BoolVarP(&v5UUID, "v5", "5", false, "Generate UUID5")
uuidCmd.Flags().BoolVarP(&nilUUID, "v0", "0", false, "Generate UUIDnil")

}

func getUUID() string {
switch {
case v4UUID:
return uuid.New().String()
case v5UUID:
return uuid.NewMD5(namespace, url).String()
case nilUUID:
return "00000000-0000-0000-0000-000000000000"
default:
return "00000000-0000-0000-0000-000000000000"
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ module github.com/jtprogru/gch
go 1.20

require (
github.com/google/uuid v1.3.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -114,6 +114,8 @@ github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=

0 comments on commit f225cc7

Please sign in to comment.