config_feature_flags.go 3.0 KB

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