branch.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package cwhub
  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. )
  8. // chooseHubBranch returns the branch name to use for the hub
  9. // It can be "master" or the branch corresponding to the current crowdsec version
  10. func chooseHubBranch() string {
  11. latest, err := cwversion.Latest()
  12. if err != nil {
  13. log.Warningf("Unable to retrieve latest crowdsec version: %s, defaulting to master", err)
  14. return "master"
  15. }
  16. csVersion := cwversion.VersionStrip()
  17. if csVersion == latest {
  18. log.Debugf("current version is equal to latest (%s)", csVersion)
  19. return "master"
  20. }
  21. // if current version is greater than the latest we are in pre-release
  22. if semver.Compare(csVersion, latest) == 1 {
  23. log.Debugf("Your current crowdsec version seems to be a pre-release (%s)", csVersion)
  24. return "master"
  25. }
  26. if csVersion == "" {
  27. log.Warning("Crowdsec version is not set, using master branch for the hub")
  28. return "master"
  29. }
  30. log.Warnf("Crowdsec is not the latest version. "+
  31. "Current version is '%s' and the latest stable version is '%s'. Please update it!",
  32. csVersion, latest)
  33. log.Warnf("As a result, you will not be able to use parsers/scenarios/collections "+
  34. "added to Crowdsec Hub after CrowdSec %s", latest)
  35. return csVersion
  36. }
  37. // SetHubBranch sets the package variable that points to the hub branch.
  38. func SetHubBranch() {
  39. // a branch is already set, or specified from the flags
  40. if HubBranch != "" {
  41. return
  42. }
  43. // use the branch corresponding to the crowdsec version
  44. HubBranch = chooseHubBranch()
  45. log.Debugf("Using branch '%s' for the hub", HubBranch)
  46. }