From fc80f7828b6c594a6fd0fa89596062abb8cf3993 Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Wed, 15 Apr 2020 04:07:27 +0200 Subject: [PATCH] Warn if function conditions are not met (#224) * Warn if function conditions are not met * Change signature to be more structured --- go/porcelain/deploy.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/go/porcelain/deploy.go b/go/porcelain/deploy.go index 9aa743a5..1409b87e 100644 --- a/go/porcelain/deploy.go +++ b/go/porcelain/deploy.go @@ -62,6 +62,10 @@ type DeployObserver interface { OnFailedUpload(*FileBundle) } +type DeployWarner interface { + OnWalkWarning(path, msg string) +} + // DeployOptions holds the option for creating a new deploy type DeployOptions struct { SiteID string @@ -579,12 +583,16 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, error) { return nil, err } functions.Add(file.Name, file) - case goFile(filePath, i): + case goFile(filePath, i, observer): file, err := newFunctionFile(filePath, i, goRuntime, observer) if err != nil { return nil, err } functions.Add(file.Name, file) + default: + if warner, ok := observer.(DeployWarner); ok { + warner.OnWalkWarning(filePath, "Function is not valid for deployment. Please check that it matches the format for the runtime.") + } } } @@ -653,17 +661,31 @@ func jsFile(i os.FileInfo) bool { return filepath.Ext(i.Name()) == ".js" } -func goFile(filePath string, i os.FileInfo) bool { +func goFile(filePath string, i os.FileInfo, observer DeployObserver) bool { + warner, hasWarner := observer.(DeployWarner) + if m := i.Mode(); m&0111 == 0 { // check if it's an executable file + if hasWarner { + warner.OnWalkWarning(filePath, "Go binary does not have executable permissions") + } return false } if _, err := elf.Open(filePath); err != nil { // check if it's a linux executable + if hasWarner { + warner.OnWalkWarning(filePath, "Go binary is not a linux executable") + } return false } v, err := version.ReadExe(filePath) - return err == nil && strings.HasPrefix(v.Release, "go1.") + if err != nil || !strings.HasPrefix(v.Release, "go1.") { + if hasWarner { + warner.OnWalkWarning(filePath, "Unable to detect Go version 1.x") + } + } + + return true } func ignoreFile(rel string) bool {