Skip to content

Commit

Permalink
skip already existing templates while build and publish
Browse files Browse the repository at this point in the history
Signed-off-by: Nitishkumar Singh <[email protected]>
  • Loading branch information
nitishkumar71 authored and alexellis committed Mar 12, 2021
1 parent 2cec979 commit 7de6613
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
7 changes: 6 additions & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@ func runBuild(cmd *cobra.Command, args []string) error {
}

if len(services.StackConfiguration.TemplateConfigs) != 0 && !disableStackPull {
err := pullStackTemplates(services.StackConfiguration.TemplateConfigs, cmd)
newTemplateInfos, err := filterExistingTemplates(services.StackConfiguration.TemplateConfigs, "./template")
if err != nil {
return fmt.Errorf("Already pulled templates directory has issue: %s", err.Error())
}

err = pullStackTemplates(newTemplateInfos, cmd)
if err != nil {
return fmt.Errorf("could not pull templates from function yaml file: %s", err.Error())
}
Expand Down
7 changes: 6 additions & 1 deletion commands/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ func runPublish(cmd *cobra.Command, args []string) error {
fmt.Printf("Created buildx node: %s\n", res.Stdout)

if len(services.StackConfiguration.TemplateConfigs) != 0 && !disableStackPull {
err := pullStackTemplates(services.StackConfiguration.TemplateConfigs, cmd)
newTemplateInfos, err := filterExistingTemplates(services.StackConfiguration.TemplateConfigs, "./template")
if err != nil {
return fmt.Errorf("Already pulled templates directory has issue: %s", err.Error())
}

err = pullStackTemplates(newTemplateInfos, cmd)
if err != nil {
return fmt.Errorf("could not pull templates from function yaml file: %s", err.Error())
}
Expand Down
14 changes: 14 additions & 0 deletions commands/template_pull_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"io/ioutil"
"os"

"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -94,3 +95,16 @@ func findTemplate(templateInfo []stack.TemplateSource, customName string) (speci
}
return nil
}

// filter templates which are already available on filesystem
func filterExistingTemplates(templateInfo []stack.TemplateSource, templatesDir string) ([]stack.TemplateSource, error) {
var newTemplates []stack.TemplateSource
for _, info := range templateInfo {
templatePath := fmt.Sprintf("%s/%s", templatesDir, info.Name)
if _, err := os.Stat(templatePath); os.IsNotExist(err) {
newTemplates = append(newTemplates, info)
}
}

return newTemplates, nil
}
31 changes: 31 additions & 0 deletions commands/template_pull_stack_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package commands

import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/openfaas/faas-cli/builder"
"github.com/openfaas/faas-cli/stack"
)

Expand Down Expand Up @@ -105,3 +108,31 @@ func Test_pullAllTemplates(t *testing.T) {
})
}
}

func Test_filterExistingTemplates(t *testing.T) {
templatesDir := "./template"
defer os.RemoveAll(templatesDir)

templates := []stack.TemplateSource{
{Name: "dockerfile", Source: "https:/openfaas-incubator/powershell-http-template"},
{Name: "ruby", Source: "https:/openfaas-incubator/openfaas-rust-template"},
{Name: "perl", Source: "https:/openfaas-incubator/perl-template"},
}

// Copy the submodule to temp directory to avoid altering it during tests
testRepoGit := filepath.Join("testdata", "templates", "template")
builder.CopyFiles(testRepoGit, templatesDir)

newTemplateInfos, err := filterExistingTemplates(templates, templatesDir)
if err != nil {
t.Errorf("Unexpected error: %s", err.Error())
}

if len(newTemplateInfos) != 1 {
t.Errorf("Wanted new templates: `%d` got `%d`", 1, len(newTemplateInfos))
}

if newTemplateInfos[0].Name != "perl" {
t.Errorf("Wanted template: `%s` got `%s`", "perl", newTemplateInfos[0].Name)
}
}

0 comments on commit 7de6613

Please sign in to comment.