-
Notifications
You must be signed in to change notification settings - Fork 9
/
fs.go
68 lines (58 loc) · 1.5 KB
/
fs.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.
// Package fs loads configuration from file system.
//
// FS loads a file with the given path from the file system and returns
// a nested map[string]any that is parsed with the given unmarshal function.
//
// The unmarshal function must be able to unmarshal the file content into a map[string]any.
// For example, with the default json.Unmarshal, the file is parsed as JSON.
package fs
import (
"encoding/json"
"fmt"
"io/fs"
"os"
)
// FS is a Provider that loads configuration from file system.
//
// To create a new FS, call [New].
type FS struct {
fs fs.FS
path string
unmarshal func([]byte, any) error
}
// New creates a FS with the given fs.FS, path and Option(s).
func New(fs fs.FS, path string, opts ...Option) FS {
option := &options{
fs: fs,
path: path,
}
for _, opt := range opts {
opt(option)
}
if option.unmarshal == nil {
option.unmarshal = json.Unmarshal
}
return FS(*option)
}
func (f FS) Load() (map[string]any, error) {
ffs := f.fs
if ffs == nil {
// Ignore error: It uses whatever returned.
path, _ := os.Getwd()
ffs = os.DirFS(path)
}
bytes, err := fs.ReadFile(ffs, f.path)
if err != nil {
return nil, fmt.Errorf("read file: %w", err)
}
var out map[string]any
if err := f.unmarshal(bytes, &out); err != nil {
return nil, fmt.Errorf("unmarshal: %w", err)
}
return out, nil
}
func (f FS) String() string {
return "fs:///" + f.path
}