Skip to content

Commit 6a1fc81

Browse files
authored
update to use opts struct to pass name (#16)
1 parent bf543c9 commit 6a1fc81

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

cmd/database.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ func init() {
2424
}
2525

2626
func database(cmd *cobra.Command, args []string) error {
27-
nc, err := newNatsConnection("piggy-client")
27+
opts := natsOpts{
28+
name: "piggy-client",
29+
prefix: viper.GetString("inbox_prefix"),
30+
}
31+
nc, err := newNatsConnection(opts)
2832
if err != nil {
2933
return err
3034
}

cmd/nats.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@ import (
99
"github.com/spf13/viper"
1010
)
1111

12-
func newNatsConnection(name string) (*nats.Conn, error) {
13-
opts := []nats.Option{nats.Name(name), nats.CustomInboxPrefix(viper.GetString("inbox_prefix"))}
12+
type natsOpts struct {
13+
name string
14+
prefix string
15+
}
16+
17+
func newNatsConnection(connOpts natsOpts) (*nats.Conn, error) {
18+
opts := []nats.Option{nats.Name(connOpts.name)}
19+
20+
if connOpts.prefix != "" {
21+
opts = append(opts, nats.CustomInboxPrefix(connOpts.prefix))
22+
}
1423

1524
_, ok := os.LookupEnv("USER")
1625

cmd/secrets.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func getSubject(verb string, id string) string {
3232
}
3333

3434
func secrets(cmd *cobra.Command, args []string) error {
35-
nc, err := newNatsConnection("piggy-client")
35+
opts := natsOpts{
36+
name: "piggy-client",
37+
prefix: viper.GetString("inbox_prefix"),
38+
}
39+
nc, err := newNatsConnection(opts)
3640
if err != nil {
3741
return err
3842
}

cmd/start.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func start(cmd *cobra.Command, args []string) error {
3232
Description: "Secrets storage for NATS",
3333
}
3434

35-
nc, err := newNatsConnection("piggybank-server")
35+
opts := natsOpts{
36+
name: "piggybank-server",
37+
}
38+
39+
nc, err := newNatsConnection(opts)
3640
if err != nil {
3741
return err
3842
}

0 commit comments

Comments
 (0)