@@ -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
@@ -152,7 +157,7 @@ func BuildImage(image string, handler string, functionName string, language stri
152157			log .Printf ("Build flags: %+v\n " , args )
153158
154159			task  :=  v2execute.ExecTask {
155- 				Cwd :         tempPath ,
160+ 				Cwd :         buildContext ,
156161				Command :     command ,
157162				Args :        args ,
158163				StreamStdio : ! quietBuild ,
@@ -306,101 +311,6 @@ type dockerBuild struct {
306311	ForcePull  bool 
307312}
308313
309- var  defaultDirPermissions  os.FileMode  =  0700 
310- 
311- const  defaultHandlerFolder  string  =  "function" 
312- 
313- // isRunningInCI checks the ENV var CI and returns true if it's set to true or 1 
314- func  isRunningInCI () bool  {
315- 	if  env , ok  :=  os .LookupEnv ("CI" ); ok  {
316- 		if  env  ==  "true"  ||  env  ==  "1"  {
317- 			return  true 
318- 		}
319- 	}
320- 	return  false 
321- }
322- 
323- // createBuildContext creates temporary build folder to perform a Docker build with language template 
324- func  createBuildContext (functionName  string , handler  string , language  string , useFunction  bool , handlerFolder  string , copyExtraPaths  []string ) (string , error ) {
325- 	tempPath  :=  fmt .Sprintf ("./build/%s/" , functionName )
326- 
327- 	if  err  :=  os .RemoveAll (tempPath ); err  !=  nil  {
328- 		return  tempPath , fmt .Errorf ("unable to clear temporary build folder: %s" , tempPath )
329- 	}
330- 
331- 	functionPath  :=  tempPath 
332- 
333- 	if  useFunction  {
334- 		if  handlerFolder  ==  ""  {
335- 			functionPath  =  path .Join (functionPath , defaultHandlerFolder )
336- 		} else  {
337- 			functionPath  =  path .Join (functionPath , handlerFolder )
338- 		}
339- 	}
340- 
341- 	// fmt.Printf("Preparing: %s %s\n", handler+"/", functionPath) 
342- 
343- 	if  isRunningInCI () {
344- 		defaultDirPermissions  =  0777 
345- 	}
346- 
347- 	mkdirErr  :=  os .MkdirAll (functionPath , defaultDirPermissions )
348- 	if  mkdirErr  !=  nil  {
349- 		fmt .Printf ("Error creating path: %s - %s.\n " , functionPath , mkdirErr .Error ())
350- 		return  tempPath , mkdirErr 
351- 	}
352- 
353- 	if  useFunction  {
354- 		if  err  :=  CopyFiles (path .Join ("./template/" , language ), tempPath ); err  !=  nil  {
355- 			fmt .Printf ("Error copying template directory: %s.\n " , err .Error ())
356- 			return  tempPath , err 
357- 		}
358- 	}
359- 
360- 	// Overlay in user-function 
361- 	// CopyFiles(handler, functionPath) 
362- 	infos , err  :=  os .ReadDir (handler )
363- 	if  err  !=  nil  {
364- 		fmt .Printf ("Error reading the handler: %s - %s.\n " , handler , err .Error ())
365- 		return  tempPath , err 
366- 	}
367- 
368- 	for  _ , info  :=  range  infos  {
369- 		switch  info .Name () {
370- 		case  "build" , "template" :
371- 			fmt .Printf ("Skipping \" %s\"  folder\n " , info .Name ())
372- 			continue 
373- 		default :
374- 			if  err  :=  CopyFiles (
375- 				filepath .Clean (path .Join (handler , info .Name ())),
376- 				filepath .Clean (path .Join (functionPath , info .Name ())),
377- 			); err  !=  nil  {
378- 				return  tempPath , err 
379- 			}
380- 		}
381- 	}
382- 
383- 	for  _ , extraPath  :=  range  copyExtraPaths  {
384- 		extraPathAbs , err  :=  pathInScope (extraPath , "." )
385- 		if  err  !=  nil  {
386- 			return  tempPath , err 
387- 		}
388- 		// Note that if useFunction is false, ie is a `dockerfile` template, then 
389- 		// functionPath == tempPath, the docker build context, not the `function` handler folder 
390- 		// inside the docker build context 
391- 		copyErr  :=  CopyFiles (
392- 			extraPathAbs ,
393- 			filepath .Clean (path .Join (functionPath , extraPath )),
394- 		)
395- 
396- 		if  copyErr  !=  nil  {
397- 			return  tempPath , copyErr 
398- 		}
399- 	}
400- 
401- 	return  tempPath , nil 
402- }
403- 
404314// pathInScope returns the absolute path to `path` and ensures that it is located within the 
405315// provided scope. An error will be returned, if the path is outside of the provided scope. 
406316func  pathInScope (path  string , scope  string ) (string , error ) {
@@ -536,7 +446,3 @@ func deDuplicate(buildOptPackages []string) []string {
536446	}
537447	return  retPackages 
538448}
539- 
540- func  isLanguageTemplate (language  string ) bool  {
541- 	return  strings .ToLower (language ) !=  "dockerfile" 
542- }
0 commit comments