-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature.go
75 lines (64 loc) · 1.58 KB
/
feature.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
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
// Use of this source code is governed by the license in the LICENSE file.
package featureflag
import (
"os"
"strconv"
"testing"
"github.com/synopkg/devbox/internal/debug"
"github.com/synopkg/devbox/internal/envir"
)
type feature struct {
name string
enabled bool
}
var features = map[string]*feature{}
// Prevent lint complaining about unused function
//
//nolint:unparam
func disable(name string) *feature {
if features[name] == nil {
features[name] = &feature{name: name}
}
features[name].enabled = false
return features[name]
}
// Prevent lint complaining about unused function
//
//nolint:unparam
func enable(name string) *feature {
if features[name] == nil {
features[name] = &feature{name: name}
}
features[name].enabled = true
return features[name]
}
var logMap = map[string]bool{}
func (f *feature) Enabled() bool {
if f == nil {
return false
}
if on, err := strconv.ParseBool(os.Getenv(envir.DevboxFeaturePrefix + f.name)); err == nil {
status := "enabled"
if !on {
status = "disabled"
}
if !logMap[f.name] {
debug.Log("Feature %q %s via environment variable.", f.name, status)
logMap[f.name] = true
}
return on
}
return f.enabled
}
func (f *feature) EnableForTest(t *testing.T) {
t.Setenv(envir.DevboxFeaturePrefix+f.name, "1")
}
// All returns a map of all known features flags and whether they're enabled.
func All() map[string]bool {
m := make(map[string]bool, len(features))
for name, feat := range features {
m[name] = feat.Enabled()
}
return m
}