forked from paketo-buildpacks/go-mod-vendor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
47 lines (41 loc) · 1.13 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
package gomodvendor
import (
"errors"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit"
)
//go:generate faux --interface VersionParser --output fakes/version_parser.go
type VersionParser interface {
ParseVersion(path string) (version string, err error)
}
type BuildPlanMetadata struct {
VersionSource string `toml:"version-source"`
Version string `toml:"version"`
Build bool `toml:"build"`
}
func Detect(goModParser VersionParser) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
version, err := goModParser.ParseVersion(filepath.Join(context.WorkingDir, GoModLocation))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return packit.DetectResult{}, packit.Fail.WithMessage("go.mod file is not present")
}
return packit.DetectResult{}, err
}
return packit.DetectResult{
Plan: packit.BuildPlan{
Requires: []packit.BuildPlanRequirement{
{
Name: GoLayerName,
Metadata: BuildPlanMetadata{
VersionSource: GoModLocation,
Build: true,
Version: version,
},
},
},
},
}, nil
}
}