Skip to content

Commit

Permalink
Added template parsing to byte payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Feb 27, 2022
1 parent aae4901 commit ceb103b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func randomUUID() string {
return uuid.New().String()
}

func parseByteTemplate(input []byte) []byte {
return []byte(parseStringTemplate(string(input)))
}

func parseStringTemplate(input string) string {
funcMap := template.FuncMap{
"random_uuid": randomUUID,
Expand Down Expand Up @@ -102,7 +106,7 @@ func httpJob(ctx context.Context, args JobArgs) error {
return err
}
for jobConfig.Next(ctx) {
req, err := http.NewRequest(parseStringTemplate(jobConfig.Method), parseStringTemplate(jobConfig.Path), bytes.NewReader(jobConfig.Body))
req, err := http.NewRequest(parseStringTemplate(jobConfig.Method), parseStringTemplate(jobConfig.Path), bytes.NewReader(parseByteTemplate(jobConfig.Body)))
if err != nil {
log.Printf("error creating request: %v", err)
continue
Expand Down Expand Up @@ -153,7 +157,7 @@ func tcpJob(ctx context.Context, args JobArgs) error {
continue
}

_, err = conn.Write(jobConfig.Body)
_, err = conn.Write(parseByteTemplate(jobConfig.Body))
if err != nil {
log.Printf("error sending body to [%v]: %v", tcpAddr, err)
} else {
Expand Down Expand Up @@ -184,7 +188,7 @@ func udpJob(ctx context.Context, args JobArgs) error {
}

for jobConfig.Next(ctx) {
_, err = conn.Write(jobConfig.Body)
_, err = conn.Write(parseByteTemplate(jobConfig.Body))
if err != nil {
log.Printf("error sending body to [%v]: %v", udpAddr, err)
} else {
Expand Down

0 comments on commit ceb103b

Please sign in to comment.