-
Notifications
You must be signed in to change notification settings - Fork 0
/
composer.go
73 lines (59 loc) · 1.41 KB
/
composer.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
package main
import (
"encoding/json"
"errors"
semver "github.com/Masterminds/semver/v3"
"os"
"strings"
)
type PHPComposerConfig struct {
Require *RequireDeps `json:"require"`
Config *ComposerConfig `json:"config"`
}
type RequireDeps struct {
PHPCondition string `json:"php"`
}
type RuntimePlatform struct {
PHPCondition string `json:"php"`
}
type ComposerConfig struct {
Platform *RuntimePlatform `json:"platform"`
}
func LoadComposerJSON(filePath string) (*PHPComposerConfig, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
decode := json.NewDecoder(f)
config := new(PHPComposerConfig)
err = decode.Decode(config)
if err != nil {
return nil, err
}
return config, nil
}
func (this *PHPComposerConfig) GetPHPCondition() string {
if this.Config != nil && this.Config.Platform != nil {
return this.Config.Platform.PHPCondition
}
if this.Require != nil {
return this.Require.PHPCondition
}
return "0"
}
func (this *PHPComposerConfig) MatchVersion(versionList ...string) (string, error) {
formatedCondition := strings.Replace(this.GetPHPCondition(), `|`, `||`, 1)
//log.Debug(formatedCondition)
constraint, err := semver.NewConstraint(formatedCondition)
if err != nil {
return "", err
}
for _, v := range versionList {
ver := semver.MustParse(v)
if constraint.Check(ver) {
return v, nil
}
}
return "", errors.New(`no version match`)
}