github.com/projectriff/riff-cli@v0.0.5-0.20180301104501-5db7a3bd9fc1/pkg/initializers/utils/function_file.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 utils 18 19 import ( 20 "errors" 21 "fmt" 22 "path/filepath" 23 24 "github.com/projectriff/riff-cli/pkg/options" 25 "github.com/projectriff/riff-cli/pkg/osutils" 26 ) 27 28 var supportedExtensions = []string{"js", "json", "java", "py", "sh"} 29 30 var languageForFileExtensions = map[string]string{ 31 "sh": "shell", 32 "jar": "java", 33 "js": "node", 34 "json": "node", 35 "py": "python", 36 } 37 38 //Assumes given file paths have been sanity checked and are valid 39 func ResolveFunctionFile(opts options.InitOptions, language string, ext string) (string, error) { 40 41 absFilePath, err := filepath.Abs(opts.FilePath) 42 if err != nil { 43 return "", err 44 } 45 46 var resolvedFilePath string 47 var functionDir string 48 var functionFile string 49 if osutils.IsDirectory(absFilePath) { 50 if opts.Artifact == "" { 51 functionFile = opts.FunctionName 52 functionDir = absFilePath 53 if ext != "" { 54 resolvedFilePath = filepath.Join(functionDir, fmt.Sprintf("%s.%s", functionFile, ext)) 55 } else { 56 functionFile, err = searchForFunctionResource(functionDir, opts.FunctionName) 57 if err != nil { 58 return "", err 59 } 60 resolvedFilePath = functionFile 61 } 62 } else { 63 resolvedFilePath = filepath.Join(absFilePath, opts.Artifact) 64 } 65 } else { 66 resolvedFilePath = absFilePath 67 } 68 if !osutils.FileExists(resolvedFilePath) { 69 return "", errors.New(fmt.Sprintf("function path %s does not exist", resolvedFilePath)) 70 } 71 72 if opts.Artifact != "" && language != "" && languageForFileExtensions[filepath.Ext(resolvedFilePath)[1:]] != language { 73 return "", errors.New(fmt.Sprintf("language %s conflicts with artifact file extension %s", language, opts.Artifact)) 74 } 75 76 return resolvedFilePath, nil 77 } 78 79 func searchForFunctionResource(dir string, name string) (string, error) { 80 files, err := filepath.Glob(filepath.Join(dir, "*")) 81 if err != nil { 82 return "", err 83 } 84 85 foundFile := "" 86 for _, f := range files { 87 if b := filepath.Base(f); b[0:len(b)-len(filepath.Ext(f))] == name { 88 for _, e := range supportedExtensions { 89 if filepath.Ext(f) == "."+e { 90 if foundFile == "" { 91 foundFile = f 92 } else { 93 return "", errors.New(fmt.Sprintf("function file is not unique %s, %s", filepath.Base(foundFile), filepath.Base(f))) 94 } 95 } 96 } 97 } 98 99 } 100 101 if foundFile == "" { 102 return "", errors.New(fmt.Sprintf("no function file found in path %s", dir)) 103 } 104 return foundFile, nil 105 }