-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclassic.go
48 lines (37 loc) · 1.11 KB
/
classic.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
package uconfig
import (
"errors"
"os"
"github.com/omeid/uconfig/plugins"
"github.com/omeid/uconfig/plugins/defaults"
"github.com/omeid/uconfig/plugins/env"
"github.com/omeid/uconfig/plugins/file"
"github.com/omeid/uconfig/plugins/flag"
)
// Files represents a set of file paths and the appropriate unmarshaller function.
type Files = file.Files
// Classic creates a uconfig manager with defaults,environment variables,
// and flags (in that order) and optionally file loaders based on the provided
// Files map and parses them right away.
func Classic(conf interface{}, files Files, userPlugins ...plugins.Plugin) (Config, error) {
fps := files.Plugins()
ps := make([]plugins.Plugin, 0, len(fps)+3+len(userPlugins))
// first defaults
ps = append(ps, defaults.New())
// then files
ps = append(ps, fps...)
// followed by env and flags
ps = append(ps, env.New(), flag.Standard())
// then any user pugins, often just _secret_.
ps = append(ps, userPlugins...)
c, err := New(conf, ps...)
if err != nil {
return c, err
}
err = c.Parse()
if errors.Is(err, ErrUsage) {
c.Usage()
os.Exit(0)
}
return c, err
}