| 1 |
// Package fs implements a koanf.Provider that reads raw bytes |
| 2 |
// from given fs.FS to be used with a koanf.Parser to parse |
| 3 |
// into conf maps. |
| 4 |
|
| 5 |
//go:build go1.16 |
| 6 |
// +build go1.16 |
| 7 |
|
| 8 |
package fs |
| 9 |
|
| 10 |
import ( |
| 11 |
"errors" |
| 12 |
"io" |
| 13 |
"io/fs" |
| 14 |
) |
| 15 |
|
| 16 |
// FS implements an fs.FS provider. |
| 17 |
type FS struct { |
| 18 |
fs fs.FS |
| 19 |
path string |
| 20 |
} |
| 21 |
|
| 22 |
// Provider returns an fs.FS provider. |
| 23 |
func Provider(fs fs.FS, filepath string) *FS { |
| 24 |
return &FS{fs: fs, path: filepath} |
| 25 |
} |
| 26 |
|
| 27 |
// ReadBytes reads the contents of given filepath from fs.FS and returns the bytes. |
| 28 |
func (f *FS) ReadBytes() ([]byte, error) { |
| 29 |
fd, err := f.fs.Open(f.path) |
| 30 |
if err != nil { |
| 31 |
return nil, err |
| 32 |
} |
| 33 |
defer fd.Close() |
| 34 |
|
| 35 |
return io.ReadAll(fd) |
| 36 |
} |
| 37 |
|
| 38 |
// Read is not supported by the fs.FS provider. |
| 39 |
func (f *FS) Read() (map[string]any, error) { |
| 40 |
return nil, errors.New("fs.FS provider does not support this method") |
| 41 |
} |