Skip to content

Commit 4ea321f

Browse files
committed
Add basic configuration options for uuid command
1 parent bb79cd3 commit 4ea321f

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

cmd/server/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
"errors"
2525
"fmt"
2626
"github.com/chclaus/dt/cmd"
27+
"github.com/chclaus/dt/config"
2728
"github.com/spf13/cobra"
29+
"github.com/spf13/viper"
2830
"net/http"
29-
"path/filepath"
3031
"os"
31-
"github.com/spf13/viper"
32-
"github.com/chclaus/dt/config"
32+
"path/filepath"
3333
)
3434

3535
// serverCmd represents the server command

cmd/uuid/v3.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ package uuid
2222

2323
import (
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

6259
func 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
}

cmd/uuid/v5.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ package uuid
2222

2323
import (
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
// uuidV5Cmd represents the uuidV5 command
@@ -33,20 +35,15 @@ var uuidV5Cmd = &cobra.Command{
3335
Short: "Generates a UUID Version 5 (namespace(UUID), value)",
3436
Long: "Generates a v5 UUID, based on SHA1 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) {
@@ -64,4 +61,7 @@ func init() {
6461

6562
uuidV5Cmd.Flags().StringP("namespace", "n", "", "The namespace that should be hashed. It should be a domain or application specific UUID")
6663
uuidV5Cmd.Flags().StringP("value", "v", "", "The value that should be hashed")
64+
uuidV5Cmd.MarkFlagRequired("value")
65+
66+
viper.BindPFlag("uuid.namespace", uuidV5Cmd.Flags().Lookup("namespace"))
6767
}

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package config
22

33
import (
4-
"github.com/spf13/viper"
5-
"github.com/mitchellh/go-homedir"
64
"fmt"
5+
"github.com/mitchellh/go-homedir"
6+
"github.com/spf13/viper"
77
"os"
88
)
99

0 commit comments

Comments
 (0)