forked from fnproject/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby.go
148 lines (126 loc) · 2.95 KB
/
ruby.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package langs
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
type RubyLangHelper struct {
BaseHelper
}
func (h *RubyLangHelper) Handles(lang string) bool {
return defaultHandles(h, lang)
}
func (h *RubyLangHelper) Runtime() string {
return h.LangStrings()[0]
}
func (lh *RubyLangHelper) LangStrings() []string {
return []string{"ruby"}
}
func (lh *RubyLangHelper) Extensions() []string {
return []string{".rb"}
}
func (lh *RubyLangHelper) BuildFromImage() (string, error) {
return "fnproject/ruby:dev", nil
}
func (lh *RubyLangHelper) RunFromImage() (string, error) {
return "fnproject/ruby", nil
}
func (h *RubyLangHelper) DockerfileBuildCmds() []string {
r := []string{}
if exists("Gemfile") {
r = append(r,
"ADD Gemfile* /function/",
"RUN bundle install",
)
}
return r
}
func (h *RubyLangHelper) DockerfileCopyCmds() []string {
return []string{
"COPY --from=build-stage /usr/lib/ruby/gems/ /usr/lib/ruby/gems/", // skip this if no Gemfile? Does it matter?
"ADD . /function/",
}
}
func (lh *RubyLangHelper) Entrypoint() (string, error) {
return "ruby func.rb", nil
}
func (lh *RubyLangHelper) HasBoilerplate() bool { return true }
func (lh *RubyLangHelper) GenerateBoilerplate() error {
wd, err := os.Getwd()
if err != nil {
return err
}
msg := "%s already exists, can't generate boilerplate"
codeFile := filepath.Join(wd, "func.rb")
if exists(codeFile) {
return fmt.Errorf(msg, "func.rb")
}
gemFile := filepath.Join(wd, "Gemfile")
if exists(gemFile) {
return fmt.Errorf(msg, "Gemfile")
}
testFile := filepath.Join(wd, "test.json")
if exists(testFile) {
return fmt.Errorf(msg, "test.json")
}
if err := ioutil.WriteFile(codeFile, []byte(rubySrcBoilerplate), os.FileMode(0644)); err != nil {
return err
}
if err := ioutil.WriteFile(gemFile, []byte(rubyGemfileBoilerplate), os.FileMode(0644)); err != nil {
return err
}
if err := ioutil.WriteFile(testFile, []byte(rubyTestBoilerPlate), os.FileMode(0644)); err != nil {
return err
}
return nil
}
const (
rubySrcBoilerplate = `require 'json'
# Default value(s)
name = "World"
# Parse input
payload = STDIN.read
if payload != ""
payload = JSON.parse(payload)
name = payload['name']
end
# Print response
x = {message:"Hello #{name}"}
# STDERR.puts x.inspect
puts x.to_json
# Logging
STDERR.puts "---> STDERR goes to server logs"
`
rubyGemfileBoilerplate = `source 'https://rubygems.org'
gem 'json', '> 1.8.2'
`
// Could use same test for most langs
rubyTestBoilerPlate = `{
"tests": [
{
"input": {
"body": {
"name": "Johnny"
}
},
"output": {
"body": {
"message": "Hello Johnny"
}
}
},
{
"input": {
"body": ""
},
"output": {
"body": {
"message": "Hello World"
}
}
}
]
}
`
)