branch.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package require
  2. // Set the appropriate hub branch according to config settings and crowdsec version
  3. import (
  4. log "github.com/sirupsen/logrus"
  5. "golang.org/x/mod/semver"
  6. "github.com/crowdsecurity/crowdsec/pkg/cwversion"
  7. "github.com/crowdsecurity/crowdsec/pkg/csconfig"
  8. )
  9. func chooseBranch(cfg *csconfig.Config) string {
  10. // this was set from config.yaml or flag
  11. if cfg.Cscli.HubBranch != "" {
  12. log.Debugf("Hub override from config: branch '%s'", cfg.Cscli.HubBranch)
  13. return cfg.Cscli.HubBranch
  14. }
  15. latest, err := cwversion.Latest()
  16. if err != nil {
  17. log.Warningf("Unable to retrieve latest crowdsec version: %s, using hub branch 'master'", err)
  18. return "master"
  19. }
  20. csVersion := cwversion.VersionStrip()
  21. if csVersion == latest {
  22. log.Debugf("Latest crowdsec version (%s), using hub branch 'master'", csVersion)
  23. return "master"
  24. }
  25. // if current version is greater than the latest we are in pre-release
  26. if semver.Compare(csVersion, latest) == 1 {
  27. log.Debugf("Your current crowdsec version seems to be a pre-release (%s), using hub branch 'master'", csVersion)
  28. return "master"
  29. }
  30. if csVersion == "" {
  31. log.Warning("Crowdsec version is not set, using hub branch 'master'")
  32. return "master"
  33. }
  34. log.Warnf("A new CrowdSec release is available (%s). "+
  35. "Your version is '%s'. Please update it to use new parsers/scenarios/collections.",
  36. latest, csVersion)
  37. return csVersion
  38. }
  39. // HubBranch sets the branch (in cscli config) and returns its value
  40. // It can be "master", or the branch corresponding to the current crowdsec version, or the value overridden in config/flag
  41. func HubBranch(cfg *csconfig.Config) string {
  42. branch := chooseBranch(cfg)
  43. cfg.Cscli.HubBranch = branch
  44. return branch
  45. }
  46. func HubURLTemplate(cfg *csconfig.Config) string {
  47. return cfg.Cscli.HubURLTemplate
  48. }