File: fs.go

package info (click to toggle)
golang-github-knadh-koanf 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: sh: 31; makefile: 14
file content (41 lines) | stat: -rw-r--r-- 849 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Package fs implements a koanf.Provider that reads raw bytes
// from given fs.FS to be used with a koanf.Parser to parse
// into conf maps.

//go:build go1.16
// +build go1.16

package fs

import (
	"errors"
	"io"
	"io/fs"
)

// FS implements an fs.FS provider.
type FS struct {
	fs   fs.FS
	path string
}

// Provider returns an fs.FS provider.
func Provider(fs fs.FS, filepath string) *FS {
	return &FS{fs: fs, path: filepath}
}

// ReadBytes reads the contents of given filepath from fs.FS and returns the bytes.
func (f *FS) ReadBytes() ([]byte, error) {
	fd, err := f.fs.Open(f.path)
	if err != nil {
		return nil, err
	}
	defer fd.Close()

	return io.ReadAll(fd)
}

// Read is not supported by the fs.FS provider.
func (f *FS) Read() (map[string]interface{}, error) {
	return nil, errors.New("fs.FS provider does not support this method")
}