Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file::with_tmpfile() function #3288

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jay7x
Copy link
Contributor

@jay7x jay7x commented Mar 30, 2024

!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.

!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 jay7x requested a review from a team as a code owner March 30, 2024 22:58
@jay7x
Copy link
Contributor Author

jay7x commented Mar 30, 2024

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..

@donoghuc
Copy link
Member

donoghuc commented Apr 1, 2024

Interesting. We have something similar in BoltSpec

def with_tempfile_containing(name, contents, extension = nil)
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?

@jay7x
Copy link
Contributor Author

jay7x commented Apr 1, 2024

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.

@donoghuc
Copy link
Member

donoghuc commented Apr 1, 2024

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.

@jay7x
Copy link
Contributor Author

jay7x commented Apr 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants