forked from frain-dev/convoy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored and implemented feature flags per feature (frain-dev#2105)
* refactored and feature flags per feature * refactored search feature flag * fixed tests * updated feature flags
- Loading branch information
Showing
13 changed files
with
365 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package ff | ||
|
||
import ( | ||
"github.com/frain-dev/convoy/config" | ||
fflag2 "github.com/frain-dev/convoy/internal/pkg/fflag" | ||
"github.com/frain-dev/convoy/pkg/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func AddFeatureFlagsCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "feature-flags", | ||
Short: "Print the list of feature flags", | ||
Annotations: map[string]string{ | ||
"CheckMigration": "true", | ||
"ShouldBootstrap": "false", | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, err := config.Get() | ||
if err != nil { | ||
log.WithError(err).Fatalf("Error fetching the config.") | ||
} | ||
f, err := fflag2.NewFFlag(&cfg) | ||
if err != nil { | ||
return err | ||
} | ||
return f.ListFeatures() | ||
}, | ||
PersistentPostRun: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,102 @@ | ||
package fflag | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/frain-dev/convoy/config" | ||
"os" | ||
"sort" | ||
"text/tabwriter" | ||
) | ||
|
||
var ErrFeatureNotEnabled = errors.New("this feature is not enabled") | ||
|
||
type ( | ||
FeatureFlagKey string | ||
) | ||
|
||
const ( | ||
Prometheus FeatureFlagKey = "prometheus" | ||
Prometheus FeatureFlagKey = "prometheus" | ||
FullTextSearch FeatureFlagKey = "full-text-search" | ||
) | ||
|
||
type ( | ||
FeatureFlagState bool | ||
) | ||
|
||
const ( | ||
enabled FeatureFlagState = true | ||
disabled FeatureFlagState = false | ||
) | ||
|
||
var features = map[FeatureFlagKey]config.FlagLevel{ | ||
Prometheus: config.ExperimentalFlagLevel, | ||
var DefaultFeaturesState = map[FeatureFlagKey]FeatureFlagState{ | ||
Prometheus: disabled, | ||
FullTextSearch: disabled, | ||
} | ||
|
||
type FFlag struct{} | ||
type FFlag struct { | ||
Features map[FeatureFlagKey]FeatureFlagState | ||
} | ||
|
||
func NewFFlag() (*FFlag, error) { | ||
return &FFlag{}, nil | ||
func NewFFlag(c *config.Configuration) (*FFlag, error) { | ||
f := &FFlag{ | ||
Features: clone(DefaultFeaturesState), | ||
} | ||
for _, flag := range c.EnableFeatureFlag { | ||
switch flag { | ||
case string(Prometheus): | ||
f.Features[Prometheus] = enabled | ||
case string(FullTextSearch): | ||
f.Features[FullTextSearch] = enabled | ||
} | ||
} | ||
return f, nil | ||
} | ||
|
||
func (c *FFlag) CanAccessFeature(key FeatureFlagKey, cfg *config.Configuration) bool { | ||
func clone(src map[FeatureFlagKey]FeatureFlagState) map[FeatureFlagKey]FeatureFlagState { | ||
dst := make(map[FeatureFlagKey]FeatureFlagState) | ||
for k, v := range src { | ||
dst[k] = v | ||
} | ||
return dst | ||
} | ||
|
||
func (c *FFlag) CanAccessFeature(key FeatureFlagKey) bool { | ||
// check for this feature in our feature map | ||
flagLevel, ok := features[key] | ||
state, ok := c.Features[key] | ||
if !ok { | ||
return false | ||
} | ||
|
||
return flagLevel <= cfg.FeatureFlag // if the feature level is less than or equal to the cfg level, we can access the feature | ||
return bool(state) | ||
} | ||
|
||
func (c *FFlag) ListFeatures() error { | ||
keys := make([]string, 0, len(c.Features)) | ||
|
||
for k := range c.Features { | ||
keys = append(keys, string(k)) | ||
} | ||
sort.Strings(keys) | ||
|
||
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0) | ||
_, err := fmt.Fprintln(w, "Features\tState") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, k := range keys { | ||
stateBool := c.Features[FeatureFlagKey(k)] | ||
state := "disabled" | ||
if stateBool { | ||
state = "enabled" | ||
} | ||
|
||
_, err := fmt.Fprintf(w, "%s\t%s\n", k, state) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return w.Flush() | ||
} |
Oops, something went wrong.