bolt icon indicating copy to clipboard operation
bolt copied to clipboard

Add `file::with_tmpfile()` function

Open jay7x opened this issue 1 year ago • 5 comments

!feature

  • Add file::with_tmpfile() function Create a temporary file and execute the block with the filename created as an argument. The file is deleted after the block execution. This will only operate on the machine you run Bolt on.

jay7x avatar Mar 30 '24 22:03 jay7x

Tempfile.new() has another signature allowing to pass Array[String, 2] as the basename. Not sure if it'd be useful to implement here also..

jay7x avatar Mar 30 '24 23:03 jay7x

Interesting. We have something similar in BoltSpec https://github.com/puppetlabs/bolt/blob/4b30c391d97ad903bdfc27fcaf0c2a910371e898/spec/lib/bolt_spec/files.rb#L7 Seems like most cases you would know what content you want in the file and only want the file around for another process to read from it. Would porting over the BoltSpec function solve your use case?

donoghuc avatar Apr 01 '24 15:04 donoghuc

Seems like most cases you would know what content you want in the file and only want the file around for another process to read from it. Would porting over the BoltSpec function solve your use case?

Consider the case:

$res = file::with_tmpfile('foo') |$filename| {
  run_command("do_something_with '${filename}'", 'localhost')
  upload_file($filename, $targets)
}

There are 2 things:

  1. The file content might be generated by a command which takes the filename as an argument.
  2. BoltSpec function yields a block with a File object as I see. In my case it was a file name.

If we can deal with both cases I'm fine to reuse the BoltSpec code :-D

Actually I'm totally ok to not include this function into the Boltlib (file::delete() should be enough). I can just use my own module with when needed.

jay7x avatar Apr 01 '24 17:04 jay7x

I dont understand the example? What do you need with an empty file in run_command? I guess, for me the only use case I can think of for a "with temp file" you would know the content of the file (or be generating it) explicitly for consumption by something else. Therefore, why would we need a temp file to exist with no way to modify the content?

As far as the return type etc, yeah that would be changed, i was more getting at the use case here and pointing out that we probably would want to have the argument to write content.

donoghuc avatar Apr 01 '24 19:04 donoghuc

I dont understand the example? What do you need with an empty file in run_command?

I mean that the file content may be generated by a command, taking the file name as an argument. In this case the file content is not known in advance. Though, to be honest, for now it's purely "synthetic" case. I'm not sure how many ppl might have this requirement.

jay7x avatar Apr 01 '24 22:04 jay7x