crowdsec/pkg/cwversion/version.go

94 lines
2.5 KiB
Go
Raw Normal View History

2020-05-15 09:39:16 +00:00
package cwversion
import (
"encoding/json"
2020-05-15 09:39:16 +00:00
"fmt"
"log"
"net/http"
"runtime"
2021-03-11 10:18:09 +00:00
"strings"
2020-05-15 09:39:16 +00:00
goversion "github.com/hashicorp/go-version"
"github.com/crowdsecurity/go-cs-lib/pkg/version"
2020-05-15 09:39:16 +00:00
)
var (
Codename string // = "SoumSoum"
System = runtime.GOOS // = "linux"
Constraint_parser = ">= 1.0, <= 2.0"
2020-05-27 14:31:08 +00:00
Constraint_scenario = ">= 1.0, < 3.0"
2020-05-15 09:39:16 +00:00
Constraint_api = "v1"
Constraint_acquis = ">= 1.0, < 2.0"
Libre2 = "WebAssembly"
2020-05-15 09:39:16 +00:00
)
func ShowStr() string {
ret := ""
ret += fmt.Sprintf("version: %s-%s\n", version.Version, version.Tag)
ret += fmt.Sprintf("Codename: %s\n", Codename)
ret += fmt.Sprintf("BuildDate: %s\n", version.BuildDate)
ret += fmt.Sprintf("GoVersion: %s\n", version.GoVersion)
ret += fmt.Sprintf("Platform: %s\n", System)
return ret
}
2020-05-15 09:39:16 +00:00
func Show() {
log.Printf("version: %s-%s", version.Version, version.Tag)
2020-05-15 09:39:16 +00:00
log.Printf("Codename: %s", Codename)
log.Printf("BuildDate: %s", version.BuildDate)
log.Printf("GoVersion: %s", version.GoVersion)
log.Printf("Platform: %s\n", System)
log.Printf("libre2: %s\n", Libre2)
2020-05-15 09:39:16 +00:00
log.Printf("Constraint_parser: %s", Constraint_parser)
log.Printf("Constraint_scenario: %s", Constraint_scenario)
log.Printf("Constraint_api: %s", Constraint_api)
log.Printf("Constraint_acquis: %s", Constraint_acquis)
}
func VersionStr() string {
return fmt.Sprintf("%s-%s-%s", version.Version, System, version.Tag)
2020-05-15 09:39:16 +00:00
}
2021-03-11 10:18:09 +00:00
func VersionStrip() string {
version := strings.Split(version.Version, "~")
2023-05-16 23:13:55 +00:00
version = strings.Split(version[0], "-")
2021-03-11 10:18:09 +00:00
return version[0]
}
func Satisfies(strvers string, constraint string) (bool, error) {
vers, err := goversion.NewVersion(strvers)
2020-05-15 09:39:16 +00:00
if err != nil {
return false, fmt.Errorf("failed to parse '%s' : %v", strvers, err)
2020-05-15 09:39:16 +00:00
}
constraints, err := goversion.NewConstraint(constraint)
2020-05-15 09:39:16 +00:00
if err != nil {
return false, fmt.Errorf("failed to parse constraint '%s'", constraint)
2020-05-15 09:39:16 +00:00
}
if !constraints.Check(vers) {
return false, nil
}
return true, nil
}
// Latest return latest crowdsec version based on github
func Latest() (string, error) {
latest := make(map[string]interface{})
resp, err := http.Get("https://version.crowdsec.net/latest")
if err != nil {
return "", err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&latest)
if err != nil {
return "", err
}
if _, ok := latest["name"]; !ok {
return "", fmt.Errorf("unable to find latest release name from github api: %+v", latest)
}
return latest["name"].(string), nil
}