Is there a recommended solution for templating? I'...
# typescript
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
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.
i think this should work
Copy code
const drone = new nomad.Job("lightsoutgames-drone", { jobspec: readFileSync("lightsoutgames-drone.nomad").toString() })
That would just embed the raw file content. It wouldn't evaluate snippets like ``DRONE_GITEA_CLIENT_SECRET = "${config.requireSecret('lightsoutgamesDroneGiteaClientSecret')}"`` in the jobspec.
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
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
. And again, `eval("``"+"${1+2}"+"``")` works in the REPL, so this should work for quick-and-dirty templating I'd think.
does that eval work in the repl with readFileSync? maybe the file has special characters that mess up the eval?