RexOps / Rex

Rex, the friendly automation framework
https://www.rexify.org
717 stars 223 forks source link

RFC: Template using PadWalker to access variables from inside task #833

Open krimdomu opened 8 years ago

krimdomu commented 8 years ago

Hi,

it would be possible with the PadWalker Module (https://metacpan.org/pod/PadWalker) to get the variables that the user declared inside a task. With this it would be possible to the something like this:

task "mytask", sub {
  my $x = 5;
  file "/tmp/foo.txt", content => template("mytemplate.tpl");
};
# the template
counter: <%=  $x %>

So it would be possible to access the variables from inside a task in the template that gets parsed in the task. With this the user doesn't need to pass the variables to the template function.

What do you think about this?

ferki commented 8 years ago

I love the idea! :+1:

krimdomu commented 8 years ago

It's author says in the docs:

I wouldn't recommend using PadWalker directly in production code

Yes, but it also says ;-)

Some of the modules that use PadWalker internally are certainly safe for and useful in production.

No the variables wouldn't be possible to modify in the template.

kablamo commented 8 years ago

Interesting.

Some questions

Also it kind of violates the idea of low coupling. If you change the task, you gotta go into the template and make sure something wasn't affected there. Whereas before you could just look at was was passed in.

But I guess thats true of automatically passing the task parameters to the template as well which I thought was a great idea a couple weeks ago....

krimdomu commented 8 years ago

I haven't done tests yet. So i don't know if it is fast or not. I just wanted to know how the people thinks about such a feature before working on it.

Yes, the debugging might be harder, indeed. But this feature should not replace the current behavor. So if user provide their template variables as they are doing today, it should work. And also the manually passed parameters must have precedence over the ones getting with PadWalker.

reneeb commented 8 years ago

I use PadWalker in production code. Not very often, but sometimes it's really handy...

IMHO this is a feature that should be enabled explicitly and needs lots of tests...

ferki commented 8 years ago

@jfried:

No the variables wouldn't be possible to modify in the template.

:+1:

@kablamo: I think low coupling is already not there anymore, as CMDB variables are also exposed towards the templates :)

@reneeb: thanks for the feedback on PadWalker! I also think this behaviour can come in handy, and it should be explicitly requested (i.e. being explicit about "I know I want this").