[Feature] tmp file and dir function in Stdlib
for a correct use of temporary files and dirs i suggest this functions :
temp_dir(path: Text = "")
this function create a temp dir with mktemp and delete dir and content on exit with trap.
if path is empty de default path can be : /tmp/{APP_NAME}_XXXX
maybe APP_NAME can be set by a shell variable
we can save path to a shell variable. If we recall this function with empty path the function return the shell variable content.
this function return path to tmp dir.
temp_file(path: Text)
this function call temp_dir with empty path
if no path was specified we can create a $RANDOM filename
maybe we can create subdir automatically if path is a subdir ("/download/api/result.json")
create file temp_dir()+path
return absolute path /tmp/{APP_NAME}_XXXX/download/api/result.json
run_in_tmp(path: Text = "")
this function call cd temp_dir(path)
this is useful if you want a temporary file just call temp_file()
what does path supposed to mean? why not just call mktemp? i think it has a template option which can let u specify your own filename but with X's for it to put randomness in there
I was thinking, maybe a block like:
tmpdir {
$touch stuff$
$rm stuff$
}
In this way the folder is automatically created in /tmp and removed at the end.
So we can use it freely in our tests and the code is more readable without using functions.
Nice idea, and great if you can manage it; but I'm not sure how you could implement what is essentially a try/finally block in Bash.
I think that on bash side it will be just pure bash code that executes the various commands, before and after the stuff in the code block itself.
Not related to the block, #718 introduced temp_dir_create(template), which has the main focus on making a directory rather than a file.
Good job @lens0021 do you think that it would be hard to implement temp file as well? Would it be like really simple to do?
It would. I reset the assignee as 1 year passed.
@lens0021 In this case I'm closing this issue. A temporary file can be easily create by created a temporary directory and then creating a whatever file inside. If this workaround is too often used to create a single temporary file, then I advise to open a new issue.