github.com/crowdsecurity/crowdsec@v1.6.1/pkg/cwversion/version.go (about) 1 package cwversion 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "log" 7 "net/http" 8 "runtime" 9 "strings" 10 11 goversion "github.com/hashicorp/go-version" 12 13 "github.com/crowdsecurity/go-cs-lib/version" 14 ) 15 16 var ( 17 Codename string // = "SoumSoum" 18 System = runtime.GOOS // = "linux" 19 Libre2 = "WebAssembly" 20 ) 21 22 const ( 23 Constraint_parser = ">= 1.0, <= 3.0" 24 Constraint_scenario = ">= 1.0, <= 3.0" 25 Constraint_api = "v1" 26 Constraint_acquis = ">= 1.0, < 2.0" 27 ) 28 29 func versionWithTag() string { 30 ret := version.Version 31 32 if !strings.HasSuffix(ret, version.Tag) { 33 ret += fmt.Sprintf("-%s", version.Tag) 34 } 35 36 return ret 37 } 38 39 func ShowStr() string { 40 ret := fmt.Sprintf("version: %s", versionWithTag()) 41 ret += fmt.Sprintf("Codename: %s\n", Codename) 42 ret += fmt.Sprintf("BuildDate: %s\n", version.BuildDate) 43 ret += fmt.Sprintf("GoVersion: %s\n", version.GoVersion) 44 ret += fmt.Sprintf("Platform: %s\n", System) 45 46 return ret 47 } 48 49 func Show() { 50 log.Printf("version: %s", versionWithTag()) 51 log.Printf("Codename: %s", Codename) 52 log.Printf("BuildDate: %s", version.BuildDate) 53 log.Printf("GoVersion: %s", version.GoVersion) 54 log.Printf("Platform: %s\n", System) 55 log.Printf("libre2: %s\n", Libre2) 56 log.Printf("Constraint_parser: %s", Constraint_parser) 57 log.Printf("Constraint_scenario: %s", Constraint_scenario) 58 log.Printf("Constraint_api: %s", Constraint_api) 59 log.Printf("Constraint_acquis: %s", Constraint_acquis) 60 } 61 62 func VersionStr() string { 63 return fmt.Sprintf("%s-%s-%s", version.Version, System, version.Tag) 64 } 65 66 func VersionStrip() string { 67 version := strings.Split(version.Version, "~") 68 version = strings.Split(version[0], "-") 69 70 return version[0] 71 } 72 73 func Satisfies(strvers string, constraint string) (bool, error) { 74 vers, err := goversion.NewVersion(strvers) 75 if err != nil { 76 return false, fmt.Errorf("failed to parse '%s' : %v", strvers, err) 77 } 78 79 constraints, err := goversion.NewConstraint(constraint) 80 if err != nil { 81 return false, fmt.Errorf("failed to parse constraint '%s'", constraint) 82 } 83 84 if !constraints.Check(vers) { 85 return false, nil 86 } 87 88 return true, nil 89 } 90 91 // Latest return latest crowdsec version based on github 92 func Latest() (string, error) { 93 latest := make(map[string]interface{}) 94 95 resp, err := http.Get("https://version.crowdsec.net/latest") 96 if err != nil { 97 return "", err 98 } 99 defer resp.Body.Close() 100 101 err = json.NewDecoder(resp.Body).Decode(&latest) 102 if err != nil { 103 return "", err 104 } 105 106 if _, ok := latest["name"]; !ok { 107 return "", fmt.Errorf("unable to find latest release name from github api: %+v", latest) 108 } 109 110 return latest["name"].(string), nil 111 }