Skip to content

Commit

Permalink
Fetch templates from web when not in local folder.
Browse files Browse the repository at this point in the history
Update get.sh to new 0.4 release.
  • Loading branch information
alexellis committed Jul 10, 2017
1 parent 08d22d3 commit 340e34c
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ build/
faas-cli
faas-cli-darwin
faas-cli-armhf
master.zip

*.jpg
*.pyc
20 changes: 17 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

const providerName = "faas"
const defaultNetwork = "func_functions"

var GitCommit string

func main() {
Expand Down Expand Up @@ -57,14 +58,14 @@ func main() {

flag.StringVar(&yamlFile, "yaml", "", "use a yaml file for a set of functions")
flag.StringVar(&yamlFileShort, "f", "", "use a yaml file for a set of functions (same as -yaml)")
flag.BoolVar(&version, "version", false, "show version and quit")
flag.BoolVar(&version, "version", false, "show version and quit")

flag.Parse()

if version {
if version {
fmt.Printf("Git Commit: %s\n", GitCommit)
return
}
}

// support short-argument -f
if len(yamlFile) == 0 && len(yamlFileShort) > 0 {
Expand Down Expand Up @@ -109,6 +110,19 @@ func main() {

switch action {
case "build":

exists, err := os.Stat("./template")
if err != nil || exists == nil {
log.Println("No templates found in current directory.")

err = fetchTemplates()
if err != nil {
log.Println("Unable to download templates from Github.")
log.Println(err)
return
}
}

if len(services.Functions) > 0 {
for k, function := range services.Functions {
if function.SkipBuild {
Expand Down
9 changes: 9 additions & 0 deletions build.redist.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

docker build -f Dockerfile.redist --build-arg http_proxy=$http_proxy --build-arg https_proxy=$https_proxy -t faas-cli . && \
docker create --name faas-cli faas-cli && \
docker cp faas-cli:/root/faas-cli . && \
docker cp faas-cli:/root/faas-cli-darwin . && \
docker cp faas-cli:/root/faas-cli-armhf . && \
docker rm -f faas-cli

110 changes: 110 additions & 0 deletions fetchTemplates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package main

import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)

func fetchTemplates() error {

err := fetchMasterZip()

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

log.Printf("Attempting to expand templates from master.zip\n")

for _, z := range zipFile.File {
relativePath := strings.Replace(z.Name, "faas-cli-master/", "", -1)
if strings.Index(relativePath, "template") == 0 {
fmt.Printf("Found %s.\n", relativePath)
rc, err := z.Open()
if err != nil {
return err
}

err = createPath(relativePath, z.Mode())
if err != nil {
return err
}

// If relativePath is just a directory, then skip expanding it.
if len(relativePath) > 1 && relativePath[len(relativePath)-1:] != string(os.PathSeparator) {
err = writeFile(rc, z.UncompressedSize64, relativePath, z.Mode())
if err != nil {
return err
}
}
}
}

return err
}

func fetchMasterZip() 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"
}
c := http.Client{}

req, err := http.NewRequest("GET", templateURL, nil)
if err != nil {
log.Println(err.Error())
return err
}
log.Printf("HTTP GET %s\n", templateURL)
res, err := c.Do(req)
if err != nil {
log.Println(err.Error())
return err
}
if res.Body != nil {
defer res.Body.Close()
}
bytesOut, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err.Error())
return err
}

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

func writeFile(rc io.ReadCloser, size uint64, relativePath string, perms os.FileMode) error {
var err error

defer rc.Close()
fmt.Printf("Writing %d bytes to %s.\n", size, relativePath)
f, err := os.OpenFile(relativePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perms)
if err != nil {
return err
}
defer f.Close()
_, err = io.CopyN(f, rc, int64(size))

return err
}

func createPath(relativePath string, perms os.FileMode) error {
dir := filepath.Dir(relativePath)
err := os.MkdirAll(dir, perms)
return err
}
6 changes: 5 additions & 1 deletion get.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
version=0.3
#!/bin/sh

# Deployed to cli.get-faas.com

version=0.4

hasCli() {
has=$(which faas-cli)
Expand Down

0 comments on commit 340e34c

Please sign in to comment.