refactor: always return an error from the buildpacks triggers

This commit is contained in:
Jose Diaz-Gonzalez
2021-01-17 23:10:21 -05:00
parent aeb9241d3b
commit 46933051b9
2 changed files with 16 additions and 17 deletions

View File

@@ -19,7 +19,7 @@ func main() {
var err error
switch trigger {
case "install":
buildpacks.TriggerInstall()
err = buildpacks.TriggerInstall()
case "post-app-clone-setup":
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)
@@ -30,11 +30,11 @@ func main() {
err = buildpacks.TriggerPostAppRenameSetup(oldAppName, newAppName)
case "post-delete":
appName := flag.Arg(0)
buildpacks.TriggerPostDelete(appName)
err = buildpacks.TriggerPostDelete(appName)
case "post-extract":
appName := flag.Arg(0)
sourceWorkDir := flag.Arg(1)
buildpacks.TriggerPostExtract(appName, sourceWorkDir)
err = buildpacks.TriggerPostExtract(appName, sourceWorkDir)
case "report":
appName := flag.Arg(0)
err = buildpacks.ReportSingleApp(appName, "")

View File

@@ -10,10 +10,12 @@ import (
)
// TriggerInstall runs the install step for the buildpacks plugin
func TriggerInstall() {
func TriggerInstall() error {
if err := common.PropertySetup("buildpacks"); err != nil {
common.LogFail(fmt.Sprintf("Unable to install the buildpacks plugin: %s", err.Error()))
return fmt.Errorf("Unable to install the buildpacks plugin: %s", err.Error())
}
return nil
}
// TriggerPostAppCloneSetup creates new buildpacks files
@@ -40,29 +42,25 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
}
// TriggerPostDelete destroys the buildpacks property for a given app container
func TriggerPostDelete(appName string) {
err := common.PropertyDestroy("buildpacks", appName)
if err != nil {
common.LogFail(err.Error())
}
func TriggerPostDelete(appName string) error {
return common.PropertyDestroy("buildpacks", appName)
}
// TriggerPostExtract writes a .buildpacks file into the app
func TriggerPostExtract(appName string, sourceWorkDir string) {
func TriggerPostExtract(appName string, sourceWorkDir string) error {
buildpacks, err := common.PropertyListGet("buildpacks", appName, "buildpacks")
if err != nil {
return
return nil
}
if len(buildpacks) == 0 {
return
return nil
}
buildpacksPath := filepath.Join(sourceWorkDir, ".buildpacks")
file, err := os.OpenFile(buildpacksPath, os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0600)
if err != nil {
common.LogFail(fmt.Sprintf("Error writing .buildpacks file: %s", err.Error()))
return
return fmt.Errorf("Error writing .buildpacks file: %s", err.Error())
}
w := bufio.NewWriter(file)
@@ -71,8 +69,9 @@ func TriggerPostExtract(appName string, sourceWorkDir string) {
}
if err = w.Flush(); err != nil {
common.LogFail(fmt.Sprintf("Error writing .buildpacks file: %s", err.Error()))
return
return fmt.Errorf("Error writing .buildpacks file: %s", err.Error())
}
file.Chmod(0600)
return nil
}