Hi folks,
I'm considering (in draft PR
https://github.com/benhoyt/goawk/pull/296) adding the option to GoAWK to allow customizing the file read/write operations that it performs when reading files, creating new files with AWK's ">" operator, or appending to them with AWK's ">>" operator.
I'd like it to be shaped like io/fs.FS. However, that only supports read-only files (Open returns an fs.File). So my thought is to have an interface extension like so:
type WriteFS interface {
fs.FS
Create(name string) (io.WriteCloser, error)
Append(name string) (io.WriteCloser, error)
}
Which are all the operations GoAWK needs.
Then if you pass a regular fs.FS (via Config.FileSystem) it'll only allow reads, but if you psas something that implements WriteFS as defined above, it'll support read and write.
I notice that some other libraries define WriteFS as:
type WriteFS interface {
fs.FS
OpenFile(name string, flag int, perm os.FileMode) (io.WriteCloser, error)
}
That fits nicely with os.OpenFile, but it pulls in a whole lot of POSIX-specific stuff and generality with the "flag" and "perm" arguments, when I only need Create and Append very specifically.
Does what I've proposed seem reasonable? Any better ideas for handling this?
-Ben