Package feature implements a simple abstraction for feature flags with arbitrary values.
A flag is registered on a FlagSet.
Flags are created using a specific method based on the type of the value of the flag, named after the type.
Currently, the supported methods are
- FlagSet.Bool for boolean flags,
- FlagSet.Float for float flags,
- FlagSet.Int for int flags and
- FlagSet.String for string flags.
Each method will return a callback that takes a context.Context
and returns a value of the specific type.
Additionally, each method can take an arbitrary number of options for adding metadata to the flag.
For example:
package main
import (
"context"
"github.com/nussjustin/feature"
)
func main() {
var set feature.FlagSet
myFeature := set.Bool("my-feature", flag.WithDescription("enables the new feature"))
if myFeature(context.Background()) {
println("my-feature enabled") // never runs, see next section
}
}
By default, the values returned for each flag will be the zero value for the specific type.
A Registry can be used to dynamically generate / fetch values for each flag.
The package currently ships with a single implementation SimpleStrategy.
Once created, a registry can used by calling the FlagSet.SetRegistry method.
Example:
package main
import (
"context"
"github.com/nussjustin/feature"
)
func main() {
var set feature.FlagSet
set.SetStrategy(&feature.SimpleStrategy{
BoolFunc: func(ctx context.Context, name string) bool {
return name == "my-feature"
},
})
myFeature := set.Bool("my-feature", flag.WithDescription("enables the new feature"))
if myFeature(context.Background()) {
println("my-feature enabled")
}
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.