@@ -53,13 +53,18 @@ func BuildImage(image string, handler string, functionName string, language stri
5353 return fmt .Errorf ("building %s, %s is an invalid path" , functionName , handler )
5454 }
5555
56- tempPath , err := createBuildContext (functionName , handler , language , isLanguageTemplate (language ), langTemplate .HandlerFolder , copyExtraPaths )
56+ opts := []builder.BuildContextOption {}
57+ if len (langTemplate .HandlerFolder ) > 0 {
58+ opts = append (opts , builder .WithHandlerOverlay (langTemplate .HandlerFolder ))
59+ }
60+
61+ buildContext , err := builder .CreateBuildContext (functionName , handler , language , copyExtraPaths , opts ... )
5762 if err != nil {
5863 return err
5964 }
6065
6166 if shrinkwrap {
62- fmt .Printf ("%s shrink-wrapped to %s\n " , functionName , tempPath )
67+ fmt .Printf ("%s shrink-wrapped to %s\n " , functionName , buildContext )
6368 return nil
6469 }
6570
@@ -154,7 +159,7 @@ func BuildImage(image string, handler string, functionName string, language stri
154159 log .Printf ("Build flags: %+v\n " , args )
155160
156161 task := v2execute.ExecTask {
157- Cwd : tempPath ,
162+ Cwd : buildContext ,
158163 Command : command ,
159164 Args : args ,
160165 StreamStdio : ! quietBuild ,
@@ -308,101 +313,6 @@ type dockerBuild struct {
308313 ForcePull bool
309314}
310315
311- var defaultDirPermissions os.FileMode = 0700
312-
313- const defaultHandlerFolder string = "function"
314-
315- // isRunningInCI checks the ENV var CI and returns true if it's set to true or 1
316- func isRunningInCI () bool {
317- if env , ok := os .LookupEnv ("CI" ); ok {
318- if env == "true" || env == "1" {
319- return true
320- }
321- }
322- return false
323- }
324-
325- // createBuildContext creates temporary build folder to perform a Docker build with language template
326- func createBuildContext (functionName string , handler string , language string , useFunction bool , handlerFolder string , copyExtraPaths []string ) (string , error ) {
327- tempPath := fmt .Sprintf ("./build/%s/" , functionName )
328-
329- if err := os .RemoveAll (tempPath ); err != nil {
330- return tempPath , fmt .Errorf ("unable to clear temporary build folder: %s" , tempPath )
331- }
332-
333- functionPath := tempPath
334-
335- if useFunction {
336- if handlerFolder == "" {
337- functionPath = path .Join (functionPath , defaultHandlerFolder )
338- } else {
339- functionPath = path .Join (functionPath , handlerFolder )
340- }
341- }
342-
343- // fmt.Printf("Preparing: %s %s\n", handler+"/", functionPath)
344-
345- if isRunningInCI () {
346- defaultDirPermissions = 0777
347- }
348-
349- mkdirErr := os .MkdirAll (functionPath , defaultDirPermissions )
350- if mkdirErr != nil {
351- fmt .Printf ("Error creating path: %s - %s.\n " , functionPath , mkdirErr .Error ())
352- return tempPath , mkdirErr
353- }
354-
355- if useFunction {
356- if err := CopyFiles (path .Join ("./template/" , language ), tempPath ); err != nil {
357- fmt .Printf ("Error copying template directory: %s.\n " , err .Error ())
358- return tempPath , err
359- }
360- }
361-
362- // Overlay in user-function
363- // CopyFiles(handler, functionPath)
364- infos , err := os .ReadDir (handler )
365- if err != nil {
366- fmt .Printf ("Error reading the handler: %s - %s.\n " , handler , err .Error ())
367- return tempPath , err
368- }
369-
370- for _ , info := range infos {
371- switch info .Name () {
372- case "build" , "template" :
373- fmt .Printf ("Skipping \" %s\" folder\n " , info .Name ())
374- continue
375- default :
376- if err := CopyFiles (
377- filepath .Clean (path .Join (handler , info .Name ())),
378- filepath .Clean (path .Join (functionPath , info .Name ())),
379- ); err != nil {
380- return tempPath , err
381- }
382- }
383- }
384-
385- for _ , extraPath := range copyExtraPaths {
386- extraPathAbs , err := pathInScope (extraPath , "." )
387- if err != nil {
388- return tempPath , err
389- }
390- // Note that if useFunction is false, ie is a `dockerfile` template, then
391- // functionPath == tempPath, the docker build context, not the `function` handler folder
392- // inside the docker build context
393- copyErr := CopyFiles (
394- extraPathAbs ,
395- filepath .Clean (path .Join (functionPath , extraPath )),
396- )
397-
398- if copyErr != nil {
399- return tempPath , copyErr
400- }
401- }
402-
403- return tempPath , nil
404- }
405-
406316// pathInScope returns the absolute path to `path` and ensures that it is located within the
407317// provided scope. An error will be returned, if the path is outside of the provided scope.
408318func pathInScope (path string , scope string ) (string , error ) {
@@ -538,7 +448,3 @@ func deDuplicate(buildOptPackages []string) []string {
538448 }
539449 return retPackages
540450}
541-
542- func isLanguageTemplate (language string ) bool {
543- return strings .ToLower (language ) != "dockerfile"
544- }
0 commit comments