diff --git a/cmd/subscribe.go b/cmd/subscribe.go index 1034212..af533ed 100644 --- a/cmd/subscribe.go +++ b/cmd/subscribe.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/skvoch/nats-cli/pkg/constants" "time" "github.com/sirupsen/logrus" @@ -14,6 +15,7 @@ var subscribeVars struct { natsServer string natsSubject string natsClusterID string + natsClientID string templateName string startDeltaFrom time.Duration } @@ -33,6 +35,7 @@ var subscribeTemplateCmd = &cobra.Command{ if err := subscribe.Run( tpl.NatsServer, tpl.NatsClusterID, + tpl.NatsClientID, tpl.NatsSubject, subscribeVars.startDeltaFrom); err != nil { logrus.Error(err) @@ -52,6 +55,7 @@ var subscribeCmd = &cobra.Command{ if err := subscribe.Run( subscribeVars.natsServer, subscribeVars.natsClusterID, + subscribeVars.natsClientID, subscribeVars.natsSubject, subscribeVars.startDeltaFrom); err != nil { logrus.Error(err) @@ -72,6 +76,7 @@ func init() { subscribeCmd.Flags().StringVarP(&subscribeVars.natsServer, "addr", "a", "", "NATS server addr") subscribeCmd.Flags().StringVarP(&subscribeVars.natsSubject, "subject", "s", "", "subject name") subscribeCmd.Flags().StringVarP(&subscribeVars.natsClusterID, "cluster-id", "c", "", "cluster id") + subscribeCmd.Flags().StringVarP(&subscribeVars.natsClientID, "client-id", "i", constants.ClientID, "client id") subscribeCmd.Flags().DurationVarP(&subscribeVars.startDeltaFrom, "delta-time", "d", 0, "delta-time") if err := subscribeCmd.MarkFlagRequired("addr"); err != nil { diff --git a/cmd/template.go b/cmd/template.go index 832be63..eaa8af1 100644 --- a/cmd/template.go +++ b/cmd/template.go @@ -2,9 +2,9 @@ package cmd import ( "fmt" - "github.com/sirupsen/logrus" "github.com/skvoch/nats-cli/internal/template" + "github.com/skvoch/nats-cli/pkg/constants" "github.com/spf13/cobra" ) @@ -12,6 +12,7 @@ type TemplateVars struct { natsServer string natsSubject string natsClusterID string + natsClientID string name string } @@ -57,6 +58,7 @@ var templateCreate = &cobra.Command{ if err := template.Add(templateVars.name, template.Item{ NatsClusterID: templateVars.natsClusterID, NatsServer: templateVars.natsServer, + NatsClientID: templateVars.natsClientID, NatsSubject: templateVars.natsSubject, }); err != nil { logrus.Error(err) @@ -80,8 +82,8 @@ var templateList = &cobra.Command{ templates := template.List() for name, item := range templates.Values { - str := fmt.Sprintf(`name: %s | server: %s | cluster-id: %s | subject: %s`, - name, item.NatsServer, item.NatsClusterID, item.NatsSubject) + str := fmt.Sprintf(`name: %s | server: %s | cluster-id: %s | client-id: %s | subject: %s`, + name, item.NatsServer, item.NatsClusterID, item.NatsClientID, item.NatsSubject) logrus.Info(str) } @@ -99,6 +101,7 @@ func init() { templateCreate.Flags().StringVarP(&templateVars.natsServer, "addr", "a", "", "NATS server addr") templateCreate.Flags().StringVarP(&templateVars.natsSubject, "subject", "s", "", "subject name") templateCreate.Flags().StringVarP(&templateVars.natsClusterID, "cluster-id", "c", "", "cluster id") + templateCreate.Flags().StringVarP(&templateVars.natsClientID, "client-id", "i", constants.ClientID, "client id") if err := templateCreate.MarkFlagRequired("addr"); err != nil { logrus.Fatal(err) diff --git a/internal/subscribe/subscribe.go b/internal/subscribe/subscribe.go index 5466891..cfb8a0e 100644 --- a/internal/subscribe/subscribe.go +++ b/internal/subscribe/subscribe.go @@ -19,9 +19,13 @@ const ( logNatsSubject = "subject" ) -func Run(address, clusterId, subject string, delta time.Duration) error { +func Run(address, clusterId, clientId, subject string, delta time.Duration) error { + if clientId == "" { + clientId = constants.ClientID + } + logrus.WithField(logNatsAddr, address).Info("trying connect to nats...") - nats, err := natsc.Connect(address, clusterId, constants.ClientID) + nats, err := natsc.Connect(address, clusterId, clientId) if err != nil { return err } @@ -32,7 +36,7 @@ func Run(address, clusterId, subject string, delta time.Duration) error { } logrus.WithField(logNatsSubject, subject).Info("successful subscription to subject!") - c := make(chan os.Signal) + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) for { diff --git a/internal/template/template.go b/internal/template/template.go index e05b301..67db772 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -14,6 +14,7 @@ type Item struct { NatsServer string NatsSubject string NatsClusterID string + NatsClientID string } type Templates struct {