Skip to content

Commit a337379

Browse files
authored
Merge pull request #1413 from thewizzy/no-template
Implement --templates flag to disable template processing on publish
2 parents 6d7578f + cb5bc7d commit a337379

File tree

2 files changed

+67
-31
lines changed

2 files changed

+67
-31
lines changed

cli/pub_command.go

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type pubCmd struct {
5555
jetstream bool
5656
sendOn string
5757
quiet bool
58+
templates bool
5859
}
5960

6061
func 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
99101
create 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+
143161
func (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 {
257267
func (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

nats/tests/pub_command_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,46 @@ func TestCLIPubSendOnNewline(t *testing.T) {
106106
})
107107
})
108108
}
109+
110+
func TestCLIPubTemplates(t *testing.T) {
111+
srv, _, _ := setupJStreamTest(t)
112+
defer srv.Shutdown()
113+
nc, _, _ := prepareHelper(srv.ClientURL())
114+
t.Run("Publish --templates (Default)", func(t *testing.T) {
115+
subject := "test-templates"
116+
var messages []string
117+
expected := "Count: 1"
118+
sub, _ := nc.Subscribe(subject, func(m *nats.Msg) {
119+
messages = append(messages, string(m.Data))
120+
})
121+
122+
runNatsCliWithInput(t, expected, fmt.Sprintf("--server='%s' pub --force-stdin %s \"Count: {{ Count }}\"", srv.ClientURL(), subject))
123+
_ = sub.Unsubscribe()
124+
125+
if len(messages) != 1 {
126+
t.Errorf("expected 1 message and received %d", len(messages))
127+
}
128+
if len(messages) > 0 && messages[0] != expected {
129+
t.Errorf("expected message %q got %q", expected, messages[0])
130+
}
131+
})
132+
133+
t.Run("Publish --no-templates", func(t *testing.T) {
134+
subject := "test-no-templates"
135+
var messages []string
136+
expected := "Count: {{ Count }}"
137+
sub, _ := nc.Subscribe(subject, func(m *nats.Msg) {
138+
messages = append(messages, string(m.Data))
139+
})
140+
141+
runNatsCliWithInput(t, expected, fmt.Sprintf("--server='%s' pub --force-stdin %s \"Count: {{ Count }}\" --no-templates", srv.ClientURL(), subject))
142+
_ = sub.Unsubscribe()
143+
144+
if len(messages) != 1 {
145+
t.Errorf("expected 1 message and received %d", len(messages))
146+
}
147+
if len(messages) > 0 && messages[0] != expected {
148+
t.Errorf("expected message %q got %q", expected, messages[0])
149+
}
150+
})
151+
}

0 commit comments

Comments
 (0)