gofrs / flock

Thread-safe file locking library in Go
https://pkg.go.dev/github.com/gofrs/flock
BSD 3-Clause "New" or "Revised" License
578 stars 66 forks source link

feat: add option to define flag and permissions of the file #76

Closed ldez closed 4 months ago

ldez commented 4 months ago

To avoid breaking changes (constructor signature and existing behavior), I extended the current constructor with a variadic of functions to add options.

The approach avoids creating a new constructor, like NewWithOptions(path string, opts Options), and breaking the current constructor signature.

func New(path string, opts ...Option) *Flock {
    ...
}

There are 2 options:

    _ = flock.New("example", flock.SetFlag(os.O_CREATE|os.O_RDWR)))
    _ = flock.New("example", flock.SetPermissions(os.FileMode(0o600)))
    _ = flock.New("example", flock.SetFlag(os.O_CREATE|os.O_RDWR), flock.SetPermissions(os.FileMode(0o600)))

Fixes #59