-
Notifications
You must be signed in to change notification settings - Fork 419
/
version.go
93 lines (80 loc) · 2.46 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cwversion
import (
"encoding/json"
"fmt"
"log"
"net/http"
"runtime"
"strings"
goversion "github.com/hashicorp/go-version"
"github.com/crowdsecurity/go-cs-lib/version"
)
var (
Codename string // = "SoumSoum"
System = runtime.GOOS // = "linux"
Constraint_parser = ">= 1.0, <= 2.0"
Constraint_scenario = ">= 1.0, < 3.0"
Constraint_api = "v1"
Constraint_acquis = ">= 1.0, < 2.0"
Libre2 = "WebAssembly"
)
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
}
func Show() {
log.Printf("version: %s-%s", version.Version, version.Tag)
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)
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)
}
func VersionStrip() string {
version := strings.Split(version.Version, "~")
version = strings.Split(version[0], "-")
return version[0]
}
func Satisfies(strvers string, constraint string) (bool, error) {
vers, err := goversion.NewVersion(strvers)
if err != nil {
return false, fmt.Errorf("failed to parse '%s' : %v", strvers, err)
}
constraints, err := goversion.NewConstraint(constraint)
if err != nil {
return false, fmt.Errorf("failed to parse constraint '%s'", constraint)
}
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
}