diff --git a/cmd/passwd.go b/cmd/passwd.go index 9e51804..dbffc66 100644 --- a/cmd/passwd.go +++ b/cmd/passwd.go @@ -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 { diff --git a/cmd/uuid.go b/cmd/uuid.go new file mode 100644 index 0000000..b7df119 --- /dev/null +++ b/cmd/uuid.go @@ -0,0 +1,59 @@ +/* +Copyright © 2023 NAME HERE +*/ +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" + } +} diff --git a/go.mod b/go.mod index 8a63507..a0150bb 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index c2ce218..d117ef1 100644 --- a/go.sum +++ b/go.sum @@ -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=