76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package feature
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
// FeatureFlag represents a feature flag configuration
|
|
type FeatureFlag struct {
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
// featureFlags stores the feature flags
|
|
var (
|
|
featureFlags = make(map[string]FeatureFlag)
|
|
mu sync.RWMutex
|
|
)
|
|
|
|
// Feature flags
|
|
const (
|
|
EnableDatabase = "enable_database"
|
|
)
|
|
|
|
// Init initializes feature flags
|
|
func Init() error {
|
|
// Check if config file exists
|
|
configFile := ".feature-flags.json"
|
|
config, err := os.ReadFile(configFile)
|
|
if err != nil {
|
|
// File doesn't exist, create with default config
|
|
config = []byte(`{
|
|
"enable_database": {
|
|
"enabled": false
|
|
}
|
|
}`)
|
|
if err := os.WriteFile(configFile, config, 0644); err != nil {
|
|
return fmt.Errorf("failed to write default feature flags config: %w", err)
|
|
}
|
|
}
|
|
|
|
// Parse feature flags from JSON
|
|
var flags map[string]FeatureFlag
|
|
if err := json.Unmarshal(config, &flags); err != nil {
|
|
return fmt.Errorf("failed to parse feature flags: %w", err)
|
|
}
|
|
|
|
// Store feature flags
|
|
mu.Lock()
|
|
featureFlags = flags
|
|
mu.Unlock()
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsEnabled checks if a feature flag is enabled
|
|
func IsEnabled(flagKey string) bool {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
|
|
flag, exists := featureFlags[flagKey]
|
|
if !exists {
|
|
return false
|
|
}
|
|
return flag.Enabled
|
|
}
|
|
|
|
// Close cleans up feature flag resources
|
|
func Close() error {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
featureFlags = make(map[string]FeatureFlag)
|
|
return nil
|
|
}
|