forked from paketo-buildpacks/nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
71 lines (56 loc) · 1.39 KB
/
parser.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
package nginx
import (
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
type Parser struct{}
func NewParser() Parser {
return Parser{}
}
func (p Parser) ParseYml(workingDir string) (string, bool, error) {
bpYML, err := os.Open(filepath.Join(workingDir, BuildpackYMLSource))
if err != nil {
return "", false, err
}
var buildpackYML struct {
Config struct {
Version string `yaml:"version"`
} `yaml:"nginx"`
}
err = yaml.NewDecoder(bpYML).Decode(&buildpackYML)
if err != nil {
return "", false, err
}
return buildpackYML.Config.Version, true, nil
}
func (p Parser) ResolveVersion(cnbPath, version string) (string, error) {
bpTOML, err := os.Open(filepath.Join(cnbPath, "buildpack.toml"))
if err != nil {
return "", err
}
var buildpackTOML struct {
Metadata struct {
DefaultVersions map[string]string `toml:"default-versions"`
VersionLines struct {
Mainline string `toml:"mainline"`
Stable string `toml:"stable"`
} `toml:"version-lines"`
} `toml:"metadata"`
}
_, err = toml.NewDecoder(bpTOML).Decode(&buildpackTOML)
if err != nil {
return "", err
}
if version == "mainline" {
version = buildpackTOML.Metadata.VersionLines.Mainline
}
if version == "stable" {
version = buildpackTOML.Metadata.VersionLines.Stable
}
if version == "" {
version = buildpackTOML.Metadata.DefaultVersions[NGINX]
}
return version, nil
}