From 703935c0d6d5a07312acf5daae2e90678d092556 Mon Sep 17 00:00:00 2001 From: Raymond Wang <14915548+wchengru@users.noreply.github.com> Date: Mon, 30 Aug 2021 14:17:32 -0700 Subject: [PATCH] valid the bootstraps before passing to NewBootstrapSingleCmd (#45) * Pull upstream changes 2021/06 (#39) * valid the bootstraps before passing to NewBootstrapSingleCmd * refactor the code to better asign the default value * reformat indents * use function to detect is file exist * minor fix * fix a logical error Co-authored-by: Renato Valenzuela <37676028+valerena@users.noreply.github.com> --- cmd/aws-lambda-rie/main.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cmd/aws-lambda-rie/main.go b/cmd/aws-lambda-rie/main.go index 4c28f51..a151ae7 100644 --- a/cmd/aws-lambda-rie/main.go +++ b/cmd/aws-lambda-rie/main.go @@ -56,16 +56,34 @@ func getCLIArgs() (options, []string) { return opts, args } +func isBootstrapFileExist(filePath string) bool { + file, err := os.Stat(filePath) + return !os.IsNotExist(err) && !file.IsDir() +} + func getBootstrap(args []string, opts options) (*rapidcore.Bootstrap, string) { var bootstrapLookupCmd []string var handler string currentWorkingDir := "/var/task" // default value if len(args) <= 1 { + // set default value to /var/task/bootstrap, but switch to the other options if it doesn't exist bootstrapLookupCmd = []string{ fmt.Sprintf("%s/bootstrap", currentWorkingDir), - optBootstrap, - runtimeBootstrap, + } + + if !isBootstrapFileExist(bootstrapLookupCmd[0]) { + var bootstrapCmdCandidates = []string{ + optBootstrap, + runtimeBootstrap, + } + + for i, bootstrapCandidate := range bootstrapCmdCandidates { + if isBootstrapFileExist(bootstrapCandidate) { + bootstrapLookupCmd = []string{bootstrapCmdCandidates[i]} + break + } + } } // handler is used later to set an env var for Lambda Image support