afero icon indicating copy to clipboard operation
afero copied to clipboard

API for append to a file

Open aranjan7 opened this issue 5 years ago • 4 comments

Is there a supported API to append to a file, or should I use Fs.open() followed by File.Writer().

aranjan7 avatar Mar 21 '19 04:03 aranjan7

You may be looking for this: Append to a file in Go. Just replace the os.OpenFile with the afero equivalent.

johnaoss avatar Sep 28 '19 18:09 johnaoss

@spf13 Thanks for afero! Would you consider a pull request that added the constants from os/file.go to afero?

// Flags to OpenFile wrapping those of the underlying system. Not all
// flags may be implemented on a given system.
const (
	// Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified.
	O_RDONLY int = syscall.O_RDONLY // open the file read-only.
	O_WRONLY int = syscall.O_WRONLY // open the file write-only.
	O_RDWR   int = syscall.O_RDWR   // open the file read-write.
	// The remaining values may be or'ed in to control behavior.
	O_APPEND int = syscall.O_APPEND // append data to the file when writing.
	O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
	O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist.
	O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
	O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.
)

Here's why I think it makes sense to add these to afero:

  1. They help to document how OpenFile can be called.
  2. If they're present, users don't have to mix afero functions and os constants in a single call.

I ran into this myself while using afero to test a command-line tool. I ended up doing the following, which works fine, but wasn't obvious to me at first.

testFS := afero.NewMemMapFs()
// ... later
fh, err := testFS.OpenFile(readingFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

telemachus avatar Jun 03 '21 16:06 telemachus

I don't see why we would want these copied in Afero. It's pretty typical to have stdlib elements used outside the std lib. For example net/http has elements used by virtually every router. It sounds like documentation would address this best. Happy to merge in a fix for better documentation here.

On Thu, Jun 3, 2021 at 12:49 PM Peter Aronoff @.***> wrote:

@spf13 https://github.com/spf13 Thanks for afero! Would you consider a pull request that added the constants from os/file.go https://github.com/golang/go/blob/master/src/os/file.go#L71-L84 to afero?

// Flags to OpenFile wrapping those of the underlying system. Not all// flags may be implemented on a given system.const ( // Exactly one of O_RDONLY, O_WRONLY, or O_RDWR must be specified. O_RDONLY int = syscall.O_RDONLY // open the file read-only. O_WRONLY int = syscall.O_WRONLY // open the file write-only. O_RDWR int = syscall.O_RDWR // open the file read-write. // The remaining values may be or'ed in to control behavior. O_APPEND int = syscall.O_APPEND // append data to the file when writing. O_CREATE int = syscall.O_CREAT // create a new file if none exists. O_EXCL int = syscall.O_EXCL // used with O_CREATE, file must not exist. O_SYNC int = syscall.O_SYNC // open for synchronous I/O. O_TRUNC int = syscall.O_TRUNC // truncate regular writable file when opened. )

Here's why I think it makes sense to add these to afero:

  1. They help to document how OpenFile can be called.
  2. If they're present, users don't have to mix afero functions and os constants in a single call.

I ran into this myself while using afero to test a command-line tool. I ended up doing the following, which works fine, but wasn't obvious to me at first.

testFS := afero.NewMemMapFs()// ... laterfh, err := testFS.OpenFile(readingFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/spf13/afero/issues/203#issuecomment-854023668, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABKKZG5GGXFH4PT4M7GD53TQ6XBVANCNFSM4HACYZXQ .

spf13 avatar Jun 04 '21 01:06 spf13

Okay, that makes sense. I will take a crack at adding to the docs. Thanks.

telemachus avatar Jun 04 '21 10:06 telemachus