Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively render fileContents #305

Open
rorylshanks opened this issue Oct 3, 2019 · 4 comments · May be fixed by #429
Open

Recursively render fileContents #305

rorylshanks opened this issue Oct 3, 2019 · 4 comments · May be fixed by #429
Labels
stage/accepted theme/template/render Relates to the rendering of templates theme/template Relates to templating in general type/enhancement

Comments

@rorylshanks
Copy link

Hi there!

I noticed that when we include a file using the fileContents tag, and that file has levant templating in it, the included template is not recursively rendered. This would be a very useful feature to have allowing for large numbers of config files to be included in the job without putting them all in the one file, while also allowing for a single templating tool to manage them all.

Thanks!

@rorylshanks rorylshanks changed the title Recursively render IncludeFile Recursively render fileContents Oct 3, 2019
@gaspo53
Copy link

gaspo53 commented Jun 2, 2020

Would be very useful to have this!

@jrasell jrasell added stage/accepted theme/template Relates to templating in general theme/template/render Relates to the rendering of templates type/enhancement and removed enhancement labels Nov 27, 2020
@bjornicus
Copy link

it would be ok, IMHO to have a separate templateFileContents or some such function to do this, or a parameter to fileContents to control it.

in the meantime, for simple things I have found that something like this can be used:
[[fileContents "my.config" | replace "$ENV" (env "ENV")]]

but would love to have the full levant templating power in those included files.

atavakoliyext added a commit to atavakoliyext/levant that referenced this issue Oct 15, 2021
The include function allows templates to be composed of other templates.

Fixes hashicorp#305
@atavakoliyext atavakoliyext linked a pull request Oct 15, 2021 that will close this issue
atavakoliyext added a commit to atavakoliyext/levant that referenced this issue Oct 16, 2021
The include function allows templates to be composed of other templates.

Fixes hashicorp#305
atavakoliyext added a commit to atavakoliyext/levant that referenced this issue Oct 18, 2021
The include function allows templates to be composed of other templates.

Fixes hashicorp#305
@foozmeat
Copy link

foozmeat commented Nov 9, 2021

I'm resorting to this 🤢 :

while grep '\[\[' job.nomad; do
  echo "*****************************************"
  levant render \
  -log-level=WARN \
  -out=job.nomad \
  job.nomad
done

@milesrichardson
Copy link

The solution from @foozmeat is pretty much the only option AFAICT, pretty disgusting :D

If your job spec is simple enough, and you only need to render it twice, then you can use file redirection like this:

levant render -var-file config.json <(levant render -var-file config.json job-1.hcl)

which works:

❯ levant render -var-file config.json <(levant render -var-file config.json job-1.hcl)
2022-10-17T20:09:02Z |INFO| helper/variable: using variable with key foo and value map[bar:map[bazz:map[buzz:1]]] from file
2022-10-17T20:09:02Z |INFO| helper/variable: using variable with key foo and value map[bar:map[bazz:map[buzz:1]]] from file
job_2_stanza {
  config = "eyJiYXp6Ijp7ImJ1enoiOjF9fQ==
}


job_1_stanza {
  config = "eyJiYXp6Ijp7ImJ1enoiOjF9fQ==
}

Personally I'm just gonna accept the non-DRY config


How to reproduce the bug (Levant v0.3.0)

For anyone coming from google search who just wants to see whether this works, here's a minimal reproduction of the current bug (Levant v0.3.0).

Given example files job-1.hcl and job-2.hcl which both include a Levant template referencing variables in config.json:

tee config.json << EOC
{
  "foo": {
    "bar": {
      "bazz": {
        "buzz": 1
      }
    }
  }
}
EOC

tee job-1.hcl << EOJ
[[ fileContents "./job-2.hcl" ]]

job_1_stanza {
  config = "[[ .foo.bar | toJson | b64enc ]]
}
EOJ

tee job-2.hcl << EOK
job_2_stanza {
  config = "[[ .foo.bar | toJson | b64enc ]]
}
EOK

job-2.hcl renders its template When rendered individually:

#❯ levant render -var-file config.json job-2.hcl
#2022-10-17T19:58:35Z |INFO| helper/variable: using variable with key foo and value map[bar:map[bazz:map[buzz:1]]] from file
job_2_stanza {
  config = "eyJiYXp6Ijp7ImJ1enoiOjF9fQ==
}

but not when rendered via fileContents as part of job-1.hcl:

#❯ levant render -var-file config.json job-1.hcl
#2022-10-17T20:04:47Z |INFO| helper/variable: using variable with key foo and value map[bar:map[bazz:map[buzz:1]]] from file
job_2_stanza {
  config = "[[ .foo.bar | toJson | b64enc ]]
}


job_1_stanza {
  config = "eyJiYXp6Ijp7ImJ1enoiOjF9fQ==
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage/accepted theme/template/render Relates to the rendering of templates theme/template Relates to templating in general type/enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants