https://pulumi.com logo
#typescript
Title
# typescript
f

fresh-honey-15888

09/14/2021, 4:42 PM
Is there a recommended solution for templating? I'm submitting jobs to Nomad and want to embed secrets in the jobspecs. I thought I could do something like: `const drone = new nomad.Job("lightsoutgames-drone", { jobspec: eval("`" + readFileSync("lightsoutgames-drone.nomad").toString() + "`") })`` then I can treat the jobspec like a template string and embed secrets in it. The
eval
trick works in the REPL but fails in Pulumi. I can cut-and-paste the error, but first wanted to make sure this wasn't a terrible idea. FWIW, this is just a personal setup, so I'm not super worried about how robust this is.
s

steep-toddler-94095

09/14/2021, 5:41 PM
i think this should work
Copy code
const drone = new nomad.Job("lightsoutgames-drone", { jobspec: readFileSync("lightsoutgames-drone.nomad").toString() })
f

fresh-honey-15888

09/14/2021, 6:14 PM
That would just embed the raw file content. It wouldn't evaluate snippets like ``DRONE_GITEA_CLIENT_SECRET = "${config.requireSecret('lightsoutgamesDroneGiteaClientSecret')}"`` in the jobspec.
s

steep-toddler-94095

09/14/2021, 6:28 PM
oh right. i suppose one option is to paste the file contents as a templated string with the backticks and replace the env vars with JS/TS vars
f

fresh-honey-15888

09/14/2021, 7:05 PM
FWIW, the error I get is: error: Running program 'C:\Users\Nolan\projects\infrastructure' failed with an unhandled exception: SyntaxError: Unexpected identifier at Object.<anonymous> (C\Users\Nolan\projects\infrastructure\index.ts42:128) at Module._compile (nodeinternal/modules/cjs/loader1101:14) at Module.m._compile (C\Users\Nolan\projects\infrastructure\node modules\ts node\src\index.ts439:23) at Module._extensions..js (nodeinternal/modules/cjs/loader1153:10) at Object.require.extensions.<computed> [as .ts] (C\Users\Nolan\projects\infrastructure\node modules\ts node\src\index.ts442:12) at Module.load (nodeinternal/modules/cjs/loader981:32) at Function.Module._load (nodeinternal/modules/cjs/loader822:12) at Module.require (nodeinternal/modules/cjs/loader1005:19) at require (nodeinternal/modules/cjs/helpers94:18) at Object.<anonymous> (C:\Users\Nolan\projects\infrastructure\node_modules\@pulumi\pulumi\cmd\run\run.js21931) Right after
toString()
. And again, `eval("``"+"${1+2}"+"``")` works in the REPL, so this should work for quick-and-dirty templating I'd think.
s

steep-toddler-94095

09/14/2021, 8:38 PM
does that eval work in the repl with readFileSync? maybe the file has special characters that mess up the eval?