Skip to content

Commit 7de6613

Browse files
nitishkumar71alexellis
authored andcommitted
skip already existing templates while build and publish
Signed-off-by: Nitishkumar Singh <[email protected]>
1 parent 2cec979 commit 7de6613

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

commands/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ func runBuild(cmd *cobra.Command, args []string) error {
198198
}
199199

200200
if len(services.StackConfiguration.TemplateConfigs) != 0 && !disableStackPull {
201-
err := pullStackTemplates(services.StackConfiguration.TemplateConfigs, cmd)
201+
newTemplateInfos, err := filterExistingTemplates(services.StackConfiguration.TemplateConfigs, "./template")
202+
if err != nil {
203+
return fmt.Errorf("Already pulled templates directory has issue: %s", err.Error())
204+
}
205+
206+
err = pullStackTemplates(newTemplateInfos, cmd)
202207
if err != nil {
203208
return fmt.Errorf("could not pull templates from function yaml file: %s", err.Error())
204209
}

commands/publish.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ func runPublish(cmd *cobra.Command, args []string) error {
148148
fmt.Printf("Created buildx node: %s\n", res.Stdout)
149149

150150
if len(services.StackConfiguration.TemplateConfigs) != 0 && !disableStackPull {
151-
err := pullStackTemplates(services.StackConfiguration.TemplateConfigs, cmd)
151+
newTemplateInfos, err := filterExistingTemplates(services.StackConfiguration.TemplateConfigs, "./template")
152+
if err != nil {
153+
return fmt.Errorf("Already pulled templates directory has issue: %s", err.Error())
154+
}
155+
156+
err = pullStackTemplates(newTemplateInfos, cmd)
152157
if err != nil {
153158
return fmt.Errorf("could not pull templates from function yaml file: %s", err.Error())
154159
}

commands/template_pull_stack.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"fmt"
55
"io/ioutil"
6+
"os"
67

78
"gopkg.in/yaml.v2"
89

@@ -94,3 +95,16 @@ func findTemplate(templateInfo []stack.TemplateSource, customName string) (speci
9495
}
9596
return nil
9697
}
98+
99+
// filter templates which are already available on filesystem
100+
func filterExistingTemplates(templateInfo []stack.TemplateSource, templatesDir string) ([]stack.TemplateSource, error) {
101+
var newTemplates []stack.TemplateSource
102+
for _, info := range templateInfo {
103+
templatePath := fmt.Sprintf("%s/%s", templatesDir, info.Name)
104+
if _, err := os.Stat(templatePath); os.IsNotExist(err) {
105+
newTemplates = append(newTemplates, info)
106+
}
107+
}
108+
109+
return newTemplates, nil
110+
}

commands/template_pull_stack_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package commands
22

33
import (
4+
"os"
5+
"path/filepath"
46
"reflect"
57
"testing"
68

9+
"github.com/openfaas/faas-cli/builder"
710
"github.com/openfaas/faas-cli/stack"
811
)
912

@@ -105,3 +108,31 @@ func Test_pullAllTemplates(t *testing.T) {
105108
})
106109
}
107110
}
111+
112+
func Test_filterExistingTemplates(t *testing.T) {
113+
templatesDir := "./template"
114+
defer os.RemoveAll(templatesDir)
115+
116+
templates := []stack.TemplateSource{
117+
{Name: "dockerfile", Source: "https://github.com/openfaas-incubator/powershell-http-template"},
118+
{Name: "ruby", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"},
119+
{Name: "perl", Source: "https://github.com/openfaas-incubator/perl-template"},
120+
}
121+
122+
// Copy the submodule to temp directory to avoid altering it during tests
123+
testRepoGit := filepath.Join("testdata", "templates", "template")
124+
builder.CopyFiles(testRepoGit, templatesDir)
125+
126+
newTemplateInfos, err := filterExistingTemplates(templates, templatesDir)
127+
if err != nil {
128+
t.Errorf("Unexpected error: %s", err.Error())
129+
}
130+
131+
if len(newTemplateInfos) != 1 {
132+
t.Errorf("Wanted new templates: `%d` got `%d`", 1, len(newTemplateInfos))
133+
}
134+
135+
if newTemplateInfos[0].Name != "perl" {
136+
t.Errorf("Wanted template: `%s` got `%s`", "perl", newTemplateInfos[0].Name)
137+
}
138+
}

0 commit comments

Comments
 (0)