config.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "strings"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/spf13/cobra"
  11. "gopkg.in/yaml.v2"
  12. )
  13. /*CliCfg is the cli configuration structure, might be unexported*/
  14. type cliConfig struct {
  15. configured bool
  16. configFolder string `yaml:"cliconfig,omitempty"` /*overload ~/.cscli/*/
  17. output string /*output is human, json*/
  18. hubFolder string
  19. InstallFolder string `yaml:"installdir"` /*/etc/crowdsec/*/
  20. BackendPluginFolder string `yaml:"backend"`
  21. dbPath string
  22. }
  23. func interactiveCfg() error {
  24. var err error
  25. reader := bufio.NewReader(os.Stdin)
  26. fmt.Print("crowdsec installation directory (default: /etc/crowdsec/config/): ")
  27. config.InstallFolder, err = reader.ReadString('\n')
  28. config.InstallFolder = strings.Replace(config.InstallFolder, "\n", "", -1) //CRLF to LF (windows)
  29. if config.InstallFolder == "" {
  30. config.InstallFolder = "/etc/crowdsec/config/"
  31. }
  32. if err != nil {
  33. log.Fatalf("failed to read input : %v", err.Error())
  34. }
  35. fmt.Print("crowdsec backend plugin directory (default: /etc/crowdsec/plugin/backend): ")
  36. config.BackendPluginFolder, err = reader.ReadString('\n')
  37. config.BackendPluginFolder = strings.Replace(config.BackendPluginFolder, "\n", "", -1) //CRLF to LF (windows)
  38. if config.BackendPluginFolder == "" {
  39. config.BackendPluginFolder = "/etc/crowdsec/plugin/backend"
  40. }
  41. if err != nil {
  42. log.Fatalf("failed to read input : %v", err.Error())
  43. }
  44. if err := writeCfg(); err != nil {
  45. log.Fatalf("failed writting configuration file : %s", err)
  46. }
  47. return nil
  48. }
  49. func writeCfg() error {
  50. if config.configFolder == "" {
  51. return fmt.Errorf("config dir is unset")
  52. }
  53. config.hubFolder = config.configFolder + "/hub/"
  54. if _, err := os.Stat(config.hubFolder); os.IsNotExist(err) {
  55. log.Warningf("creating skeleton!")
  56. if err := os.MkdirAll(config.hubFolder, os.ModePerm); err != nil {
  57. return fmt.Errorf("failed to create missing directory : '%s'", config.hubFolder)
  58. }
  59. }
  60. out := path.Join(config.configFolder, "/config")
  61. configYaml, err := yaml.Marshal(&config)
  62. if err != nil {
  63. return fmt.Errorf("failed marshaling config: %s", err)
  64. }
  65. err = ioutil.WriteFile(out, configYaml, 0644)
  66. if err != nil {
  67. return fmt.Errorf("failed to write to %s : %s", out, err)
  68. }
  69. log.Infof("wrote config to %s ", out)
  70. return nil
  71. }
  72. func NewConfigCmd() *cobra.Command {
  73. var cmdConfig = &cobra.Command{
  74. Use: "config [command] <value>",
  75. Short: "Allows to view/edit cscli config",
  76. Long: `Allow to configure sqlite path and installation directory.
  77. If no commands are specified, config is in interactive mode.`,
  78. Example: ` - cscli config show
  79. - cscli config prompt`,
  80. Args: cobra.ExactArgs(1),
  81. }
  82. var cmdConfigShow = &cobra.Command{
  83. Use: "show",
  84. Short: "Displays current config",
  85. Long: `Displays the current cli configuration.`,
  86. Args: cobra.ExactArgs(0),
  87. Run: func(cmd *cobra.Command, args []string) {
  88. if config.output == "json" {
  89. log.WithFields(log.Fields{
  90. "installdir": config.InstallFolder,
  91. "cliconfig": path.Join(config.configFolder, "/config"),
  92. }).Warning("Current config")
  93. } else {
  94. x, err := yaml.Marshal(config)
  95. if err != nil {
  96. log.Fatalf("failed to marshal current configuration : %v", err)
  97. }
  98. fmt.Printf("%s", x)
  99. fmt.Printf("#cliconfig: %s", path.Join(config.configFolder, "/config"))
  100. }
  101. },
  102. }
  103. cmdConfig.AddCommand(cmdConfigShow)
  104. var cmdConfigInterctive = &cobra.Command{
  105. Use: "prompt",
  106. Short: "Prompt for configuration values in an interactive fashion",
  107. Long: `Start interactive configuration of cli. It will successively ask for install dir, db path.`,
  108. Args: cobra.ExactArgs(0),
  109. Run: func(cmd *cobra.Command, args []string) {
  110. err := interactiveCfg()
  111. if err != nil {
  112. log.Fatalf("Failed to run interactive config : %s", err)
  113. }
  114. log.Warningf("Configured, please run update.")
  115. },
  116. }
  117. cmdConfig.AddCommand(cmdConfigInterctive)
  118. var cmdConfigInstalldir = &cobra.Command{
  119. Use: "installdir [value]",
  120. Short: `Configure installation directory`,
  121. Long: `Configure the installation directory of crowdsec, such as /etc/crowdsec/config/`,
  122. Args: cobra.ExactArgs(1),
  123. Run: func(cmd *cobra.Command, args []string) {
  124. config.InstallFolder = args[0]
  125. if err := writeCfg(); err != nil {
  126. log.Fatalf("failed writting configuration: %s", err)
  127. }
  128. },
  129. }
  130. cmdConfig.AddCommand(cmdConfigInstalldir)
  131. var cmdConfigBackendFolder = &cobra.Command{
  132. Use: "backend [value]",
  133. Short: `Configure installation directory`,
  134. Long: `Configure the backend plugin directory of crowdsec, such as /etc/crowdsec/plugins/backend`,
  135. Args: cobra.ExactArgs(1),
  136. Run: func(cmd *cobra.Command, args []string) {
  137. config.BackendPluginFolder = args[0]
  138. if err := writeCfg(); err != nil {
  139. log.Fatalf("failed writting configuration: %s", err)
  140. }
  141. },
  142. }
  143. cmdConfig.AddCommand(cmdConfigBackendFolder)
  144. return cmdConfig
  145. }