skx / marionette

Something like puppet, for the localhost only.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide file path to environment

adam12 opened this issue · comments

Do you think it would be possible to provide a local variable to a file about which directory it exists in? I'm not keen on hardcoding paths to files and was hoping to use something relative to the file itself.

Inside parser.go we ioutil.ReadFile(path) and then NewWithEnvironment(contents of file, p.e). I wonder if we could update p.e in that case to refer to the realpath of path, minus the filename and extension.

Something like p.e.Set("dir", get real path) and then unset it afterwards somehow.

Then inside my rules:

file { 
  target => "/foo"
  source => "${dir}/file_relative_to_this_file"
}

WDYT?

My immediate reaction is that this should already work:

 // ~/Repos/github.com/skx/blah/input.rule
 file { 
    target => "/foo"
    source => "files/file_relative_to_this_file"
 }

Would use ~/Repos/github.com/skx/blah/files/file_relative_to_this_file. You could also use:

  dir = `pwd`

  ...
      source => "${dir}/files/${arch}/input.template"

Am I missing something myself? Otherwise I don't have any particular objection though.

Both of these solutions work but they are similar in the sense that the current working directory has to be consistent. I think my concerns might be affected by how I'm splitting up my rules, which is different from how you have yours in the second example.

If you had a slightly deeper tree, and for some reason ran from inside that tree, then the paths to the files would be wrong.

I might have to think on this, because maybe it's simpler to enforce the expectation that it's run from a specific directory consistently. I came up with a change here to add DIR to the environment, but it has a bug where the resetting of the DIR value after an include, affects the include too. I think I'd have to duplicate the environment passed in, and I'm not sure how to do that yet.