For cscli: it should provide a terse output, not nag users with configuration details. Although it's usually important that cscli and crowdsec have the same enabled features, having it list them every time the command is invoked can be too much. For crowdsec: when features are set from the environment, it's too early to log where we should. So we can use log.Debug at activation time, and list them again once logging is configured. - wrap some functions in csconfig for convenience and DRY - for each enabled feature, log.Debug - log all enabled features once as Info (crowdsec) or Debug (cscli) - file does not exist -> log.Trace
45 lines
1,014 B
Go
45 lines
1,014 B
Go
package csconfig
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
|
)
|
|
|
|
|
|
// LoadFeatureFlagsEnv parses the environment variables to enable feature flags.
|
|
func LoadFeatureFlagsEnv(logger *log.Logger) error {
|
|
if err := fflag.Crowdsec.SetFromEnv(logger); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
|
|
// LoadFeatureFlags parses {ConfigDir}/feature.yaml to enable feature flags.
|
|
func LoadFeatureFlagsFile(cConfig *Config, logger *log.Logger) error {
|
|
featurePath := filepath.Join(cConfig.ConfigPaths.ConfigDir, "feature.yaml")
|
|
|
|
if err := fflag.Crowdsec.SetFromYamlFile(featurePath, logger); err != nil {
|
|
return fmt.Errorf("file %s: %s", featurePath, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
|
|
// ListFeatureFlags returns a list of the enabled feature flags.
|
|
func ListFeatureFlags() string {
|
|
enabledFeatures := fflag.Crowdsec.GetEnabledFeatures()
|
|
|
|
msg := "<none>"
|
|
if len(enabledFeatures) > 0 {
|
|
msg = strings.Join(enabledFeatures, ", ")
|
|
}
|
|
|
|
return msg
|
|
}
|