-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfigtree.go
More file actions
93 lines (89 loc) · 2.27 KB
/
figtree.go
File metadata and controls
93 lines (89 loc) · 2.27 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
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
package figtree
import (
"flag"
"os"
"sync"
"sync/atomic"
)
// New will initialize the figTree package
// Usage:
//
// When defining:
// figs := figtree.New()
// figs.NewInt("workers", 10, "number of workers")
// figs.Parse()
// OR err := figs.Load()
// OR err := figs.ParseFile("path/to/file.json")
// THEN workers := *figs.Int("workers") // workers is a regular int
func New() Plant {
return With(Options{Tracking: false})
}
// Grow is a memetic alias of New
// Example:
//
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()
// figs := figtree.Grow()
// go func() {
// for {
// select {
// case <-ctx.Done():
// return
// case mutation, ok := <-figs.Mutations():
// if ok {
// log.Println(mutation)
// }
// }
// }
// }()
//
// // figs implements figtree.Plant interface
func Grow() Plant {
return With(Options{Tracking: true})
}
// With creates a new fig figTree with predefined Options
//
// Example:
//
// figs := With(Options{
// ConfigFilePath: "/opt/app/production.config.yaml",
// Pollinate: true,
// Tracking: true,
// Harvest: 369,
// })
// // define your figs.New<Mutagenesis>() here...
// figs.NewString("domain", "", "Domain name")
// figs.WithValidator("domain", figtree.AssureStringNotEmpty)
// err := figs.Load()
// domainInProductionConfigYamlFile := *figs.String("domain")
func With(opts Options) Plant {
angel := atomic.Bool{}
angel.Store(true)
chBuf := 1
if opts.Tracking && opts.Harvest > 0 {
chBuf = opts.Harvest
}
fig := &figTree{
ConfigFilePath: opts.ConfigFile,
ignoreEnv: opts.IgnoreEnvironment,
filterTests: opts.Germinate,
pollinate: opts.Pollinate,
tracking: opts.Tracking,
harvest: chBuf,
angel: &angel,
problems: make([]error, 0),
aliases: make(map[string]string),
figs: make(map[string]*figFruit),
values: &sync.Map{},
withered: make(map[string]witheredFig),
mu: sync.RWMutex{},
mutationsCh: make(chan Mutation, chBuf),
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
}
fig.flagSet.Usage = fig.Usage
angel.Store(false)
if opts.IgnoreEnvironment {
os.Clearenv()
}
return fig
}