generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.go
65 lines (57 loc) · 1.94 KB
/
init.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
package ferrite
import (
"fmt"
"github.com/dogmatiq/ferrite/internal/environment"
"github.com/dogmatiq/ferrite/internal/mode"
"github.com/dogmatiq/ferrite/internal/mode/export/dotenv"
"github.com/dogmatiq/ferrite/internal/mode/usage/markdown"
"github.com/dogmatiq/ferrite/internal/mode/validate"
"github.com/dogmatiq/ferrite/internal/variable"
)
// Init initializes Ferrite.
//
// Different modes can be selected by setting the `FERRITE_MODE` environment
// variable.
//
// "validate" mode: This is the default mode. If one or more environment
// variables are invalid, this mode renders a description of all declared
// environment variables and their associated values and validation failures to
// `STDERR`, then exits the process with a non-zero exit code.
//
// It also shows warnings if deprecated environment variables are used.
//
// "usage/markdown" mode: This mode renders Markdown documentation about the
// environment variables to `STDOUT`. The output is designed to be included in
// the application's `README.md` file or a similar file.
//
// "export/dotenv" mode: This mode renders environment variables to `STDOUT` in
// a format suitable for use as a `.env` file.
func Init(options ...InitOption) {
cfg := initConfig{
mode.DefaultConfig,
}
cfg.ModeConfig.Registries.Add(variable.DefaultRegistry)
for _, opt := range options {
opt.applyInitOption(&cfg)
}
switch m := environment.Get("FERRITE_MODE"); m {
case "validate", "":
validate.Run(cfg.ModeConfig)
case "usage/markdown":
markdown.Run(cfg.ModeConfig)
case "export/dotenv":
dotenv.Run(cfg.ModeConfig)
default:
fmt.Fprintf(cfg.ModeConfig.Err, "unrecognized FERRITE_MODE (%s)\n", m)
cfg.ModeConfig.Exit(1)
}
}
// An InitOption changes the behavior of the Init() function.
type InitOption interface {
applyInitOption(*initConfig)
}
// initConfig is the configuration for the Init() function, built from
// InitOption values.
type initConfig struct {
ModeConfig mode.Config
}