github.com/aneshas/cli@v0.0.0-20180104210444-aec958fa47db/langs/go.go (about) 1 package langs 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 ) 8 9 type GoLangHelper struct { 10 BaseHelper 11 } 12 13 func (h *GoLangHelper) Handles(lang string) bool { 14 return defaultHandles(h, lang) 15 } 16 func (h *GoLangHelper) Runtime() string { 17 return h.LangStrings()[0] 18 } 19 20 func (lh *GoLangHelper) LangStrings() []string { 21 return []string{"go"} 22 } 23 func (lh *GoLangHelper) Extensions() []string { 24 return []string{".go"} 25 } 26 27 func (lh *GoLangHelper) BuildFromImage() (string, error) { 28 return "fnproject/go:dev", nil 29 } 30 31 func (lh *GoLangHelper) RunFromImage() (string, error) { 32 return "fnproject/go", nil 33 } 34 35 func (h *GoLangHelper) DockerfileBuildCmds() []string { 36 r := []string{} 37 // more info on Go multi-stage builds: https://medium.com/travis-on-docker/multi-stage-docker-builds-for-creating-tiny-go-images-e0e1867efe5a 38 // For now we assume that dependencies are vendored already, but we could vendor them 39 // inside the container. Maybe we should check for /vendor dir and if it doesn't exist, 40 // either run `dep init` if no Gopkg.toml/lock found or `dep ensure` if it's there. 41 r = append(r, "ADD . /go/src/func/") 42 // if exists("Gopkg.toml") { 43 // r = append(r, 44 // "RUN go get -u github.com/golang/dep/cmd/dep", 45 // "RUN cd /src && dep ensure", 46 // ) 47 // } 48 r = append(r, "RUN cd /go/src/func/ && go build -o func") 49 return r 50 } 51 52 func (h *GoLangHelper) DockerfileCopyCmds() []string { 53 return []string{ 54 "COPY --from=build-stage /go/src/func/func /function/", 55 } 56 } 57 58 func (lh *GoLangHelper) Entrypoint() (string, error) { 59 return "./func", nil 60 } 61 62 func (lh *GoLangHelper) HasBoilerplate() bool { return true } 63 64 func (lh *GoLangHelper) GenerateBoilerplate() error { 65 wd, err := os.Getwd() 66 if err != nil { 67 return err 68 } 69 codeFile := filepath.Join(wd, "func.go") 70 if exists(codeFile) { 71 return ErrBoilerplateExists 72 } 73 testFile := filepath.Join(wd, "test.json") 74 if exists(testFile) { 75 return ErrBoilerplateExists 76 } 77 78 if err := ioutil.WriteFile(codeFile, []byte(helloGoSrcBoilerplate), os.FileMode(0644)); err != nil { 79 return err 80 } 81 82 if err := ioutil.WriteFile(testFile, []byte(goTestBoilerPlate), os.FileMode(0644)); err != nil { 83 return err 84 } 85 return nil 86 } 87 88 const ( 89 helloGoSrcBoilerplate = `package main 90 91 import ( 92 "encoding/json" 93 "fmt" 94 "os" 95 ) 96 97 type Person struct { 98 Name string 99 } 100 101 func main() { 102 p := &Person{Name: "World"} 103 json.NewDecoder(os.Stdin).Decode(p) 104 mapD := map[string]string{"message": fmt.Sprintf("Hello %s", p.Name)} 105 mapB, _ := json.Marshal(mapD) 106 fmt.Println(string(mapB)) 107 } 108 ` 109 110 // Could use same test for most langs 111 goTestBoilerPlate = `{ 112 "tests": [ 113 { 114 "input": { 115 "body": { 116 "name": "Johnny" 117 } 118 }, 119 "output": { 120 "body": { 121 "message": "Hello Johnny" 122 } 123 } 124 }, 125 { 126 "input": { 127 "body": "" 128 }, 129 "output": { 130 "body": { 131 "message": "Hello World" 132 } 133 } 134 } 135 ] 136 } 137 ` 138 )