-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvault_selector_example_test.go
99 lines (76 loc) · 2.61 KB
/
vault_selector_example_test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package goblin_test
import (
"fmt"
"os"
"github.com/aphistic/goblin"
)
func ExampleVaultSelector() {
// Use a FilesystemVault if a path is provided as an environment variable,
// but fall back to using a MemoryVault by default. This, for example,
// could be useful during development when you are iterating on embedded
// files and don't want to have to rebuild for every change.
const assetVaultPathKey = "ASSET_VAULT_PATH"
_ = os.Setenv(assetVaultPathKey, "/")
// Create a filesystem vault and a memory vault to use.
fsysAssetVault := goblin.NewFilesystemVault(os.Getenv(assetVaultPathKey))
memAssetVault := goblin.NewMemoryVault() // Loaded from an embedded vault
// Create a vault selector that will use the filesystem asset vault
// if the environment variable ASSET_VAULT_PATH has a non-empty vault,
// use the embedded asset vault if not.
assetVaultSelector := goblin.NewVaultSelector(
goblin.SelectEnvNotEmpty(assetVaultPathKey, fsysAssetVault),
goblin.SelectDefault(memAssetVault),
)
// Since ASSET_VAULT_PATH is not empty, we use the filesystem vault.
v, _ := assetVaultSelector.GetVault()
fmt.Printf("Vault: %s\n", v)
// Output:
// Vault: Filesystem Vault (/)
}
func ExampleSelectEnvBool() {
const envKey = "USE_SELECT_ENV_BOOL"
os.Setenv(envKey, "true")
memVault := goblin.NewMemoryVault()
fsVault := goblin.NewFilesystemVault("/")
vs := goblin.NewVaultSelector(
goblin.SelectEnvBool(envKey, memVault),
goblin.SelectDefault(fsVault),
)
fmt.Printf("%s\n", vs)
// Output: Vault Selector (Memory Vault)
}
func ExampleSelectEnvNotEmpty() {
const envKey = "USE_SELECT_ENV_NON_EMPTY"
os.Setenv(envKey, "any value here")
memVault := goblin.NewMemoryVault()
fsVault := goblin.NewFilesystemVault("/")
vs := goblin.NewVaultSelector(
goblin.SelectEnvNotEmpty(envKey, memVault),
goblin.SelectDefault(fsVault),
)
fmt.Printf("%s\n", vs)
// Output: Vault Selector (Memory Vault)
}
func ExampleSelectPath() {
const vaultPath = "/usr"
rootVault := goblin.NewFilesystemVault("/")
usrVault := goblin.NewFilesystemVault(vaultPath)
vs := goblin.NewVaultSelector(
goblin.SelectPath(vaultPath, usrVault),
goblin.SelectDefault(rootVault),
)
fmt.Printf("%s\n", vs)
// Output: Vault Selector (Filesystem Vault (/usr))
}
func ExampleSelectDefault() {
fsVault := goblin.NewFilesystemVault("/")
memVault := goblin.NewMemoryVault()
// The first vault will always be selected because it
// will always return a vault value.
vs := goblin.NewVaultSelector(
goblin.SelectDefault(fsVault),
goblin.SelectDefault(memVault),
)
fmt.Printf("%s\n", vs)
// Output: Vault Selector (Filesystem Vault (/))
}