Skip to content

Commit

Permalink
Remove master.zip after unpacking templates from archive
Browse files Browse the repository at this point in the history
1. Remove master.zip after pulling template directory
2. Tests template fetch with mock HTTP server

Signed-off-by: Eric Stoekl <ems5311@gmail.com>
  • Loading branch information
ericstoekl authored and alexellis committed Sep 22, 2017
1 parent 64475ae commit db425d5
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 17 deletions.
6 changes: 3 additions & 3 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func runBuild(cmd *cobra.Command, args []string) {
}
}

if pullErr := PullTemplates(); pullErr != nil {
if pullErr := PullTemplates(""); pullErr != nil {
log.Fatalln("Could not pull templates for OpenFaaS.", pullErr)
}

Expand Down Expand Up @@ -114,13 +114,13 @@ func runBuild(cmd *cobra.Command, args []string) {
}

// PullTemplates pulls templates from Github from the master zip download file.
func PullTemplates() error {
func PullTemplates(templateUrl string) error {
var err error
exists, err := os.Stat("./template")
if err != nil || exists == nil {
log.Println("No templates found in current directory.")

err = fetchTemplates()
err = fetchTemplates(templateUrl)
if err != nil {
log.Println("Unable to download templates from Github.")
return err
Expand Down
35 changes: 22 additions & 13 deletions commands/fetch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ import (
"strings"
)

const ZipFileName string = "master.zip"

// fetchTemplates fetch code templates from GitHub master zip file.
func fetchTemplates() error {
func fetchTemplates(templateUrl string) error {

err := fetchMasterZip()
err := fetchMasterZip(templateUrl)

zipFile, err := zip.OpenReader("./master.zip")
zipFile, err := zip.OpenReader(ZipFileName)
if err != nil {
return err
}

log.Printf("Attempting to expand templates from master.zip\n")
log.Printf("Attempting to expand templates from %s\n", ZipFileName)

for _, z := range zipFile.File {
relativePath := strings.Replace(z.Name, "faas-cli-master/", "", -1)
Expand All @@ -51,26 +53,32 @@ func fetchTemplates() error {
}
}

log.Printf("Cleaning up zip file...")
if _, err := os.Stat(ZipFileName); err == nil {
os.Remove(ZipFileName)
} else {
return err
}
fmt.Println("")

return err
}

func fetchMasterZip() error {
func fetchMasterZip(templateUrl string) error {
var err error
if _, err = os.Stat("master.zip"); err != nil {
templateURL := os.Getenv("templateUrl")
if len(templateURL) == 0 {
templateURL = "https://github.com/alexellis/faas-cli/archive/master.zip"
if _, err = os.Stat(ZipFileName); err != nil {

if len(templateUrl) == 0 {
templateUrl = "https://github.com/alexellis/faas-cli/archive/" + ZipFileName
}
c := http.Client{}

req, err := http.NewRequest("GET", templateURL, nil)
req, err := http.NewRequest("GET", templateUrl, nil)
if err != nil {
log.Println(err.Error())
return err
}
log.Printf("HTTP GET %s\n", templateURL)
log.Printf("HTTP GET %s\n", templateUrl)
res, err := c.Do(req)
if err != nil {
log.Println(err.Error())
Expand All @@ -85,12 +93,13 @@ func fetchMasterZip() error {
return err
}

log.Printf("Writing %dKb to master.zip\n", len(bytesOut)/1024)
err = ioutil.WriteFile("./master.zip", bytesOut, 0700)
log.Printf("Writing %dKb to %s\n", len(bytesOut)/1024, ZipFileName)
err = ioutil.WriteFile(ZipFileName, bytesOut, 0700)
if err != nil {
log.Println(err.Error())
}
}
fmt.Println("")
return err
}

Expand Down
48 changes: 48 additions & 0 deletions commands/fetch_templates_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Alex Ellis, Eric Stoekl 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
package commands

import (
"net/http"
"net/http/httptest"
"os"
"testing"
)

var SmallestZipFile = []byte{80, 75, 05, 06, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}

func Test_PullTemplates(t *testing.T) {
// Remove existing master.zip file if it exists
if _, err := os.Stat("master.zip"); err == nil {
t.Log("Found a master.zip file, removing it...")

err := os.Remove("master.zip")
if err != nil {
t.Fatal(err)
}
}

// Remove existing templates folder, if it exist
if _, err := os.Stat("template/"); err == nil {
t.Log("Found a template/ directory, removing it...")

err := os.RemoveAll("template/")
if err != nil {
t.Fatal(err)
}
}

// Create fake server for testing.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// Write out the minimum number of bytes to make the response a valid .zip file
w.Write(SmallestZipFile)

}))
defer ts.Close()

err := PullTemplates(ts.URL)
if err != nil {
t.Error(err)
}
}
2 changes: 1 addition & 1 deletion commands/new_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ the "Dockerfile" lang type in your YAML file.
return
}

PullTemplates()
PullTemplates("")

if _, err := os.Stat(functionName); err == nil {
fmt.Printf("Folder: %s already exists\n", functionName)
Expand Down

0 comments on commit db425d5

Please sign in to comment.