Skip to content

Commit

Permalink
update to use opts struct to pass name
Browse files Browse the repository at this point in the history
  • Loading branch information
hooksie1 committed Aug 22, 2024
1 parent bf543c9 commit e7df90d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func init() {
}

func database(cmd *cobra.Command, args []string) error {
nc, err := newNatsConnection("piggy-client")
opts := natsOpts{
name: "piggy-client",
prefix: viper.GetString("inbox_prefix"),
}
nc, err := newNatsConnection(opts)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions cmd/nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ import (
"github.com/spf13/viper"
)

func newNatsConnection(name string) (*nats.Conn, error) {
opts := []nats.Option{nats.Name(name), nats.CustomInboxPrefix(viper.GetString("inbox_prefix"))}
type natsOpts struct {
name string
prefix string
}

func newNatsConnection(connOpts natsOpts) (*nats.Conn, error) {
opts := []nats.Option{nats.Name(connOpts.name)}

if connOpts.prefix != "" {
opts = append(opts, nats.CustomInboxPrefix(connOpts.prefix))
}

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

Expand Down
6 changes: 5 additions & 1 deletion cmd/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func getSubject(verb string, id string) string {
}

func secrets(cmd *cobra.Command, args []string) error {
nc, err := newNatsConnection("piggy-client")
opts := natsOpts{
name: "piggy-client",
prefix: viper.GetString("inbox_prefix"),
}
nc, err := newNatsConnection(opts)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func start(cmd *cobra.Command, args []string) error {
Description: "Secrets storage for NATS",
}

nc, err := newNatsConnection("piggybank-server")
opts := natsOpts{
name: "piggybank-server",
}

nc, err := newNatsConnection(opts)
if err != nil {
return err
}
Expand Down

0 comments on commit e7df90d

Please sign in to comment.