Skip to content

Commit 35dc699

Browse files
weltekialexellis
authored andcommitted
Export CreateBuildContext function
Simplify the signature of createBuildContext and export the function so that it can be used by other packages. Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent 8b55f87 commit 35dc699

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

Diff for: builder/build.go

+41-21
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,18 @@ func BuildImage(image string, handler string, functionName string, language stri
5252
return fmt.Errorf("building %s, %s is an invalid path", functionName, handler)
5353
}
5454

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)
5667
if err != nil {
5768
return err
5869
}
@@ -303,8 +314,17 @@ func isRunningInCI() bool {
303314
return false
304315
}
305316

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) {
308328
tempPath := fmt.Sprintf("./build/%s/", functionName)
309329

310330
if err := os.RemoveAll(tempPath); err != nil {
@@ -313,16 +333,19 @@ func createBuildContext(functionName string, handler string, language string, us
313333

314334
functionPath := tempPath
315335

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)
319344
} else {
320-
functionPath = path.Join(functionPath, handlerFolder)
345+
functionPath = path.Join(functionPath, defaultHandlerFolder)
321346
}
322347
}
323348

324-
// fmt.Printf("Preparing: %s %s\n", handler+"/", functionPath)
325-
326349
if isRunningInCI() {
327350
defaultDirPermissions = 0777
328351
}
@@ -333,8 +356,8 @@ func createBuildContext(functionName string, handler string, language string, us
333356
return tempPath, mkdirErr
334357
}
335358

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 {
338361
fmt.Printf("Error copying template directory: %s.\n", err.Error())
339362
return tempPath, err
340363
}
@@ -350,6 +373,7 @@ func createBuildContext(functionName string, handler string, language string, us
350373

351374
for _, info := range infos {
352375
switch info.Name() {
376+
// Ignore the build and template folder.
353377
case "build", "template":
354378
fmt.Printf("Skipping \"%s\" folder\n", info.Name())
355379
continue
@@ -368,16 +392,12 @@ func createBuildContext(functionName string, handler string, language string, us
368392
if err != nil {
369393
return tempPath, err
370394
}
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
372397
// functionPath == tempPath, the docker build context, not the `function` handler folder
373398
// 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
381401
}
382402
}
383403

@@ -516,6 +536,6 @@ func deDuplicate(buildOptPackages []string) []string {
516536
return retPackages
517537
}
518538

519-
func isLanguageTemplate(language string) bool {
520-
return strings.ToLower(language) != "dockerfile"
539+
func isDockerfileTemplate(language string) bool {
540+
return strings.ToLower(language) == "dockerfile"
521541
}

Diff for: builder/build_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@ import (
99
"github.com/openfaas/faas-cli/stack"
1010
)
1111

12-
func Test_isLanguageTemplate_Dockerfile(t *testing.T) {
12+
func Test_isDockerfileTemplate_Dockerfile(t *testing.T) {
1313

1414
language := "Dockerfile"
1515

16-
want := false
17-
got := isLanguageTemplate(language)
16+
want := true
17+
got := isDockerfileTemplate(language)
1818
if got != want {
1919
t.Errorf("language: %s got %v, want %v", language, got, want)
2020
}
2121
}
2222

23-
func Test_isLanguageTemplate_Node(t *testing.T) {
24-
23+
func Test_isDockerfileTemplate_Node(t *testing.T) {
2524
language := "node"
2625

27-
want := true
28-
got := isLanguageTemplate(language)
26+
want := false
27+
got := isDockerfileTemplate(language)
2928
if got != want {
3029
t.Errorf("language: %s got %v, want %v", language, got, want)
3130
}

Diff for: builder/publish.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,18 @@ func PublishImage(image string, handler string, functionName string, language st
6161
return fmt.Errorf("building %s, %s is an invalid path", functionName, handler)
6262
}
6363

64-
tempPath, err := createBuildContext(functionName, handler, language, isLanguageTemplate(language), langTemplate.HandlerFolder, copyExtraPaths)
64+
// To avoid breaking the CLI for custom templates that do not set the language attribute
65+
// we ensure it is always set.
66+
//
67+
// While templates are expected to have the language in `template.yaml` set to the same name as the template folder
68+
// this was never enforced.
69+
langTemplate.Language = language
70+
71+
if isDockerfileTemplate(langTemplate.Language) {
72+
langTemplate = nil
73+
}
74+
75+
tempPath, err := CreateBuildContext(functionName, handler, langTemplate, copyExtraPaths)
6576
if err != nil {
6677
return err
6778
}

0 commit comments

Comments
 (0)