@@ -52,7 +52,18 @@ func BuildImage(image string, handler string, functionName string, language stri
52
52
return fmt .Errorf ("building %s, %s is an invalid path" , functionName , handler )
53
53
}
54
54
55
- tempPath , err := createBuildContext (functionName , handler , language , isLanguageTemplate (language ), langTemplate .HandlerFolder , copyExtraPaths )
55
+ // To avoid breaking the CLI for custom templates that do not set the language attribute
56
+ // we ensure it is always set.
57
+ //
58
+ // While templates are expected to have the language in `template.yaml` set to the same name as the template folder
59
+ // this was never enforced.
60
+ langTemplate .Language = language
61
+
62
+ if isDockerfileTemplate (langTemplate .Language ) {
63
+ langTemplate = nil
64
+ }
65
+
66
+ tempPath , err := CreateBuildContext (functionName , handler , langTemplate , copyExtraPaths )
56
67
if err != nil {
57
68
return err
58
69
}
@@ -303,8 +314,17 @@ func isRunningInCI() bool {
303
314
return false
304
315
}
305
316
306
- // createBuildContext creates temporary build folder to perform a Docker build with language template
307
- func createBuildContext (functionName string , handler string , language string , useFunction bool , handlerFolder string , copyExtraPaths []string ) (string , error ) {
317
+ // CreateBuildContext creates a Docker build context using the provided function handler and language template.
318
+ //
319
+ // Parameters:
320
+ // - functionName: the name of the function.
321
+ // - handler: path to the function handler folder.
322
+ // - template: function language template to use. Set to nil if no template is required (e.g. the handler folder is the build context with Dockerfile).
323
+ // - copyExtraPaths: additional path to copy into the function handler folder. Paths should be relative to the current directory.
324
+ // Any path outside of this current directory will be skipped.
325
+ //
326
+ // Returns the path to the new build context. An error is returned if creating the build context fails.
327
+ func CreateBuildContext (functionName string , handler string , template * stack.LanguageTemplate , copyExtraPaths []string ) (string , error ) {
308
328
tempPath := fmt .Sprintf ("./build/%s/" , functionName )
309
329
310
330
if err := os .RemoveAll (tempPath ); err != nil {
@@ -313,16 +333,19 @@ func createBuildContext(functionName string, handler string, language string, us
313
333
314
334
functionPath := tempPath
315
335
316
- if useFunction {
317
- if handlerFolder == "" {
318
- functionPath = path .Join (functionPath , defaultHandlerFolder )
336
+ useTemplate := false
337
+ if template != nil {
338
+ useTemplate = true
339
+ }
340
+
341
+ if useTemplate {
342
+ if len (template .HandlerFolder ) > 0 {
343
+ functionPath = path .Join (functionPath , template .HandlerFolder )
319
344
} else {
320
- functionPath = path .Join (functionPath , handlerFolder )
345
+ functionPath = path .Join (functionPath , defaultHandlerFolder )
321
346
}
322
347
}
323
348
324
- // fmt.Printf("Preparing: %s %s\n", handler+"/", functionPath)
325
-
326
349
if isRunningInCI () {
327
350
defaultDirPermissions = 0777
328
351
}
@@ -333,8 +356,8 @@ func createBuildContext(functionName string, handler string, language string, us
333
356
return tempPath , mkdirErr
334
357
}
335
358
336
- if useFunction {
337
- if err := CopyFiles (path .Join ("./template/" , language ), tempPath ); err != nil {
359
+ if useTemplate {
360
+ if err := CopyFiles (path .Join ("./template/" , template . Language ), tempPath ); err != nil {
338
361
fmt .Printf ("Error copying template directory: %s.\n " , err .Error ())
339
362
return tempPath , err
340
363
}
@@ -350,6 +373,7 @@ func createBuildContext(functionName string, handler string, language string, us
350
373
351
374
for _ , info := range infos {
352
375
switch info .Name () {
376
+ // Ignore the build and template folder.
353
377
case "build" , "template" :
354
378
fmt .Printf ("Skipping \" %s\" folder\n " , info .Name ())
355
379
continue
@@ -368,16 +392,12 @@ func createBuildContext(functionName string, handler string, language string, us
368
392
if err != nil {
369
393
return tempPath , err
370
394
}
371
- // Note that if useFunction is false, ie is a `dockerfile` template, then
395
+
396
+ // Note that if template is nil, i.e. is a `dockerfile` template, then
372
397
// functionPath == tempPath, the docker build context, not the `function` handler folder
373
398
// inside the docker build context
374
- copyErr := CopyFiles (
375
- extraPathAbs ,
376
- filepath .Clean (path .Join (functionPath , extraPath )),
377
- )
378
-
379
- if copyErr != nil {
380
- return tempPath , copyErr
399
+ if err := CopyFiles (extraPathAbs , filepath .Clean (path .Join (functionPath , extraPath ))); err != nil {
400
+ return tempPath , err
381
401
}
382
402
}
383
403
@@ -516,6 +536,6 @@ func deDuplicate(buildOptPackages []string) []string {
516
536
return retPackages
517
537
}
518
538
519
- func isLanguageTemplate (language string ) bool {
520
- return strings .ToLower (language ) ! = "dockerfile"
539
+ func isDockerfileTemplate (language string ) bool {
540
+ return strings .ToLower (language ) = = "dockerfile"
521
541
}
0 commit comments