-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_loader.go
75 lines (61 loc) · 1.82 KB
/
config_loader.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
69
70
71
72
73
74
75
package fdk
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"gopkg.in/yaml.v3"
)
var (
// ErrCfgNotFound defines the inability to find a config at the expected location.
ErrCfgNotFound = errors.New("no config provided")
)
// ConfigLoader defines the behavior for loading config.
type ConfigLoader interface {
LoadConfig(ctx context.Context) ([]byte, error)
}
// RegisterConfigLoader will register a config loader at the specified type. Similar to registering
// a database with the database/sql, you're able to provide a config for use at runtime. During Run,
// the config loader defined by the env var, CS_CONFIG_LOADER_TYPE, is used. If one is not provided,
// then the fs config loader will be used.
func RegisterConfigLoader(loaderType string, cr ConfigLoader) {
if _, ok := configReaders[loaderType]; ok {
panic(fmt.Sprintf("config loader type already exists: %q", loaderType))
}
configReaders[loaderType] = cr
}
func loadConfigBytes(ctx context.Context) ([]byte, error) {
crt := os.Getenv("CS_CONFIG_LOADER_TYPE")
if crt == "" {
crt = "fs"
}
loader := configReaders[crt]
if loader == nil {
panic(fmt.Sprintf("unmatched config loader type provided: %q", crt))
}
return loader.LoadConfig(ctx)
}
var configReaders = map[string]ConfigLoader{
"fs": new(localCfgLoader),
}
type localCfgLoader struct{}
func (*localCfgLoader) LoadConfig(ctx context.Context) ([]byte, error) {
file := os.Getenv("CS_FN_CONFIG_PATH")
b, err := os.ReadFile(file)
if os.IsNotExist(err) {
return nil, ErrCfgNotFound
}
if err != nil {
return nil, err
}
if ext := filepath.Ext(file); ext == ".yaml" || ext == ".yml" {
var out map[string]any
if err := yaml.Unmarshal(b, &out); err != nil {
return nil, fmt.Errorf("failed to read yaml config: %w", err)
}
return json.Marshal(out)
}
return b, nil
}