@@ -22,9 +22,11 @@ package uuid
2222
2323import (
2424 "fmt"
25+ "github.com/chclaus/dt/config"
2526 "github.com/pkg/errors"
2627 "github.com/satori/go.uuid"
2728 "github.com/spf13/cobra"
29+ "github.com/spf13/viper"
2830)
2931
3032// uuidV3Cmd represents the uuidV3 command
@@ -33,24 +35,19 @@ var uuidV3Cmd = &cobra.Command{
3335 Short : "Generates a UUID Version 3 (namespace(UUID), value)" ,
3436 Long : "Generates a v3 UUID, based on MD5 hashing of (namespace(UUID), value) (RFC 4122)" ,
3537 PreRunE : func (cmd * cobra.Command , args []string ) error {
36- ns := cmd . Flag ( "namespace" ). Value . String ()
37- errMsg := "You have to specify a namespace and value. The namespace MUST be a valid UUID"
38+ ns := config . Cfg . UUID . Namespace
39+ errMsg := "Error: You have to specify a namespace and value. The namespace MUST be a valid UUID"
3840 if ns == "" {
3941 return errors .New (errMsg )
4042 }
4143 if _ , err := uuid .FromString (ns ); err != nil {
4244 return errors .New (errMsg )
4345 }
4446
45- val := cmd .Flag ("value" ).Value .String ()
46- if val == "" {
47- return errors .New (errMsg )
48- }
49-
5047 return nil
5148 },
5249 Run : func (cmd * cobra.Command , args []string ) {
53- ns := cmd . Flag ( "namespace" ). Value . String ()
50+ ns := config . Cfg . UUID . Namespace
5451 nsUUID , _ := uuid .FromString (ns )
5552 val := cmd .Flag ("value" ).Value .String ()
5653
@@ -60,8 +57,13 @@ var uuidV3Cmd = &cobra.Command{
6057}
6158
6259func init () {
60+ cobra .OnInitialize (config .InitConfig )
61+
6362 uuidCmd .AddCommand (uuidV3Cmd )
6463
6564 uuidV3Cmd .Flags ().StringP ("namespace" , "n" , "" , "The namespace that should be hashed. It should be a domain or application specific UUID" )
6665 uuidV3Cmd .Flags ().StringP ("value" , "v" , "" , "The value that should be hashed" )
66+ uuidV3Cmd .MarkFlagRequired ("value" )
67+
68+ viper .BindPFlag ("uuid.namespace" , uuidV3Cmd .Flags ().Lookup ("namespace" ))
6769}
0 commit comments