-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
53 lines (45 loc) · 1.42 KB
/
example_test.go
File metadata and controls
53 lines (45 loc) · 1.42 KB
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
package basic_test
import (
"context"
"fmt"
"path/filepath"
"github.com/fastabc/fastconf"
"github.com/fastabc/fastconf/examples/internal/exutil"
)
type basicExampleConfig struct {
Server struct {
Addr string `yaml:"addr" json:"addr"`
} `yaml:"server" json:"server"`
Database struct {
Pool int `yaml:"pool" json:"pool"`
} `yaml:"database" json:"database"`
}
// Example_basic demonstrates loading a profile overlay from a config directory.
//
// See also: docs/cookbook/k8s.md — profile-aware overlay loading with
// fastconf.WithProfile under a typical Kubernetes layout.
func Example_basic() {
root, cleanup := exutil.TempDir("example-basic-")
defer cleanup()
confDir := filepath.Join(root, "conf.d")
exutil.WriteFile(filepath.Join(confDir, "base", "00-app.yaml"), "server:\n addr: \":8080\"\ndatabase:\n pool: 10\n")
exutil.WriteFile(filepath.Join(confDir, "overlays", "prod", "10-app.yaml"), "server:\n addr: \":8443\"\ndatabase:\n pool: 32\n")
restoreEnv := exutil.SetEnv("APP_PROFILE", "prod")
defer restoreEnv()
mgr, err := fastconf.New[basicExampleConfig](context.Background(),
fastconf.WithDir(confDir),
fastconf.WithProfile(fastconf.ProfileOptions{
EnvVar: "APP_PROFILE",
Default: "dev",
}),
)
if err != nil {
fmt.Println(err)
return
}
defer mgr.Close()
app := mgr.Get()
fmt.Printf("%s %d %d\n", app.Server.Addr, app.Database.Pool, len(mgr.Snapshot().Sources))
// Output:
// :8443 32 2
}