Skip to content

Commit

Permalink
Added copyTree function and replaced the template copyFiles calls
Browse files Browse the repository at this point in the history
Signed-off-by: rgee0 <[email protected]>
  • Loading branch information
rgee0 committed Jul 23, 2017
1 parent ff3fd86 commit 444aad3
Showing 1 changed file with 55 additions and 14 deletions.
69 changes: 55 additions & 14 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 @@ -122,15 +123,15 @@ func main() {
}
} else {
if len(image) == 0 {
fmt.Println("Give a valid -image name for your Docker image.")
fmt.Println("Please provide a valid -image name for your Docker image.")
return
}
if len(handler) == 0 {
fmt.Println("Please give the full path to your function's handler.")
fmt.Println("Please provide the full path to your function's handler.")
return
}
if len(functionName) == 0 {
fmt.Println("Please give the deployed -name of your function")
fmt.Println("Please provide the deployed -name of your function.")
return
}
buildImage(image, handler, functionName, language, nocache)
Expand All @@ -151,11 +152,11 @@ func main() {
}
} else {
if len(image) == 0 {
fmt.Println("Give an image name to be deployed.")
fmt.Println("Please provide an image name to be deployed.")
return
}
if len(functionName) == 0 {
fmt.Println("Give a -name for your function as it will be deployed on FaaS")
fmt.Println("Please provide a -name for your function as it will be deployed on FaaS")
return
}

Expand Down Expand Up @@ -273,11 +274,11 @@ func buildImage(image string, handler string, functionName string, language stri
// createBuildTemplate creates temporary build folder to perform a Docker build with Node template
func createBuildTemplate(functionName string, handler string, language string) string {
tempPath := fmt.Sprintf("./build/%s/", functionName)
fmt.Printf("Clearing temporary folder: %s\n", tempPath)
fmt.Printf("Clearing temporary build folder: %s\n", tempPath)

clearErr := os.RemoveAll(tempPath)
if clearErr != nil {
fmt.Printf("Error clearing down temporary build folder %s\n", tempPath)
fmt.Printf("Error clearing temporary build folder %s\n", tempPath)
}

fmt.Printf("Preparing %s %s\n", handler+"/", tempPath+"function")
Expand All @@ -288,10 +289,8 @@ func createBuildTemplate(functionName string, handler string, language string) s
fmt.Printf("Error creating path %s - %s.\n", functionPath, mkdirErr.Error())
}

// TODO: index folders and copy everything from template, rather than set folders.
// Drop in template
copyFiles("./template/"+language, tempPath)
copyFiles("./template/"+language+"/function", tempPath+"function/")
// Drop in directory tree from template
copyTree("./template/"+language, tempPath)

// Overlay in user-function
copyFiles(handler, tempPath+"function/")
Expand All @@ -312,6 +311,48 @@ func copyFiles(path string, destination string) {
}
}

func copyTree(src string, destination string) {

files, err := ioutil.ReadDir(src)
if err != nil {
log.Fatal(err)
}

for _, file := range files {

if file.IsDir() == false {

cp(src+"/"+file.Name(), destination+file.Name())

} else {

//make new destination dir
newDir := destination + file.Name() + "/"
if !pathExists(newDir) {

newDirErr := os.Mkdir(newDir, 0700)

if err != nil {
fmt.Printf("Error creating path %s - %s.\n", newDir, newDirErr.Error())
}
}

//call copyTree to copy the contents
copyTree(src+"/"+file.Name(), newDir)
}
}
}

func pathExists(path string) bool {
exists := true

if _, err := os.Stat(path); os.IsNotExist(err) {
exists = false
}

return exists
}

func cp(src string, destination string) error {
fmt.Printf("cp - %s %s\n", src, destination)
memoryBuffer, readErr := ioutil.ReadFile(src)
Expand Down

0 comments on commit 444aad3

Please sign in to comment.