github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/core/function_generator.go (about)

     1  /*
     2   * Copyright 2018 the original author or authors.
     3   *
     4   *   Licensed under the Apache License, Version 2.0 (the "License");
     5   *   you may not use this file except in compliance with the License.
     6   *   You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *   Unless required by applicable law or agreed to in writing, software
    11   *   distributed under the License is distributed on an "AS IS" BASIS,
    12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *   See the License for the specific language governing permissions and
    14   *   limitations under the License.
    15   */
    16  
    17  package core
    18  
    19  import (
    20  	"github.com/projectriff/riff-cli/pkg/options"
    21  	"bytes"
    22  	"text/template"
    23  )
    24  
    25  //TODO: Kludgy '-' used to supress blank line, {{else}} adds a new line.
    26  var functionTemplate = `
    27  apiVersion: {{.ApiVersion}}
    28  kind: Function
    29  metadata:
    30    name: {{.Name}}
    31  spec:
    32    protocol: {{.Protocol}}
    33    input: {{.Input}}
    34  {{- if .Output}} 
    35    output: {{.Output}}
    36  {{ else }}{{ end }}
    37    container:
    38      image: {{.Image}}
    39  `
    40  
    41  func DefaultGenerateFunction(opts options.InitOptions) (string, error) {
    42  	function := Function{
    43  		ApiVersion: ApiVersion,
    44  		Name:       opts.FunctionName,
    45  		Input:      opts.Input,
    46  		Output:     opts.Output,
    47  		Protocol:   opts.Protocol,
    48  		Image:      options.ImageName(opts),
    49  	}
    50  
    51  	var tmpl *template.Template
    52  	var err error
    53  	var buffer bytes.Buffer
    54  
    55  	tmpl, err = template.New("function").Parse(functionTemplate)
    56  	if err != nil {
    57  		return "", err
    58  	}
    59  	err = tmpl.Execute(&buffer, function)
    60  	if err != nil {
    61  		return "", err
    62  	}
    63  	return buffer.String(), nil
    64  }
    65