github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/initializers.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 initializers
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"path/filepath"
    23  
    24  	"github.com/projectriff/riff-cli/pkg/initializers/java"
    25  	"github.com/projectriff/riff-cli/pkg/initializers/node"
    26  	"github.com/projectriff/riff-cli/pkg/initializers/python"
    27  	"github.com/projectriff/riff-cli/pkg/initializers/shell"
    28  	"github.com/projectriff/riff-cli/pkg/initializers/utils"
    29  	"github.com/projectriff/riff-cli/pkg/options"
    30  )
    31  
    32  var supportedExtensions = []string{"js", "json", "jar", "py", "sh"}
    33  
    34  type Initializer struct {
    35  	Initialize func(options.InitOptions) error
    36  }
    37  
    38  var languageForFileExtension = map[string]string{
    39  	"sh":   "shell",
    40  	"jar":  "java",
    41  	"js":   "node",
    42  	"json": "node",
    43  	"py":   "python",
    44  }
    45  
    46  func Java() Initializer {
    47  	return Initializer{
    48  		Initialize: java.Initialize,
    49  	}
    50  }
    51  
    52  func Python() Initializer {
    53  	return Initializer{
    54  		Initialize: python.Initialize,
    55  	}
    56  }
    57  func Node() Initializer {
    58  	return Initializer{
    59  		Initialize: node.Initialize,
    60  	}
    61  }
    62  func Shell() Initializer {
    63  	return Initializer{
    64  		Initialize: shell.Initialize,
    65  	}
    66  }
    67  
    68  func Initialize(opts options.InitOptions) error {
    69  	filePath, err := utils.ResolveFunctionFile(opts, "", "")
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	language := languageForFileExtension[filepath.Ext(filePath)[1:]]
    75  
    76  	switch language {
    77  	case "shell":
    78  		Shell().Initialize(opts)
    79  	case "node":
    80  		Node().Initialize(opts)
    81  	case "java":
    82  		return errors.New("Java resources detected. Use 'riff init java' to specify additional required options")
    83  	case "python":
    84  		return errors.New("Python resources detected. Use 'riff init python' to specify additional required options")
    85  	default:
    86  		//TODO: Should never get here
    87  		return errors.New(fmt.Sprintf("unsupported language %s\n", language))
    88  	}
    89  	return nil
    90  }