risor-io / risor

Fast and flexible scripting for Go developers and DevOps.
https://risor.io
Apache License 2.0
581 stars 24 forks source link

feat: add in memory file type #170

Closed luisdavim closed 6 months ago

luisdavim commented 6 months ago

When embedding Risor into my project, I wanted to be able to capture the whole output of the script. For now I have a copy of this code in my project but this might be useful to others so I'm adding it here.

    stdout := &InMemFile{}
    vos := ros.NewVirtualOS(ctx, ros.WithStdout(stdout))
    ctx = ros.WithOS(ctx, vos)
luisdavim commented 6 months ago

looks like I completely missed this: https://github.com/risor-io/risor/blob/main/os/in_memory_file.go (no clue how, I actually looked for it) 🤦 I'll update the PR to add some of the extra functionality from my version to the existing one.

luisdavim commented 6 months ago

@myzie I still don't know how I missed the pre-existing implementation and I'm kind of embarrassed by it but now that this is here, we have 3 options:

  1. Merge this as is and keep both implementations, the pre-existing one is fine for stdin and stdout use cases but writes are always append only and seek is not supported.
  2. Drop the old implementation...
  3. Close this PR and keep things as they are, the main disadvantage of the new implementation is that it's more complex and, I'm not 100% sure but it might be slightly less performant... The advantages are what's stated in point no. 1

I'm ok with any of the above options, please let me know what you think.

PS: I've updated the PR to rename the previous implementation to BufferFile and added tests.