hashicorp / levant

An open source templating and deployment tool for HashiCorp Nomad jobs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

replace function does not work as described

kpweiler opened this issue · comments

Description
The example given in the documentation for the replace function does not work as described. I've used the exact example in the documentation and instead of the string "Batman and Catwoman" being rendered, I just got "Catwomen".

Relevant Nomad job specification file
This obviously isn't a valid nomad spec, but this simple example levant template should suffice.

job "test-job" {
  group "test-group" {
    task "test-task" {
      template {
        data = "[[ replace "Batman and Robin" "Robin" "Catwoman" ]]"

        destination = "local/file"
        perms = "755"
      }
    }
  }
}

renders

job "test-job" {
  group "test-group" {
    task "test-task" {
      template {
        data = "Catwoman"

        destination = "local/file"
        perms = "755"
      }
    }
  }
}

Output of levant version:

Levant v0.3.0

Output of consul version:

not relevant

Output of nomad version:

not relevant

Additional environment details:

Debug log outputs from Levant:

2021-08-12T13:25:00-05:00 |DEBU| template/render: no variable file passed, trying defaults
2021-08-12T13:25:00-05:00 |DEBU| helper/files: no default var-file found
2021-08-12T13:25:00-05:00 |DEBU| template/render: no command line variables passed
2021-08-12T13:25:00-05:00 |DEBU| template/funcs: renaming "add" function to "levantAdd"
2021-08-12T13:25:00-05:00 |DEBU| template/funcs: renaming "replace" function to "levantReplace"
2021-08-12T13:25:00-05:00 |DEBU| template/funcs: renaming "env" function to "levantEnv"

with some help from @mattgodbolt, we noticed that the arguments are reversed:

$ cat thing.nomad
[[ replace  "Robin" "Catwoman" "Batman and Robin"]]

$ levant render thing.nomad
Batman and Catwoman

$ cat other_thing.nomad
[[ replace "Catwoman" "Robin" "Batman and Robin" ]]

$ levant render other_thing.nomad
Batman and Robin

This should be [[ "Batman and Robin" | replace "Robin" "Catwoman" ]]. Making an update to the docs now. Go template always passes the pipeline as the last argument to the template functions.