Skip to content

Commit

Permalink
Remove file.WithLog to favor standard log.Printf (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Mar 17, 2023
1 parent 8373044 commit 157d826
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Removed
- Remove file.WithLog to favor standard log.Printf (#32).

## [v0.1.0] - 3/12/2023

Initial alpha release.
4 changes: 2 additions & 2 deletions provider/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package file
import (
"fmt"
"io/fs"
"log"
"os"
)

Expand All @@ -26,7 +27,6 @@ type File struct {
fs fs.FS
path string
unmarshal func([]byte, any) error
log func(...any)
ignoreNotExist bool
}

Expand All @@ -47,7 +47,7 @@ func (f File) Load() (map[string]any, error) {
}
if err != nil {
if f.ignoreNotExist && os.IsNotExist(err) {
f.log(fmt.Sprintf("Config file %s does not exist.", f.path))
log.Printf("Config file %s does not exist.", f.path)

return make(map[string]any), nil
}
Expand Down
14 changes: 6 additions & 8 deletions provider/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package file_test

import (
"bytes"
"context"
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -121,21 +123,17 @@ func TestFile_Load(t *testing.T) {
}

func TestFile_log(t *testing.T) {
t.Parallel()
buf := new(bytes.Buffer)
log.SetOutput(buf)
log.SetFlags(0)

var log []any
_, err := file.New(
"not_found.json",
file.IgnoreFileNotExit(),
file.WithLog(
func(a ...any) {
log = append(log, a...)
},
),
).Load()

require.NoError(t, err)
require.Equal(t, []any{"Config file not_found.json does not exist."}, log)
require.Equal(t, "Config file not_found.json does not exist.\n", buf.String())
}

func TestFile_Watch(t *testing.T) {
Expand Down
11 changes: 0 additions & 11 deletions provider/file/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package file
import (
"encoding/json"
"io/fs"
"log"
)

// WithFS provides the fs.FS that config file is loaded from.
Expand Down Expand Up @@ -34,15 +33,6 @@ func IgnoreFileNotExit() Option {
}
}

// WithLog provides the function that logs message.
//
// The default function [log.Print].
func WithLog(log func(...any)) Option {
return func(file *options) {
file.log = log
}
}

// Option configures the given File.
type Option func(file *options)

Expand All @@ -52,7 +42,6 @@ func apply(path string, opts []Option) options {
option := &options{
path: path,
unmarshal: json.Unmarshal,
log: log.Print,
}
for _, opt := range opts {
opt(option)
Expand Down
9 changes: 5 additions & 4 deletions provider/file/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package file
import (
"context"
"fmt"
"log"
"path/filepath"
"time"

Expand All @@ -25,7 +26,7 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {
}
defer func() {
if err := watcher.Close(); err != nil {
f.log(fmt.Sprintf("Error when closing watcher for %s: %v", f.path, err))
log.Printf("Error when closing watcher for %s: %v", f.path, err)
}
}()

Expand Down Expand Up @@ -72,12 +73,12 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {

switch {
case event.Has(fsnotify.Remove):
f.log(fmt.Sprintf("Config file %s has been removed.", f.path))
log.Printf("Config file %s has been removed.", f.path)
watchFunc(nil)
case event.Has(fsnotify.Create) || event.Has(fsnotify.Write):
values, err := f.Load()
if err != nil {
f.log(fmt.Sprintf("Error when reloading configuration from %s: %v", f.path, err))
log.Printf("Error when reloading configuration from %s: %v", f.path, err)

continue
}
Expand All @@ -89,7 +90,7 @@ func (f File) Watch(ctx context.Context, watchFunc func(map[string]any)) error {
return nil
}

f.log(fmt.Sprintf("Error when watching file %s: %v", f.path, err))
log.Printf("Error when watching file %s: %v", f.path, err)

case <-ctx.Done():
return nil
Expand Down
4 changes: 1 addition & 3 deletions provider/file/watch_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

package file

import "fmt"

func (f File) Watch(context.Context, func(map[string]any)) error {
f.log(fmt.Sprintf("File.Watch does not supported on %s.", runtime.GOOS))
log.Printf("File.Watch does not supported on %s.", runtime.GOOS)

return nil
}

0 comments on commit 157d826

Please sign in to comment.