Skip to content

Commit

Permalink
new cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis <[email protected]>
  • Loading branch information
alexellis committed Sep 4, 2017
1 parent 26cfe46 commit e013183
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 12 deletions.
10 changes: 5 additions & 5 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ func createBuildTemplate(functionName string, handler string, language string) s
}

// Drop in directory tree from template
copyFiles("./template/"+language, tempPath, true)
CopyFiles("./template/"+language, tempPath, true)

// Overlay in user-function
copyFiles(handler, tempPath+"function/", true)
CopyFiles(handler, tempPath+"function/", true)

return tempPath
}

func copyFiles(src string, destination string, recursive bool) {
func CopyFiles(src string, destination string, recursive bool) {

files, err := ioutil.ReadDir(src)
if err != nil {
Expand Down Expand Up @@ -106,8 +106,8 @@ func copyFiles(src string, destination string, recursive bool) {

//did the call ask to recurse into sub directories?
if recursive == true {
//call copyFiles to copy the contents
copyFiles(src+"/"+file.Name(), newDir, true)
//call CopyFiles to copy the contents
CopyFiles(src+"/"+file.Name(), newDir, true)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,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 @@ -109,7 +109,8 @@ func runBuild(cmd *cobra.Command, args []string) {

}

func pullTemplates() error {
// PullTemplates pulls templates from Github from the master zip download file.
func PullTemplates() error {
var err error
exists, err := os.Stat("./template")
if err != nil || exists == nil {
Expand Down
5 changes: 5 additions & 0 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func runDeploy(cmd *cobra.Command, args []string) {
return
}

// Override gateway if passed
if len(gateway) > 0 && gateway != "http://localhost:8080" {
parsedServices.Provider.GatewayURL = gateway
}

if parsedServices != nil {
services = *parsedServices
}
Expand Down
4 changes: 3 additions & 1 deletion commands/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ func runInvoke(cmd *cobra.Command, args []string) {
gatewayAddress = services.Provider.GatewayURL
}
}
if len(gatewayAddress) == 0 {

if len(gateway) > 0 && gateway != "http://localhost:8080" {
gatewayAddress = gateway
}

fmt.Fprintf(os.Stderr, "Reading from STDIN - hit (Control + D) to stop.\n")
functionInput, err := ioutil.ReadAll(os.Stdin)
if err != nil {
Expand Down
100 changes: 100 additions & 0 deletions commands/new_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Alex Ellis 2017. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

package commands

import (
"fmt"
"io/ioutil"
"os"

"github.com/alexellis/faas-cli/builder"
"github.com/spf13/cobra"
)

var (
lang string
list bool
)

func init() {
newFunctionCmd.Flags().StringVar(&functionName, "name", "", "Name for your function")
newFunctionCmd.Flags().StringVar(&lang, "lang", "", "Language or template to use")
newFunctionCmd.Flags().StringVar(&gateway, "gateway", "http://localhost:8080",
"Gateway URL or http://localhost:8080 to store in YAML stack file")

newFunctionCmd.Flags().BoolVar(&list, "list", false, "List available languages")

faasCmd.AddCommand(newFunctionCmd)
}

// newFunctionCmd displays newFunction information
var newFunctionCmd = &cobra.Command{
Use: "new [--name] [--lang] [--list]",
Short: "Create a new template in the current folder with the name given as name",
Long: fmt.Sprintf(`The new command creates a new function based upon hello-world in the
given language or type in --list for a list of languages available.`),
Example: ` faas-cli new
faas-cli new --name chatbot --lang node
faas-cli new --list`,
Run: runNewFunction,
}

func runNewFunction(cmd *cobra.Command, args []string) {
if list == true {
fmt.Printf(`Languages available as templates:
- node
- python
- ruby
- csharp
Or alternatively create a folder and a new Dockerfile, then pick
the "Dockerfile" lang type in your YAML file.
`)
return
}
if len(functionName) == 0 {
fmt.Printf("You must give a --function name\n")
return
}

if len(lang) == 0 {
fmt.Printf("You must give a --lang parameter\n")
return
}

PullTemplates()

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

if err := os.Mkdir("./"+functionName, 0700); err == nil {
fmt.Printf("Folder: %s created.\n", functionName)
}

builder.CopyFiles("./template/"+lang+"/function/", "./"+functionName+"/", true)

stack := `provider:
name: faas
gateway: ` + gateway + `
functions:
` + functionName + `:
lang: ` + lang + `
handler: ./` + functionName + `
image: ` + functionName + `
`

fmt.Printf("Function created in folder: %s\n", functionName)

stackWriteErr := ioutil.WriteFile("./"+functionName+".yml", []byte(stack), 0600)
if stackWriteErr != nil {
fmt.Printf("Error writing stack file %s\n", stackWriteErr)
} else {
fmt.Printf("Stack file written: %s\n", functionName+".yml")
}

return
}
14 changes: 10 additions & 4 deletions template/node/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@ WORKDIR /root/
ENV NPM_CONFIG_LOGLEVEL warn

# Wrapper/boot-strapper
COPY package.json .
COPY package.json .
RUN npm i

# Function
COPY index.js .
COPY function function
COPY index.js .
RUN mkdir -p ./root/function

COPY function/*.json ./function/
WORKDIR /root/function
RUN npm i || :
WORKDIR /root/
COPY function function
WORKDIR /root/function

WORKDIR /root/

ENV cgi_headers="true"
Expand All @@ -29,4 +35,4 @@ ENV fprocess="node index.js"

HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]
CMD ["fwatchdog"]

0 comments on commit e013183

Please sign in to comment.