Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[Feature] tmp file and dir function in Stdlib

Open CymDeveloppement opened this issue 1 year ago • 4 comments

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()

CymDeveloppement avatar Jul 26 '24 08:07 CymDeveloppement

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

b1ek avatar Jul 31 '24 00:07 b1ek

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.

Mte90 avatar Oct 02 '24 10:10 Mte90

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.

hdwalters avatar Oct 02 '24 11:10 hdwalters

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.

Mte90 avatar Oct 02 '24 13:10 Mte90

Not related to the block, #718 introduced temp_dir_create(template), which has the main focus on making a directory rather than a file.

lens0021 avatar Jul 09 '25 15:07 lens0021

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?

Ph0enixKM avatar Aug 09 '25 16:08 Ph0enixKM

It would. I reset the assignee as 1 year passed.

lens0021 avatar Aug 10 '25 00:08 lens0021

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

Ph0enixKM avatar Aug 16 '25 05:08 Ph0enixKM