diff --git a/commands/build.go b/commands/build.go index 16f142ae..7d22b8e8 100644 --- a/commands/build.go +++ b/commands/build.go @@ -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()) } diff --git a/commands/publish.go b/commands/publish.go index 9297d5df..78a5e1ef 100644 --- a/commands/publish.go +++ b/commands/publish.go @@ -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()) } diff --git a/commands/template_pull_stack.go b/commands/template_pull_stack.go index 4fbea788..a0a39498 100644 --- a/commands/template_pull_stack.go +++ b/commands/template_pull_stack.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "io/ioutil" + "os" "gopkg.in/yaml.v2" @@ -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 +} diff --git a/commands/template_pull_stack_test.go b/commands/template_pull_stack_test.go index ce30c0c7..ee650963 100644 --- a/commands/template_pull_stack_test.go +++ b/commands/template_pull_stack_test.go @@ -1,9 +1,12 @@ package commands import ( + "os" + "path/filepath" "reflect" "testing" + "github.com/openfaas/faas-cli/builder" "github.com/openfaas/faas-cli/stack" ) @@ -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://github.com/openfaas-incubator/powershell-http-template"}, + {Name: "ruby", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"}, + {Name: "perl", Source: "https://github.com/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) + } +}