-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdetect.go
111 lines (96 loc) · 2.89 KB
/
detect.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package nginx
import (
"fmt"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/fs"
)
//go:generate faux --interface VersionParser --output fakes/version_parser.go
type VersionParser interface {
ResolveVersion(cnbPath, version string) (resultVersion string, err error)
ParseYml(workDir string) (ymlVersion string, exists bool, err error)
}
type BuildPlanMetadata struct {
Version string `toml:"version,omitempty"`
VersionSource string `toml:"version-source,omitempty"`
Launch bool `toml:"launch"`
}
func Detect(config Configuration, versionParser VersionParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
plan := packit.DetectResult{
Plan: packit.BuildPlan{
Provides: []packit.BuildPlanProvision{
{Name: NGINX},
},
},
}
if !filepath.IsAbs(config.NGINXConfLocation) {
config.NGINXConfLocation = filepath.Join(context.WorkingDir, config.NGINXConfLocation)
}
confExists, err := fs.Exists(config.NGINXConfLocation)
if err != nil {
return packit.DetectResult{}, fmt.Errorf("failed to stat nginx.conf: %w", err)
}
if !confExists && config.WebServer != "nginx" {
return plan, nil
}
var requirements []packit.BuildPlanRequirement
var version string
if config.NGINXVersion != "" {
version, err = versionParser.ResolveVersion(context.CNBPath, config.NGINXVersion)
if err != nil {
return packit.DetectResult{}, err
}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: NGINX,
Metadata: BuildPlanMetadata{
Version: version,
VersionSource: "BP_NGINX_VERSION",
Launch: true,
},
})
}
ymlVersion, ymlExists, err := versionParser.ParseYml(context.WorkingDir)
if ymlExists {
version, err = versionParser.ResolveVersion(context.CNBPath, ymlVersion)
if err != nil {
return packit.DetectResult{}, err
}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: NGINX,
Metadata: BuildPlanMetadata{
Version: version,
VersionSource: "buildpack.yml",
Launch: true,
},
})
}
if config.NGINXVersion == "" && !ymlExists {
version, err = versionParser.ResolveVersion(context.CNBPath, "")
if err != nil {
return packit.DetectResult{}, err
}
requirements = append(requirements, packit.BuildPlanRequirement{
Name: NGINX,
Metadata: BuildPlanMetadata{
Version: version,
VersionSource: "buildpack.toml",
Launch: true,
},
})
}
if err != nil {
return packit.DetectResult{}, fmt.Errorf("parsing version failed: %w", err)
}
if config.LiveReloadEnabled {
requirements = append(requirements, packit.BuildPlanRequirement{
Name: "watchexec",
Metadata: map[string]interface{}{
"launch": true,
},
})
}
plan.Plan.Requires = requirements
return plan, nil
}
}