python - Template variable replacements only. Is text/Template a good fit? -


i'm looking efficient way replace bunch of placeholders/tokens in user supplied text file, corresponding values stored in simple map or environment vars. thing template file supplied end user, i'm looking "safe" way variable replacements, without risk of code execution, etc.

go's standard "text/template" work replacement imposes specific formatting requirements (e.g. dot "." before key) , opens other possibilities function invocations, pipelines, etc.

so i'm looking for, ideally, function can parse text file configurable delimiters ("{{}}" or "${}" or "$##$") , replace detected tokens lookups supplied map or env var values. similar python's string.template (https://docs.python.org/2.6/library/string.html?highlight=string.template#string.template) does.

is there easy way configure or reuse text/template library this? there other approaches fit use case better? i've looked non-golang options (like envsubtr, awk , sed scripts etc.) feel free go outside of go if fits better.

sample input file ('template.properties'):

var1=$#var_1#$ var2=$#var_2#$ 

sample input data:

var_1 = apples var_2 = oranges 

expected output after processing:

var1=apples var2=oranges 

this work long variable names don't contain ere metacharacters:

$ cat tst.awk nr==fnr { var2val[$1] = $nf; next } {     (var in var2val) {         sub("[$]#"var"#[$]",var2val[var])     }     print }  $ awk -f tst.awk input.data template.properties var1=apples var2=oranges 

wrt comment below having mappings in variables instead of in input.data, might you're looking for:

$ cat tst.awk begin {     split(vars,tmp)     (i in tmp) {         var2val[tmp[i]] = environ[tmp[i]]     } } {     (var in var2val) {         sub("[$]#"var"#[$]",var2val[var])     }     print } 

will work shell variables like:

$ var_1=apples var_2=oranges gawk -v vars="var_1 var_2" -f tst.awk template.properties var1=apples var2=oranges 

or:

$ export var_1=apples $ export var_2=oranges $ gawk -v vars="var_1 var_2" -f tst.awk template.properties var1=apples var2=oranges 

or:

$ var_1=apples $ var_2=oranges $ var_1="$var_1" var_2="$var_2" gawk -v vars="var_1 var_2" -f tst.awk template.properties var1=apples var2=oranges 

note gawk-specific due environ , requires var_1 etc. exported or set on command line have above.

or maybe want:

$ cat tst.awk begin {     var2val["var_1"] = var_1     var2val["var_2"] = var_2 } {     (var in var2val) {         sub("[$]#"var"#[$]",var2val[var])     }     print }  $ var_1=apples $ var_2=oranges $ awk -v var_1="$var_1" -v var_2="$var_2" -f tst.awk template.properties var1=apples var2=oranges 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -