@@ -55,6 +55,7 @@ type pubCmd struct {
5555 jetstream bool
5656 sendOn string
5757 quiet bool
58+ templates bool
5859}
5960
6061func configurePubCommand (app commandHost ) {
@@ -94,6 +95,7 @@ Available template functions are:
9495 pub .Flag ("send-on" , fmt .Sprintf ("When to send data from stdin: '%s' (default) or '%s'" , sendOnEOF , sendOnNewline )).
9596 Default ("eof" ).EnumVar (& c .sendOn , sendOnNewline , sendOnEOF )
9697 pub .Flag ("quiet" , "Show just the output received" ).Short ('q' ).UnNegatableBoolVar (& c .quiet )
98+ pub .Flag ("templates" , "Enables template functions in the body and subject (does not affect headers)" ).Default ("true" ).BoolVar (& c .templates )
9799
98100 requestHelp := `Body and Header values of the messages may use Go templates to
99101create unique messages.
@@ -140,6 +142,22 @@ func (c *pubCmd) prepareMsg(subj string, body []byte, seq int) (*nats.Msg, error
140142 return msg , iu .ParseStringsToMsgHeader (c .hdrs , seq , msg )
141143}
142144
145+ func (c * pubCmd ) parseTemplates (request string , ctr int ) (string , string ) {
146+ if c .templates {
147+ body , err := iu .PubReplyBodyTemplate (c .body , request , ctr )
148+ if err != nil {
149+ log .Printf ("Could not parse body template: %s" , err )
150+ }
151+
152+ subj , err := iu .PubReplyBodyTemplate (c .subject , request , ctr )
153+ if err != nil {
154+ log .Printf ("Could not parse subject template: %s" , err )
155+ }
156+ return string (body ), string (subj )
157+ }
158+ return c .body , c .subject
159+ }
160+
143161func (c * pubCmd ) doReq (nc * nats.Conn , progress * progress.Tracker ) error {
144162 logOutput := ! c .raw && progress == nil
145163
@@ -148,17 +166,9 @@ func (c *pubCmd) doReq(nc *nats.Conn, progress *progress.Tracker) error {
148166 log .Printf ("Sending request on %q\n " , c .subject )
149167 }
150168
151- body , err := iu .PubReplyBodyTemplate (c .body , "" , i )
152- if err != nil {
153- log .Printf ("Could not parse body template: %s" , err )
154- }
169+ body , subj := c .parseTemplates ("" , i )
155170
156- subj , err := iu .PubReplyBodyTemplate (c .subject , "" , i )
157- if err != nil {
158- log .Printf ("Could not parse subject template: %s" , err )
159- }
160-
161- msg , err := c .prepareMsg (string (subj ), body , i )
171+ msg , err := c .prepareMsg (subj , []byte (body ), i )
162172 if err != nil {
163173 return err
164174 }
@@ -257,22 +267,13 @@ func (c *pubCmd) doReq(nc *nats.Conn, progress *progress.Tracker) error {
257267func (c * pubCmd ) doJetstream (nc * nats.Conn , progress * progress.Tracker ) error {
258268 for i := 1 ; i <= c .cnt ; i ++ {
259269 start := time .Now ()
260- body , err := iu .PubReplyBodyTemplate (c .body , "" , i )
261- if err != nil {
262- log .Printf ("Could not parse body template: %s" , err )
263- }
264-
265- subj , err := iu .PubReplyBodyTemplate (c .subject , "" , i )
266- if err != nil {
267- log .Printf ("Could not parse subject template: %s" , err )
268- }
270+ body , subj := c .parseTemplates ("" , i )
269271
270- msg , err := c .prepareMsg (string ( subj ), body , i )
272+ msg , err := c .prepareMsg (subj , [] byte ( body ) , i )
271273 if err != nil {
272274 return err
273275 }
274276
275- msg .Subject = string (subj )
276277 if ! c .quiet {
277278 log .Printf ("Published %d bytes to %q\n " , len (body ), c .subject )
278279 }
@@ -436,17 +437,9 @@ func (c *pubCmd) publish(_ *fisk.ParseContext) error {
436437 }
437438
438439 for i := 1 ; i <= c .cnt ; i ++ {
439- body , err := iu .PubReplyBodyTemplate (c .body , "" , i )
440- if err != nil {
441- log .Printf ("Could not parse body template: %s" , err )
442- }
443-
444- subj , err := iu .PubReplyBodyTemplate (c .subject , "" , i )
445- if err != nil {
446- log .Printf ("Could not parse subject template: %s" , err )
447- }
440+ body , subj := c .parseTemplates ("" , i )
448441
449- msg , err := c .prepareMsg (string ( subj ), body , i )
442+ msg , err := c .prepareMsg (subj , [] byte ( body ) , i )
450443 if err != nil {
451444 errCh <- err
452445 return
0 commit comments