2020-05-15 09:39:16 +00:00
|
|
|
package cwversion
|
|
|
|
|
|
|
|
import (
|
2020-09-01 12:32:45 +00:00
|
|
|
"encoding/json"
|
2020-05-15 09:39:16 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-09-01 12:32:45 +00:00
|
|
|
"net/http"
|
2022-03-14 10:29:34 +00:00
|
|
|
"runtime"
|
2021-03-11 10:18:09 +00:00
|
|
|
"strings"
|
2020-05-15 09:39:16 +00:00
|
|
|
|
|
|
|
version "github.com/hashicorp/go-version"
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Given a version number MAJOR.MINOR.PATCH, increment the:
|
|
|
|
|
|
|
|
MAJOR version when you make incompatible API changes,
|
|
|
|
MINOR version when you add functionality in a backwards compatible manner, and
|
|
|
|
PATCH version when you make backwards compatible bug fixes.
|
|
|
|
|
|
|
|
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var (
|
2022-03-14 10:29:34 +00:00
|
|
|
Version string // = "v0.0.0"
|
|
|
|
Codename string // = "SoumSoum"
|
|
|
|
BuildDate string // = "0000-00-00_00:00:00"
|
|
|
|
Tag string // = "dev"
|
|
|
|
GoVersion = runtime.Version()[2:] // = "1.13"
|
|
|
|
System = runtime.GOOS // = "linux"
|
2021-03-10 17:27:21 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func ShowStr() string {
|
|
|
|
ret := ""
|
|
|
|
ret += fmt.Sprintf("version: %s-%s\n", Version, Tag)
|
|
|
|
ret += fmt.Sprintf("Codename: %s\n", Codename)
|
|
|
|
ret += fmt.Sprintf("BuildDate: %s\n", BuildDate)
|
|
|
|
ret += fmt.Sprintf("GoVersion: %s\n", GoVersion)
|
2021-04-23 13:23:46 +00:00
|
|
|
ret += fmt.Sprintf("Platform: %s\n", System)
|
2020-11-30 09:37:17 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func Show() {
|
|
|
|
log.Printf("version: %s-%s", Version, Tag)
|
|
|
|
log.Printf("Codename: %s", Codename)
|
|
|
|
log.Printf("BuildDate: %s", BuildDate)
|
|
|
|
log.Printf("GoVersion: %s", GoVersion)
|
2022-02-04 12:02:45 +00:00
|
|
|
log.Printf("Platform: %s\n", System)
|
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 {
|
2021-04-23 13:23:46 +00:00
|
|
|
return fmt.Sprintf("%s-%s-%s", Version, System, Tag)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 10:18:09 +00:00
|
|
|
func VersionStrip() string {
|
2023-02-25 19:05:48 +00:00
|
|
|
version := strings.Split(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]
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func Statisfies(strvers string, constraint string) (bool, error) {
|
|
|
|
vers, err := version.NewVersion(strvers)
|
|
|
|
if err != nil {
|
2020-05-19 19:31:06 +00:00
|
|
|
return false, fmt.Errorf("failed to parse '%s' : %v", strvers, err)
|
2020-05-15 09:39:16 +00:00
|
|
|
}
|
|
|
|
constraints, err := version.NewConstraint(constraint)
|
|
|
|
if err != nil {
|
2020-05-19 19:31:06 +00:00
|
|
|
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
|
|
|
|
}
|
2020-09-01 12:32:45 +00:00
|
|
|
|
|
|
|
// Latest return latest crowdsec version based on github
|
|
|
|
func Latest() (string, error) {
|
|
|
|
latest := make(map[string]interface{})
|
|
|
|
|
2021-09-02 13:17:37 +00:00
|
|
|
resp, err := http.Get("https://version.crowdsec.net/latest")
|
2020-09-01 12:32:45 +00:00
|
|
|
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 {
|
2020-11-30 09:37:17 +00:00
|
|
|
return "", fmt.Errorf("unable to find latest release name from github api: %+v", latest)
|
2020-09-01 12:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return latest["name"].(string), nil
|
|
|
|
}
|