github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/config_feature_flags.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	"github.com/fatih/color"
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
    11  	"github.com/crowdsecurity/crowdsec/pkg/fflag"
    12  )
    13  
    14  func (cli *cliConfig) featureFlags(showRetired bool) error {
    15  	green := color.New(color.FgGreen).SprintFunc()
    16  	red := color.New(color.FgRed).SprintFunc()
    17  	yellow := color.New(color.FgYellow).SprintFunc()
    18  	magenta := color.New(color.FgMagenta).SprintFunc()
    19  
    20  	printFeature := func(feat fflag.Feature) {
    21  		nameDesc := feat.Name
    22  		if feat.Description != "" {
    23  			nameDesc += ": " + feat.Description
    24  		}
    25  
    26  		status := red("✗")
    27  		if feat.IsEnabled() {
    28  			status = green("✓")
    29  		}
    30  
    31  		fmt.Printf("%s %s", status, nameDesc)
    32  
    33  		if feat.State == fflag.DeprecatedState {
    34  			fmt.Printf("\n  %s %s", yellow("DEPRECATED"), feat.DeprecationMsg)
    35  		}
    36  
    37  		if feat.State == fflag.RetiredState {
    38  			fmt.Printf("\n  %s %s", magenta("RETIRED"), feat.DeprecationMsg)
    39  		}
    40  
    41  		fmt.Println()
    42  	}
    43  
    44  	feats := fflag.Crowdsec.GetAllFeatures()
    45  
    46  	enabled := []fflag.Feature{}
    47  	disabled := []fflag.Feature{}
    48  	retired := []fflag.Feature{}
    49  
    50  	for _, feat := range feats {
    51  		if feat.State == fflag.RetiredState {
    52  			retired = append(retired, feat)
    53  			continue
    54  		}
    55  
    56  		if feat.IsEnabled() {
    57  			enabled = append(enabled, feat)
    58  			continue
    59  		}
    60  
    61  		disabled = append(disabled, feat)
    62  	}
    63  
    64  	if len(enabled) > 0 {
    65  		fmt.Println(" --- Enabled features ---")
    66  		fmt.Println()
    67  
    68  		for _, feat := range enabled {
    69  			printFeature(feat)
    70  		}
    71  
    72  		fmt.Println()
    73  	}
    74  
    75  	if len(disabled) > 0 {
    76  		fmt.Println(" --- Disabled features ---")
    77  		fmt.Println()
    78  
    79  		for _, feat := range disabled {
    80  			printFeature(feat)
    81  		}
    82  
    83  		fmt.Println()
    84  	}
    85  
    86  	fmt.Println("To enable a feature you can: ")
    87  	fmt.Println("  - set the environment variable CROWDSEC_FEATURE_<uppercase_feature_name> to true")
    88  
    89  	featurePath, err := filepath.Abs(csconfig.GetFeatureFilePath(ConfigFilePath))
    90  	if err != nil {
    91  		// we already read the file, shouldn't happen
    92  		return err
    93  	}
    94  
    95  	fmt.Printf("  - add the line '- <feature_name>' to the file %s\n", featurePath)
    96  	fmt.Println()
    97  
    98  	if len(enabled) == 0 && len(disabled) == 0 {
    99  		fmt.Println("However, no feature flag is available in this release.")
   100  		fmt.Println()
   101  	}
   102  
   103  	if showRetired && len(retired) > 0 {
   104  		fmt.Println(" --- Retired features ---")
   105  		fmt.Println()
   106  
   107  		for _, feat := range retired {
   108  			printFeature(feat)
   109  		}
   110  
   111  		fmt.Println()
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  func (cli *cliConfig) newFeatureFlagsCmd() *cobra.Command {
   118  	var showRetired bool
   119  
   120  	cmd := &cobra.Command{
   121  		Use:               "feature-flags",
   122  		Short:             "Displays feature flag status",
   123  		Long:              `Displays the supported feature flags and their current status.`,
   124  		Args:              cobra.ExactArgs(0),
   125  		DisableAutoGenTag: true,
   126  		RunE: func(_ *cobra.Command, _ []string) error {
   127  			return cli.featureFlags(showRetired)
   128  		},
   129  	}
   130  
   131  	flags := cmd.Flags()
   132  	flags.BoolVar(&showRetired, "retired", false, "Show retired features")
   133  
   134  	return cmd
   135  }