-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.go
74 lines (62 loc) · 1.51 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
package require
import (
"errors"
"os"
"path/filepath"
"github.com/paketo-buildpacks/nginx"
"github.com/paketo-buildpacks/packit/v2"
"github.com/paketo-buildpacks/packit/v2/scribe"
)
const (
indexFile = "index.html"
WebRootEnv = "BP_STATIC_WEBROOT"
)
type BuildPlanMetadata struct {
Version string `toml:"version,omitempty"`
VersionSource string `toml:"version-source,omitempty"`
Launch bool `toml:"launch"`
}
func Detect(logger scribe.Emitter) packit.DetectFunc {
return func(context packit.DetectContext) (packit.DetectResult, error) {
if _, err := WebRoot(context.WorkingDir); err != nil {
return packit.DetectResult{}, err
}
result := packit.DetectResult{
Plan: packit.BuildPlan{
Requires: []packit.BuildPlanRequirement{
{
Name: nginx.NGINX,
Metadata: BuildPlanMetadata{Launch: true},
},
},
},
}
return result, nil
}
}
func WebRoot(workingDir string) (string, error) {
// the webroot can be set using an env var
if v, ok := os.LookupEnv(WebRootEnv); ok {
return v, nil
}
paths := []string{"./", "./public"}
publicDir := ""
for _, path := range paths {
f, err := os.Stat(filepath.Join(workingDir, path, indexFile))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return "", packit.Fail.WithMessage("failed to stat: %w", err)
}
if f.IsDir() {
continue
}
publicDir = path
break
}
if len(publicDir) == 0 {
return "", packit.Fail.WithMessage("no index.html found")
}
return publicDir, nil
}