From 5370bc86fa056ca4d085083e058f09dceee26227 Mon Sep 17 00:00:00 2001 From: Travis Reeder Date: Thu, 20 Jul 2017 16:35:07 -0700 Subject: [PATCH] Added Go boilerplate --- cli/langs/go.go | 87 ++++++++++++++++++++++++++++++ examples/tutorial/hello/go/func.go | 1 - 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/cli/langs/go.go b/cli/langs/go.go index 264ea80cc..e74f2f368 100644 --- a/cli/langs/go.go +++ b/cli/langs/go.go @@ -1,5 +1,11 @@ package langs +import ( + "io/ioutil" + "os" + "path/filepath" +) + type GoLangHelper struct { BaseHelper } @@ -38,3 +44,84 @@ func (h *GoLangHelper) DockerfileCopyCmds() []string { func (lh *GoLangHelper) Entrypoint() string { return "./func" } + +// HasPreBuild returns whether the Java runtime has boilerplate that can be generated. +func (lh *GoLangHelper) HasBoilerplate() bool { return true } + +// GenerateBoilerplate will generate function boilerplate for a Java runtime. The default boilerplate is for a Maven +// project. +func (lh *GoLangHelper) GenerateBoilerplate() error { + wd, err := os.Getwd() + if err != nil { + return err + } + codeFile := filepath.Join(wd, "func.go") + if exists(codeFile) { + return ErrBoilerplateExists + } + testFile := filepath.Join(wd, "test.json") + if exists(testFile) { + return ErrBoilerplateExists + } + + if err := ioutil.WriteFile(codeFile, []byte(helloGoSrcBoilerplate), os.FileMode(0644)); err != nil { + return err + } + + if err := ioutil.WriteFile(testFile, []byte(testBoilerPlate), os.FileMode(0644)); err != nil { + return err + } + return nil +} + +const ( + helloGoSrcBoilerplate = `package main + +import ( + "encoding/json" + "fmt" + "os" +) + +type Person struct { + Name string +} + +func main() { + p := &Person{Name: "World"} + json.NewDecoder(os.Stdin).Decode(p) + mapD := map[string]string{"message": fmt.Sprintf("Hello %s", p.Name)} + mapB, _ := json.Marshal(mapD) + fmt.Println(string(mapB)) +} +` + + // Could use same test for most langs + testBoilerPlate = `{ + "tests": [ + { + "input": { + "body": { + "name": "Johnny" + } + }, + "output": { + "body": { + "message": "Hello Johnny" + } + } + }, + { + "input": { + "body": "" + }, + "output": { + "body": { + "message": "Hello World" + } + } + } + ] +} +` +) diff --git a/examples/tutorial/hello/go/func.go b/examples/tutorial/hello/go/func.go index adaac4144..c1f3f8401 100644 --- a/examples/tutorial/hello/go/func.go +++ b/examples/tutorial/hello/go/func.go @@ -14,7 +14,6 @@ type Person struct { func main() { p := &Person{Name: "World"} json.NewDecoder(os.Stdin).Decode(p) - // fmt.Printf("Hello %v!\n", p.Name) mapD := map[string]string{"message": fmt.Sprintf("Hello %s", p.Name)} mapB, _ := json.Marshal(mapD) fmt.Println(string(mapB))