github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/core/function_artifacts_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  	"fmt"
    21  	"io/ioutil"
    22  	"path/filepath"
    23  	"strings"
    24  
    25  	"github.com/projectriff/riff-cli/pkg/options"
    26  	"github.com/projectriff/riff-cli/pkg/osutils"
    27  )
    28  
    29  const (
    30  	ApiVersion = "projectriff.io/v1"
    31  )
    32  
    33  type FunctionResources struct {
    34  	Topics       string
    35  	Function     string
    36  	DockerFile   string
    37  	DockerIgnore string
    38  }
    39  
    40  type Function struct {
    41  	ApiVersion string
    42  	Name       string
    43  	Input      string
    44  	Output     string
    45  	Image      string
    46  	Protocol   string
    47  }
    48  
    49  type ArtifactsGenerator struct {
    50  	GenerateFunction     func(options.InitOptions) (string, error)
    51  	GenerateDockerFile   func(options.InitOptions) (string, error)
    52  	GenerateDockerIgnore func(options.InitOptions) (string, error)
    53  }
    54  
    55  func GenerateFunctionArtifacts(generator ArtifactsGenerator, workdir string, opts options.InitOptions) error {
    56  	var functionResources FunctionResources
    57  	var err error
    58  	functionResources.Topics, err = createTopics(opts)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	functionResources.Function, err = generator.GenerateFunction(opts)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	functionResources.DockerFile, err = generator.GenerateDockerFile(opts)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	if generator.GenerateDockerIgnore != nil {
    71  		// optionally generate .dockerignore
    72  		functionResources.DockerIgnore, err = generator.GenerateDockerIgnore(opts)
    73  		if err != nil {
    74  			return err
    75  		}
    76  	}
    77  
    78  	if opts.DryRun {
    79  		fmt.Printf("%s-%s.yaml\n", opts.FunctionName, "topics")
    80  		fmt.Print("----")
    81  		fmt.Printf("%s", functionResources.Topics)
    82  		fmt.Print("----\n")
    83  		fmt.Printf("\n%s-%s.yaml\n", opts.FunctionName, "function")
    84  		fmt.Print("----")
    85  		fmt.Printf("%s", functionResources.Function)
    86  		fmt.Print("----\n")
    87  		fmt.Print("\nDockerfile\n")
    88  		fmt.Print("----")
    89  		fmt.Printf("%s", functionResources.DockerFile)
    90  		fmt.Print("----\n")
    91  		if generator.GenerateDockerIgnore != nil {
    92  			fmt.Print("\n.dockerignore\n")
    93  			fmt.Print("----")
    94  			fmt.Printf("%s", functionResources.DockerIgnore)
    95  			fmt.Print("----\n")
    96  		}
    97  		fmt.Println("")
    98  	} else {
    99  		var err error
   100  		err = writeFile(
   101  			filepath.Join(workdir,
   102  				fmt.Sprintf("%s-%s.yaml", opts.FunctionName, "topics")),
   103  			functionResources.Topics,
   104  			opts.Force)
   105  		if err != nil {
   106  			return err
   107  		}
   108  
   109  		err = writeFile(
   110  			filepath.Join(workdir,
   111  				fmt.Sprintf("%s-%s.yaml", opts.FunctionName, "function")),
   112  			functionResources.Function,
   113  			opts.Force)
   114  		if err != nil {
   115  			return err
   116  		}
   117  
   118  		err = writeFile(
   119  			filepath.Join(workdir, "Dockerfile"),
   120  			functionResources.DockerFile,
   121  			opts.Force)
   122  		if err != nil {
   123  			return err
   124  		}
   125  
   126  		if generator.GenerateDockerIgnore != nil {
   127  			err = writeFile(
   128  				filepath.Join(workdir, ".dockerignore"),
   129  				functionResources.DockerIgnore,
   130  				opts.Force)
   131  			if err != nil {
   132  				return err
   133  			}
   134  		}
   135  	}
   136  	return nil
   137  }
   138  
   139  func writeFile(filename string, text string, overwrite bool) error {
   140  	if !overwrite && osutils.FileExists(filename) {
   141  		fmt.Printf("Skipping existing file %s  - set --force to overwrite.\n", filename)
   142  		return nil
   143  
   144  	} else {
   145  		fmt.Printf("Initializing %s\n", filename)
   146  		return ioutil.WriteFile(filename, []byte(strings.TrimLeft(text, "\n")), 0644)
   147  	}
   148  }