-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
63 lines (52 loc) · 1.28 KB
/
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
package configuration_secret_files_test
import (
"fmt"
"github.com/BoRuDar/configuration/v4"
configuration_secret_files "github.com/MadsRC/configuration-secret-files"
"os"
"path/filepath"
)
const Payload = `Hello, World!`
var dir string
func SetupExample() (func(), error) {
var err error
dir, err = os.MkdirTemp("", "example")
if err != nil {
return nil, err
}
fpath := filepath.Join(dir, "secret")
err = os.WriteFile(fpath, []byte(Payload), 0644)
if err != nil {
return nil, err
}
return func() {
os.RemoveAll(dir)
}, nil
}
type Config struct {
SomethingNotSecret string
TheSecret string `secret_file:"secret"`
}
func Example() {
// IGNORE THIS PART OF THE EXAMPLE, WE NEED TO DO SOME SETUP
cleanup, err := SetupExample()
if err != nil {
fmt.Println(err)
return
}
defer cleanup()
// END OF THE SETUP - STOP IGNORING
cfg := &Config{}
configurator := configuration.New(cfg, configuration_secret_files.NewProvider(
// This is for the sake of the example. If we don't set the directory, the default one will be used.
configuration_secret_files.WithDirectory(filepath.Join(dir)),
))
err = configurator.InitValues()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", cfg)
// Output:
// &{SomethingNotSecret: TheSecret:Hello, World!}
}