version.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cwversion
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "strings"
  8. version "github.com/hashicorp/go-version"
  9. )
  10. /*
  11. Given a version number MAJOR.MINOR.PATCH, increment the:
  12. MAJOR version when you make incompatible API changes,
  13. MINOR version when you add functionality in a backwards compatible manner, and
  14. PATCH version when you make backwards compatible bug fixes.
  15. Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
  16. */
  17. var (
  18. Version string // = "v0.0.0"
  19. Codename string // = "SoumSoum"
  20. BuildDate string // = "I don't remember exactly"
  21. Tag string // = "dev"
  22. GoVersion string // = "1.13"
  23. System string // = "linux"
  24. Constraint_parser = ">= 1.0, <= 2.0"
  25. Constraint_scenario = ">= 1.0, < 3.0"
  26. Constraint_api = "v1"
  27. Constraint_acquis = ">= 1.0, < 2.0"
  28. )
  29. func ShowStr() string {
  30. ret := ""
  31. ret += fmt.Sprintf("version: %s-%s\n", Version, Tag)
  32. ret += fmt.Sprintf("Codename: %s\n", Codename)
  33. ret += fmt.Sprintf("BuildDate: %s\n", BuildDate)
  34. ret += fmt.Sprintf("GoVersion: %s\n", GoVersion)
  35. ret += fmt.Sprintf("Platform: %s\n", System)
  36. return ret
  37. }
  38. func Show() {
  39. log.Printf("version: %s-%s", Version, Tag)
  40. log.Printf("Codename: %s", Codename)
  41. log.Printf("BuildDate: %s", BuildDate)
  42. log.Printf("GoVersion: %s", GoVersion)
  43. log.Printf("Platform: %s\n", System)
  44. log.Printf("Constraint_parser: %s", Constraint_parser)
  45. log.Printf("Constraint_scenario: %s", Constraint_scenario)
  46. log.Printf("Constraint_api: %s", Constraint_api)
  47. log.Printf("Constraint_acquis: %s", Constraint_acquis)
  48. }
  49. func VersionStr() string {
  50. return fmt.Sprintf("%s-%s-%s", Version, System, Tag)
  51. }
  52. func VersionStrip() string {
  53. version := strings.Split(Version, "-")
  54. return version[0]
  55. }
  56. func Statisfies(strvers string, constraint string) (bool, error) {
  57. vers, err := version.NewVersion(strvers)
  58. if err != nil {
  59. return false, fmt.Errorf("failed to parse '%s' : %v", strvers, err)
  60. }
  61. constraints, err := version.NewConstraint(constraint)
  62. if err != nil {
  63. return false, fmt.Errorf("failed to parse constraint '%s'", constraint)
  64. }
  65. if !constraints.Check(vers) {
  66. return false, nil
  67. }
  68. return true, nil
  69. }
  70. // Latest return latest crowdsec version based on github
  71. func Latest() (string, error) {
  72. latest := make(map[string]interface{})
  73. resp, err := http.Get("https://version.crowdsec.net/latest")
  74. if err != nil {
  75. return "", err
  76. }
  77. defer resp.Body.Close()
  78. err = json.NewDecoder(resp.Body).Decode(&latest)
  79. if err != nil {
  80. return "", err
  81. }
  82. if _, ok := latest["name"]; !ok {
  83. return "", fmt.Errorf("unable to find latest release name from github api: %+v", latest)
  84. }
  85. return latest["name"].(string), nil
  86. }