I have a struct Artifact
and the following two functions of interest:
type Artifact struct {
Name string
ZipFile io.ReadWriter
}
func New(name string, files []string, zipArchiveStream io.ReadWriter) *Artifact {}
func (a *Artifact) Upload() error {}
So here's the problem: before passing that io.ReadWriter
around, I was using the file name. There are three operations I need to do with the zip file:
- Add the necessary files to it (i.e. Write)
- Re-read it to calculate the SHA256 sum (i.e. Seek, Read)
- Upload it to an S3 bucket (i.e. Seek, Read)
Well so before, when using the file name, I opened, closed, re-opened etc. for every operation. I started writing the unit tests however, and I realized my code was not really testable so I decided I would use io.ReadWriter
so I could open files in real code usage, and pass buffers in testing.
The problem right now is that after the stream is read, its internal pointer needs to be reset in order to perform a second read (i.e. calculating the checksum, then uploading), but as far as I have read, streams cannot be rewinded. How should I approach this problem? because it seems like some is conceptually wrong with my current approach.