Skip to content

Commit

Permalink
1) Additional error checking for fetch request response
Browse files Browse the repository at this point in the history
2) Update guide with new links
3) Change to simplified version of
ParseYAMLFile/ParseYAMLFileForLanguageTemplate
4) simplify canWriteLanguage map, remove global variable usage
5) Don't print error message if no fprocess supplied (proxy/deploy.go)
6) Use absolute filepath for switching back to home directory after each new_function_test
7) Don't remove files before --overwrite

Signed-off-by: Eric Stoekl <ems5311@gmail.com>
Signed-off-by: Minh-Quan TRAN <account@itscaro.me>
  • Loading branch information
ericstoekl committed Oct 29, 2017
1 parent e90747e commit 4b13408
Show file tree
Hide file tree
Showing 28 changed files with 110 additions and 136 deletions.
2 changes: 1 addition & 1 deletion commands/add_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Currently supported verbs: %v`, supportedVerbs)
Short: "Downloads templates from the specified github repo",
Long: `Downloads the compressed github repo specified by [URL], and extracts the 'template'
directory from the root of the repo, if it exists.`,
Example: "faas-cli template pull https://github.com/alexellis/faas-cli",
Example: "faas-cli template pull https://github.com/openfaas/faas-cli",
Run: runAddTemplate,
}

Expand Down
13 changes: 4 additions & 9 deletions commands/add_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,14 @@ func Test_addTemplate_with_overwriting(t *testing.T) {
faasCmd.SetArgs([]string{"template", "pull", repository})
faasCmd.Execute()

// reset cacheCanWriteLanguage
cacheCanWriteLanguage = make(map[string]bool)

var buf bytes.Buffer
log.SetOutput(&buf)

r := regexp.MustCompile(`(?m:Cannot overwrite the following \(\d+\) directories:)`)
r := regexp.MustCompile(`(?m:Cannot overwrite the following \d+ directories:)`)

faasCmd.SetArgs([]string{"template", "pull", repository})
faasCmd.Execute()

// reset cacheCanWriteLanguage
cacheCanWriteLanguage = make(map[string]bool)

if !r.MatchString(buf.String()) {
t.Fatal(buf.String())
}
Expand All @@ -63,7 +57,8 @@ func Test_addTemplate_with_overwriting(t *testing.T) {
faasCmd.SetArgs([]string{"template", "pull", repository, "--overwrite"})
faasCmd.Execute()

if r.MatchString(buf.String()) {
str := buf.String()
if r.MatchString(str) {
t.Fatal()
}

Expand All @@ -89,7 +84,7 @@ func Test_addTemplate_no_arg(t *testing.T) {
func Test_addTemplate_error_not_valid_url(t *testing.T) {
var buf bytes.Buffer

faasCmd.SetArgs([]string{"template", "pull", "git@github.com:alexellis/faas-cli.git"})
faasCmd.SetArgs([]string{"template", "pull", "git@github.com:openfaas/faas-cli.git"})
faasCmd.SetOutput(&buf)
faasCmd.Execute()

Expand Down
2 changes: 1 addition & 1 deletion commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func runBuild(cmd *cobra.Command, args []string) {

var services stack.Services
if len(yamlFile) > 0 {
parsedServices, err := stack.ParseYAMLFileForStack(yamlFile, regex, filter)
parsedServices, err := stack.ParseYAMLFile(yamlFile, regex, filter)
if err != nil {
log.Fatalln(err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func runDeploy(cmd *cobra.Command, args []string) {

var services stack.Services
if len(yamlFile) > 0 {
parsedServices, err := stack.ParseYAMLFileForStack(yamlFile, regex, filter)
parsedServices, err := stack.ParseYAMLFile(yamlFile, regex, filter)
if err != nil {
log.Fatalln(err.Error())
return
Expand Down
61 changes: 30 additions & 31 deletions commands/fetch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package commands

import (
"archive/zip"
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -23,19 +24,19 @@ const (
templateDirectory = "./template/"
)

var cacheCanWriteLanguage = make(map[string]bool)

// fetchTemplates fetch code templates from GitHub master zip file.
func fetchTemplates(templateURL string, overwrite bool) error {
var existingLanguages []string
countFetchedTemplates := 0
availableLanguages := make(map[string]bool)
var fetchedTemplates []string

if len(templateURL) == 0 {
templateURL = defaultTemplateRepository
}

archive, err := fetchMasterZip(templateURL)
if err != nil {
removeArchive(archive)
return err
}

Expand All @@ -62,15 +63,15 @@ func fetchTemplates(templateURL string, overwrite bool) error {
if len(languageSplit) == 3 && relativePath[len(relativePath)-1:] == "/" {
// template/language/

if !canWriteLanguage(language, overwrite) {
if !canWriteLanguage(&availableLanguages, language, overwrite) {
existingLanguages = append(existingLanguages, language)
continue
}
countFetchedTemplates++
fetchedTemplates = append(fetchedTemplates, language)
} else {
// template/language/*

if !canWriteLanguage(language, overwrite) {
if !canWriteLanguage(&availableLanguages, language, overwrite) {
continue
}
}
Expand All @@ -96,18 +97,19 @@ func fetchTemplates(templateURL string, overwrite bool) error {
}

if len(existingLanguages) > 0 {
log.Printf("Cannot overwrite the following (%d) directories: %v\n", len(existingLanguages), existingLanguages)
log.Printf("Cannot overwrite the following %d directories: %v\n", len(existingLanguages), existingLanguages)
}

zipFile.Close()

log.Printf("Fetched %d template(s) from %s\n", countFetchedTemplates, templateURL)
log.Printf("Fetched %d template(s) : %v from %s\n", len(fetchedTemplates), fetchedTemplates, templateURL)

err = removeArchive(archive)

return err
}

// removeArchive removes the given file
func removeArchive(archive string) error {
log.Printf("Cleaning up zip file...")
if _, err := os.Stat(archive); err == nil {
Expand Down Expand Up @@ -140,6 +142,11 @@ func fetchMasterZip(templateURL string) (string, error) {
log.Println(err.Error())
return "", err
}
if res.StatusCode != http.StatusOK {
err := errors.New(fmt.Sprintf("%s is not valid, status code %d", templateURL, res.StatusCode))
log.Println(err.Error())
return "", err
}
if res.Body != nil {
defer res.Body.Close()
}
Expand Down Expand Up @@ -181,32 +188,24 @@ func createPath(relativePath string, perms os.FileMode) error {
}

// canWriteLanguage tells whether the language can be processed or not
// if overwrite is activated, the directory template/language/ is removed before to keep it in sync
func canWriteLanguage(language string, overwrite bool) bool {

func canWriteLanguage(availableLanguages *map[string]bool, language string, overwrite bool) bool {
canWrite := false
if len(language) > 0 {
if _, ok := cacheCanWriteLanguage[language]; ok {
return cacheCanWriteLanguage[language]
if _, ok := (*availableLanguages)[language]; ok {
return (*availableLanguages)[language]
}
canWrite = checkLanguage(language, overwrite)
(*availableLanguages)[language] = canWrite
}

dir := templateDirectory + language
if _, err := os.Stat(dir); err == nil {
// The directory template/language/ exists
if overwrite == false {
cacheCanWriteLanguage[language] = false
} else {
// Clean up the directory to keep in sync with new updates
if err := os.RemoveAll(dir); err != nil {
log.Panicf("Directory %s cannot be removed", dir)
}
cacheCanWriteLanguage[language] = true
}
} else {
cacheCanWriteLanguage[language] = true
}
return canWrite
}

return cacheCanWriteLanguage[language]
func checkLanguage(language string, overwrite bool) bool {
dir := templateDirectory + language
if _, err := os.Stat(dir); err == nil && !overwrite {
// The directory template/language/ exists
return false
}

return false
return true
}
2 changes: 1 addition & 1 deletion commands/fetch_templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
)

func Test_PullTemplates(t *testing.T) {
func Test_fetchTemplates(t *testing.T) {
defer tearDown_fetch_templates(t)

// Create fake server for testing.
Expand Down
2 changes: 1 addition & 1 deletion commands/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func runInvoke(cmd *cobra.Command, args []string) {
functionName = args[0]

if len(yamlFile) > 0 {
parsedServices, err := stack.ParseYAMLFileForStack(yamlFile, regex, filter)
parsedServices, err := stack.ParseYAMLFile(yamlFile, regex, filter)
if err != nil {
log.Fatalln(err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func runList(cmd *cobra.Command, args []string) {
var gatewayAddress string
var yamlGateway string
if len(yamlFile) > 0 {
parsedServices, err := stack.ParseYAMLFileForStack(yamlFile, regex, filter)
parsedServices, err := stack.ParseYAMLFile(yamlFile, regex, filter)
if err != nil {
log.Fatalln(err.Error())
return
Expand Down
6 changes: 3 additions & 3 deletions commands/new_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func runNewFunction(cmd *cobra.Command, args []string) {
if list == true {
var available_templates string

if f, err := ioutil.ReadDir(templateDirectory); err != nil {
available_templates = "There is no available languages"
if templateFolders, err := ioutil.ReadDir(templateDirectory); err != nil {
available_templates = "No language templates were found. Please run 'faas-cli template pull'."
} else {
for _, file := range f {
for _, file := range templateFolders {
if file.IsDir() {
available_templates += fmt.Sprintf("- %s\n", file.Name())
}
Expand Down
19 changes: 14 additions & 5 deletions commands/new_function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package commands

import (
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
Expand All @@ -19,11 +20,16 @@ const InvalidYAMLMsg = `is not valid YAML`
const InvalidYAMLMap = `map is empty`
const ListOptionOutput = `Languages available as templates:
- csharp
- go
- go-armhf
- node
- node-arm64
- node-armhf
- python
- python-armhf
- python3
- ruby`

const LangNotExistsOutput = `(?m:bash is unavailable or not supported.)`

type NewFunctionTest struct {
Expand Down Expand Up @@ -89,7 +95,7 @@ func runNewFunctionTest(t *testing.T, nft NewFunctionTest) {
}

// Make sure that the information in the YAML file is correct:
parsedServices, err := stack.ParseYAMLFileForStack(funcYAML, "", "")
parsedServices, err := stack.ParseYAMLFile(funcYAML, "", "")
if err != nil {
t.Fatalf("Couldn't open modified YAML file \"%s\" due to error: %v", funcYAML, err)
}
Expand All @@ -111,7 +117,8 @@ func runNewFunctionTest(t *testing.T, nft NewFunctionTest) {
}

func Test_newFunctionTests(t *testing.T) {
// Change directory to testdata

homeDir, _ := filepath.Abs(".")
if err := os.Chdir("testdata/new_function"); err != nil {
t.Fatalf("Error on cd to testdata dir: %v", err)
}
Expand All @@ -122,13 +129,14 @@ func Test_newFunctionTests(t *testing.T) {
})
}

if err := os.Chdir("../.."); err != nil {
if err := os.Chdir(homeDir); err != nil {
t.Fatalf("Error on cd back to commands/ directory: %v", err)
}
}

func Test_newFunctionListCmds(t *testing.T) {

homeDir, _ := filepath.Abs(".")
if err := os.Chdir("testdata/new_function"); err != nil {
t.Fatalf("Error on cd to testdata dir: %v", err)
}
Expand All @@ -148,13 +156,14 @@ func Test_newFunctionListCmds(t *testing.T) {
t.Fatalf("Output is not as expected: %s\n", stdOut)
}

if err := os.Chdir("../.."); err != nil {
if err := os.Chdir(homeDir); err != nil {
t.Fatalf("Error on cd back to commands/ directory: %v", err)
}
}

func Test_languageNotExists(t *testing.T) {

homeDir, _ := filepath.Abs(".")
if err := os.Chdir("testdata/new_function"); err != nil {
t.Fatalf("Error on cd to testdata dir: %v", err)
}
Expand All @@ -178,7 +187,7 @@ func Test_languageNotExists(t *testing.T) {
t.Fatalf("Output is not as expected: %s\n", stdOut)
}

if err := os.Chdir("../.."); err != nil {
if err := os.Chdir(homeDir); err != nil {
t.Fatalf("Error on cd back to commands/ directory: %v", err)
}
}
2 changes: 1 addition & 1 deletion commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func runPush(cmd *cobra.Command, args []string) {

var services stack.Services
if len(yamlFile) > 0 {
parsedServices, err := stack.ParseYAMLFileForStack(yamlFile, regex, filter)
parsedServices, err := stack.ParseYAMLFile(yamlFile, regex, filter)
if err != nil {
log.Fatalln(err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion commands/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ explicitly specifying a function name.`,
func runDelete(cmd *cobra.Command, args []string) {
var services stack.Services
if len(yamlFile) > 0 {
parsedServices, err := stack.ParseYAMLFileForStack(yamlFile, regex, filter)
parsedServices, err := stack.ParseYAMLFile(yamlFile, regex, filter)
if err != nil {
log.Fatalln(err.Error())
return
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
4 changes: 2 additions & 2 deletions guide/TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ template
In order to build functions using 3rd party templates, you need to add 3rd templates before the build step, with the following command:

```
./faas-cli add-template https://github.com/alexellis/faas-cli
./faas-cli template pull https://github.com/itscaro/openfaas-template-php
```

If you need to update the downloaded repository, just add the flag `--overwrite` to the download command:

```
./faas-cli add-template https://github.com/alexellis/faas-cli --override
./faas-cli template pull https://github.com/itscaro/openfaas-template-php --override
```
2 changes: 0 additions & 2 deletions proxy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ func DeployFunction(fprocess string, gateway string, functionName string, image
var fprocessTemplate string
if len(fprocess) > 0 {
fprocessTemplate = fprocess
} else {
fmt.Printf("Command to be invoked for function %s not found.\n", functionName)
}

gateway = strings.TrimRight(gateway, "/")
Expand Down
27 changes: 0 additions & 27 deletions stack/common.go

This file was deleted.

Loading

0 comments on commit 4b13408

Please sign in to comment.