Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added WithPerm() #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithInit())
// /path/to/example.log.20170204
```

with Perm
```go
w := cronowriter.MustNew("/path/to/example.log.%Y%m%d", writer.WithPerm(os.FileMode(0600)))

// output file with permissions
// -rw------- /path/to/example.log.20170204
```

## Example using with zap

### [uber-go/zap](https://github.com/uber-go/zap)
Expand Down
11 changes: 10 additions & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type (
path string // current file path
symlink *strftime.Strftime // symbolic link to current file path
fp *os.File // current file pointer
perm os.FileMode
loc *time.Location
mux sync.Locker
log logger
Expand Down Expand Up @@ -48,6 +49,7 @@ func New(pattern string, options ...Option) (*CronoWriter, error) {
mux: new(sync.Mutex), // default mutex enable
log: &nopLogger{},
init: false,
perm: os.FileMode(0644),
}

for _, option := range options {
Expand Down Expand Up @@ -132,6 +134,13 @@ func WithInit() Option {
}
}

// WithPerm sets permissions for created files
func WithPerm(perm os.FileMode) Option {
return func(c *CronoWriter) {
c.perm = perm
}
}

// Write writes to the file and rotate files automatically based on current date and time.
func (c *CronoWriter) Write(b []byte) (int, error) {
c.mux.Lock()
Expand All @@ -154,7 +163,7 @@ func (c *CronoWriter) Write(b []byte) (int, error) {
return c.write(nil, err)
}

fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
fp, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, c.perm)
if err != nil {
return c.write(nil, err)
}
Expand Down
12 changes: 12 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ func TestCronoWriter_Path(t *testing.T) {
}
}

func TestCronoWriter_Perm(t *testing.T) {
expected := os.FileMode(0600)
c := MustNew(filepath.Join(tmpDir, "test.log.%Y%m%d"), WithInit(), WithPerm(expected))
stat, err := os.Stat(c.Path())
if err != nil {
t.Fatal(err)
}
if stat.Mode() != expected {
t.Errorf("Expected file mode %#o, got %#o", expected, stat.Mode())
}
}

func TestCronoWriter_WriteSymlink(t *testing.T) {
stubNow("2017-02-04 16:35:05 +0900")
tests := []struct {
Expand Down