github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/langs/ruby.go (about)

     1  package langs
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  type RubyLangHelper struct {
    11  	BaseHelper
    12  }
    13  
    14  func (h *RubyLangHelper) Handles(lang string) bool {
    15  	return defaultHandles(h, lang)
    16  }
    17  func (h *RubyLangHelper) Runtime() string {
    18  	return h.LangStrings()[0]
    19  }
    20  
    21  func (h *RubyLangHelper) LangStrings() []string {
    22  	return []string{"ruby"}
    23  }
    24  func (h *RubyLangHelper) Extensions() []string {
    25  	return []string{".rb"}
    26  }
    27  func (h *RubyLangHelper) DefaultFormat() string {
    28  	return "http-stream"
    29  }
    30  func (h *RubyLangHelper) BuildFromImage() (string, error) {
    31  	return "fnproject/ruby:dev", nil
    32  }
    33  
    34  func (h *RubyLangHelper) RunFromImage() (string, error) {
    35  	return "fnproject/ruby", nil
    36  }
    37  
    38  func (h *RubyLangHelper) DockerfileBuildCmds() []string {
    39  	r := []string{}
    40  	if exists("Gemfile") {
    41  		r = append(r,
    42  			"ADD Gemfile* /function/",
    43  			"RUN bundle install",
    44  		)
    45  	}
    46  	return r
    47  }
    48  
    49  func (h *RubyLangHelper) DockerfileCopyCmds() []string {
    50  	return []string{
    51  		"COPY --from=build-stage /usr/lib/ruby/gems/ /usr/lib/ruby/gems/", // skip this if no Gemfile?  Does it matter?
    52  		"ADD . /function/",
    53  	}
    54  }
    55  
    56  func (h *RubyLangHelper) Entrypoint() (string, error) {
    57  	return "ruby func.rb", nil
    58  }
    59  
    60  func (h *RubyLangHelper) HasBoilerplate() bool { return true }
    61  
    62  func (h *RubyLangHelper) GenerateBoilerplate(path string) error {
    63  	msg := "%s already exists, can't generate boilerplate"
    64  	codeFile := filepath.Join(path, "func.rb")
    65  	if exists(codeFile) {
    66  		return fmt.Errorf(msg, "func.rb")
    67  	}
    68  	gemFile := filepath.Join(path, "Gemfile")
    69  	if exists(gemFile) {
    70  		return fmt.Errorf(msg, "Gemfile")
    71  	}
    72  
    73  	if err := ioutil.WriteFile(codeFile, []byte(rubySrcBoilerplate), os.FileMode(0644)); err != nil {
    74  		return err
    75  	}
    76  
    77  	if err := ioutil.WriteFile(gemFile, []byte(rubyGemfileBoilerplate), os.FileMode(0644)); err != nil {
    78  		return err
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  const (
    85  	rubySrcBoilerplate = `require 'fdk'
    86  
    87  def myfunction(context:, input:)
    88    input_value = input.respond_to?(:fetch) ? input.fetch('name') : input
    89    name = input_value.to_s.strip.empty? ? 'World' : input_value
    90    { message: "Hello #{name}!" }
    91  end
    92  
    93  FDK.handle(target: :myfunction)
    94  `
    95  
    96  	rubyGemfileBoilerplate = `source 'https://rubygems.org' do
    97    gem 'fdk', '~> 0.0.16'
    98  end
    99  `
   100  )