Skip to content

Simple feature flag package for Go

License

Notifications You must be signed in to change notification settings

nussjustin/feature

Repository files navigation

feature Go Reference Lint Test

Package feature implements a simple abstraction for feature flags with arbitrary values.

Examples

Registering a flag

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

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
	}
}

Configuring a registry

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")
	}
}

Contributing

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.

License

MIT

About

Simple feature flag package for Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages