-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathhooks_app.go
More file actions
62 lines (50 loc) · 1.23 KB
/
hooks_app.go
File metadata and controls
62 lines (50 loc) · 1.23 KB
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
package hooks
import (
"os"
"os/exec"
"path/filepath"
"regexp"
"github.com/cloudfoundry/libbuildpack"
)
type AppHook struct {
libbuildpack.DefaultHook
}
func init() {
libbuildpack.AddHook(AppHook{})
}
func (h AppHook) BeforeCompile(compiler *libbuildpack.Stager) error {
return runHook("pre_compile", compiler)
}
func (h AppHook) AfterCompile(compiler *libbuildpack.Stager) error {
return runHook("post_compile", compiler)
}
func runHook(scriptName string, compiler *libbuildpack.Stager) error {
path := filepath.Join(compiler.BuildDir(), "bin", scriptName)
if exists, err := libbuildpack.FileExists(path); err != nil {
return err
} else if exists {
compiler.Logger().BeginStep("Running %s hook", scriptName)
if err := os.Chmod(path, 0755); err != nil {
return err
}
fileContents, err := os.ReadFile(path)
if err != nil {
return err
}
shebangRegex := regexp.MustCompile("^\\s*#!")
hasShebang := shebangRegex.Match(fileContents)
var cmd *exec.Cmd
if hasShebang {
cmd = exec.Command(path)
} else {
cmd = exec.Command("/bin/sh", path)
}
cmd.Dir = compiler.BuildDir()
output, err := cmd.CombinedOutput()
if err != nil {
return err
}
compiler.Logger().Info("%s", output)
}
return nil
}