This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
79 lines (68 loc) · 2 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/buildpack/libbuildpack/buildplan"
"github.com/cloudfoundry/dep-cnb/dep"
"github.com/cloudfoundry/libcfbuildpack/helper"
"gopkg.in/yaml.v2"
"github.com/cloudfoundry/libcfbuildpack/detect"
"github.com/pkg/errors"
)
const ErrorMsg = "no Gopkg.toml found at root level"
func main() {
context, err := detect.DefaultDetect()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed to create a default detection context: %s", err)
os.Exit(100)
}
code, err := runDetect(context)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "failed detection: %s", err)
}
os.Exit(code)
}
func runDetect(context detect.Detect) (int, error) {
goPkgFile := filepath.Join(context.Application.Root, "Gopkg.toml")
if exists, err := helper.FileExists(goPkgFile); err != nil {
return detect.FailStatusCode, errors.Wrap(err, fmt.Sprintf("error checking filepath: %s", goPkgFile))
} else if !exists {
return detect.FailStatusCode, fmt.Errorf(ErrorMsg)
}
bpYmlFile := filepath.Join(context.Application.Root, "buildpack.yml")
if exists, err := helper.FileExists(bpYmlFile); err != nil {
return detect.FailStatusCode, errors.Wrap(err, fmt.Sprintf("error checking filepath: %s", bpYmlFile))
} else if exists {
importPath, err := parseImportPath(bpYmlFile)
if err != nil {
return detect.FailStatusCode, errors.Wrap(err, "error reading buildpack.yml")
}
if importPath == "" {
return context.Fail(), nil
}
return context.Pass(buildplan.BuildPlan{
dep.Dependency: buildplan.Dependency{
Metadata: buildplan.Metadata{
"build": true,
"import-path": importPath,
},
},
})
}
return context.Fail(), nil
}
func parseImportPath(bpYmlFilePath string) (string, error) {
contents, err := ioutil.ReadFile(bpYmlFilePath)
if err != nil {
return "", err
}
bpYML := struct {
ImportPath string `yaml:"import-path"`
}{}
if err := yaml.Unmarshal(contents, &bpYML); err != nil {
return "", err
}
return bpYML.ImportPath, nil
}