Skip to content

Commit

Permalink
refactor: optimize and fix (#1)
Browse files Browse the repository at this point in the history
* refactor: optimize and fix

* fix: lint

* feat: use upstream test cases

* fix: lint

* fix: test

* fix: test

* feat: add example test
  • Loading branch information
devhaozi authored Dec 4, 2023
1 parent e8606da commit a26a795
Show file tree
Hide file tree
Showing 27 changed files with 1,598 additions and 247 deletions.
25 changes: 25 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
version: 2
updates:
- package-ecosystem: gomod
directory: /
labels:
- "🤖 Dependencies"
schedule:
interval: daily
groups:
go-modules:
patterns:
- "*"
open-pull-requests-limit: 100
- package-ecosystem: github-actions
directory: /
labels:
- "🤖 Dependencies"
schedule:
interval: daily
groups:
github-actions:
patterns:
- "*"
open-pull-requests-limit: 100
38 changes: 0 additions & 38 deletions .github/workflows/ci.yml

This file was deleted.

32 changes: 32 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Lint
on:
push:
branches:
- master
pull_request:
permissions:
contents: read
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 'stable'
cache: false
- name: go mod pakcage cache
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('go.mod') }}
- name: Install dependencies
run: go mod tidy
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
skip-cache: true
skip-pkg-cache: true
skip-build-cache: true
version: latest
args: --timeout=30m ./...
11 changes: 11 additions & 0 deletions .github/workflows/pr-check-title.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: PR Check Title
on:
pull_request:
jobs:
pr-check-title:
runs-on: ubuntu-latest
steps:
- uses: Slashgear/[email protected]
with:
regexp: "^(feat|fix|docs|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release|revert)(\\(.+\\))?!?: .+"
helpMessage: "Your PR title is invalid. Please follow the Conventional Commits specification: https://www.conventionalcommits.org/en/v1.0.0/"
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test
on:
push:
branches:
- master
pull_request:
jobs:
test:
strategy:
matrix:
go:
- "stable"
- "oldstable"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- name: go mod pakcage cache
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.mod') }}
- name: Install dependencies
run: go mod tidy
- name: Run tests
run: go test ./...
56 changes: 0 additions & 56 deletions .golangci.yml

This file was deleted.

5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

8 changes: 0 additions & 8 deletions Changes

This file was deleted.

24 changes: 0 additions & 24 deletions Makefile

This file was deleted.

11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ file-rotatelogs

Provide an `io.Writer` that periodically rotates log files from within the application. Port of [File::RotateLogs](https://metacpan.org/release/File-RotateLogs) from Perl to Go.

[![Build Status](https://travis-ci.org/lestrrat-go/file-rotatelogs.png?branch=master)](https://travis-ci.org/lestrrat-go/file-rotatelogs)
[![Go Doc](https://godoc.org/github.com/goravel/file-rotatelogs?status.svg)](https://godoc.org/github.com/goravel/file-rotatelogs)
[![Release](https://img.shields.io/github/release/goravel/file-rotatelogs.svg)](https://github.com/goravel/file-rotatelogs/releases)
[![Test](https://github.com/goravel/file-rotatelogs/actions/workflows/test.yml/badge.svg)](https://github.com/goravel/file-rotatelogs/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/goravel/file-rotatelogs)](https://goreportcard.com/report/github.com/goravel/file-rotatelogs)
[![License](https://img.shields.io/github/license/goravel/file-rotatelogs)](https://img.shields.io/github/license/goravel/file-rotatelogs)

[![GoDoc](https://godoc.org/github.com/lestrrat-go/file-rotatelogs?status.svg)](https://godoc.org/github.com/lestrrat-go/file-rotatelogs)

# SYNOPSIS

Expand All @@ -17,7 +20,7 @@ import (
"net/http"

apachelog "github.com/lestrrat-go/apache-logformat"
rotatelogs "github.com/goravel/file-rotatelogs"
rotatelogs "github.com/goravel/file-rotatelogs/v2"
)

func main() {
Expand Down Expand Up @@ -61,7 +64,7 @@ such as `github.com/lestrrat-go/apache-logformat`.
```go
import(
"log"
"github.com/goravel/file-rotatelogs"
"github.com/goravel/file-rotatelogs/v2"
)

func main() {
Expand Down
15 changes: 10 additions & 5 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package rotatelogs_test
package rotatelogs

import (
"fmt"
"io/ioutil"
"os"
)

func ExampleForceNewFile() {
logDir, err := ioutil.TempDir("", "rotatelogs_test")
logDir, err := os.MkdirTemp("", "rotatelogs_test")
if err != nil {
fmt.Println("could not create log directory ", err)

Expand Down Expand Up @@ -39,14 +38,20 @@ func ExampleForceNewFile() {
}
}

files, err := ioutil.ReadDir(logDir)
files, err := os.ReadDir(logDir)
if err != nil {
fmt.Println("ReadDir failed ", err)

return
}
for _, file := range files {
fmt.Println(file.Name(), file.Size())
info, err := file.Info()
if err != nil {
fmt.Println("Info failed ", err)

return
}
fmt.Println(file.Name(), info.Size())
}

err = os.RemoveAll(logDir)
Expand Down
10 changes: 5 additions & 5 deletions internal/fileutil/fileutil.go → fileutil.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package fileutil
package rotatelogs

import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/lestrrat-go/strftime"
"github.com/pkg/errors"
"github.com/goravel/file-rotatelogs/v2/strftime"
)

// GenerateFn creates a file name based on the pattern, the current time, and the
Expand Down Expand Up @@ -46,12 +46,12 @@ func CreateFile(filename string) (*os.File, error) {
// ./foo/bar/baz/hello.log must make sure ./foo/bar/baz is existed
dirname := filepath.Dir(filename)
if err := os.MkdirAll(dirname, 0755); err != nil {
return nil, errors.Wrapf(err, "failed to create directory %s", dirname)
return nil, fmt.Errorf("failed to create directory %s: %w", dirname, err)
}
// if we got here, then we need to create a file
fh, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return nil, errors.Errorf("failed to open file %s: %s", filename, err)
return nil, fmt.Errorf("failed to open file %s: %w", filename, err)
}

return fh, nil
Expand Down
17 changes: 12 additions & 5 deletions internal/fileutil/fileutil_test.go → fileutil_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package fileutil_test
package rotatelogs

import (
"fmt"
"os"
"testing"
"time"

"github.com/jonboulle/clockwork"
"github.com/lestrrat-go/strftime"
"github.com/stretchr/testify/assert"

"github.com/goravel/file-rotatelogs/v2/strftime"
)

func TestGenerateFn(t *testing.T) {
Expand All @@ -22,8 +23,7 @@ func TestGenerateFn(t *testing.T) {
if !assert.NoError(t, err, `strftime.New should succeed`) {
return
}
clock := clockwork.NewFakeClockAt(xt)
fn := GenerateFn(pattern, clock, 24*time.Hour)
fn := GenerateFn(pattern, NewClock(xt), 24*time.Hour)
expected := fmt.Sprintf("/path/to/%04d/%02d/%02d",
xt.Year(),
xt.Month(),
Expand All @@ -35,3 +35,10 @@ func TestGenerateFn(t *testing.T) {
}
}
}

func TestCreateFileSuccess(t *testing.T) {
file, err := CreateFile("testfile.log")
assert.NoError(t, err)
assert.NoError(t, file.Close())
assert.NoError(t, os.Remove("testfile.log"))
}
15 changes: 7 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module github.com/goravel/file-rotatelogs/v2

go 1.12
go 1.20

require (
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jonboulle/clockwork v0.1.0
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc // indirect
github.com/lestrrat-go/strftime v0.0.0-20180821113735-8b31f9c59b0f
github.com/pkg/errors v0.8.1
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc
github.com/stretchr/testify v1.3.0
github.com/tebeka/strftime v0.1.3 // indirect
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
)
Loading

0 comments on commit a26a795

Please sign in to comment.