features: add basic feature flag implementation

This commit is contained in:
Michael Eischer
2024-01-28 16:15:32 +01:00
parent 0589da60b3
commit 5974a79497
4 changed files with 235 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
package feature
import (
"fmt"
"testing"
)
// TestSetFlag temporarily sets a feature flag to the given value until the
// returned function is called.
//
// Usage
// ```
// defer TestSetFlag(t, features.Flags, features.ExampleFlag, true)()
// ```
func TestSetFlag(t *testing.T, f *FlagSet, flag FlagName, value bool) func() {
current := f.Enabled(flag)
if err := f.Apply(fmt.Sprintf("%s=%v", flag, value)); err != nil {
// not reachable
panic(err)
}
return func() {
if err := f.Apply(fmt.Sprintf("%s=%v", flag, current)); err != nil {
// not reachable
panic(err)
}
}
}