forked from imjerrybao/apex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.go
49 lines (41 loc) · 1.14 KB
/
inference.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Package inference adds file-based inference to detect runtimes when they are not explicitly specified.
package inference
import (
"os"
"path/filepath"
"github.com/apex/apex/function"
"github.com/apex/apex/plugins/golang"
"github.com/apex/apex/plugins/java"
"github.com/apex/apex/plugins/nodejs"
"github.com/apex/apex/plugins/python"
)
func init() {
function.RegisterPlugin("inference", &Plugin{
Files: map[string]string{
"main.py": python.Runtime,
"index.js": nodejs.Runtime43,
"main.go": golang.Runtime,
"target/apex.jar": java.Runtime,
"build/libs/apex.jar": java.Runtime,
},
})
}
// Plugin implementation.
type Plugin struct {
Files map[string]string
}
// Open checks for files in the function directory to infer its runtime.
func (p *Plugin) Open(fn *function.Function) error {
if fn.Runtime != "" {
return nil
}
fn.Log.Debug("inferring runtime")
for name, runtime := range p.Files {
if _, err := os.Stat(filepath.Join(fn.Path, name)); err == nil {
fn.Log.WithField("runtime", runtime).Debug("inferred runtime")
fn.Runtime = runtime
return nil
}
}
return nil
}