Added Go boilerplate

This commit is contained in:
Travis Reeder
2017-07-20 16:35:07 -07:00
parent 84bbfc0003
commit 5370bc86fa
2 changed files with 87 additions and 1 deletions

View File

@@ -1,5 +1,11 @@
package langs package langs
import (
"io/ioutil"
"os"
"path/filepath"
)
type GoLangHelper struct { type GoLangHelper struct {
BaseHelper BaseHelper
} }
@@ -38,3 +44,84 @@ func (h *GoLangHelper) DockerfileCopyCmds() []string {
func (lh *GoLangHelper) Entrypoint() string { func (lh *GoLangHelper) Entrypoint() string {
return "./func" 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"
}
}
}
]
}
`
)

View File

@@ -14,7 +14,6 @@ type Person struct {
func main() { func main() {
p := &Person{Name: "World"} p := &Person{Name: "World"}
json.NewDecoder(os.Stdin).Decode(p) json.NewDecoder(os.Stdin).Decode(p)
// fmt.Printf("Hello %v!\n", p.Name)
mapD := map[string]string{"message": fmt.Sprintf("Hello %s", p.Name)} mapD := map[string]string{"message": fmt.Sprintf("Hello %s", p.Name)}
mapB, _ := json.Marshal(mapD) mapB, _ := json.Marshal(mapD)
fmt.Println(string(mapB)) fmt.Println(string(mapB))