runtime/local: add support for idiomatic folder structures (#1963)

* runtime/local: add support for idiomatic folder structures

* runtime/local: add test coverage

* runtime/local: increase test coverage

* runtime/local: add test for empty local source

* runtime/local: make entrypoint public
This commit is contained in:
ben-toogood
2020-08-21 11:17:42 +01:00
committed by GitHub
parent f9f61d29de
commit 6cda6ef92e
7 changed files with 95 additions and 1 deletions

View File

@@ -286,8 +286,13 @@ func (r *localRuntime) Create(s *runtime.Service, opts ...runtime.CreateOption)
options.Namespace = defaultNamespace
}
if len(options.Command) == 0 {
ep, err := Entrypoint(s.Source)
if err != nil {
return err
}
options.Command = []string{"go"}
options.Args = []string{"run", "."}
options.Args = []string{"run", ep}
}
// pass secrets as env vars
@@ -633,3 +638,42 @@ func (r *localRuntime) Stop() error {
func (r *localRuntime) String() string {
return "local"
}
// Entrypoint determines the entrypoint for the service, since main.go doesn't always exist at
// the top level
func Entrypoint(dir string) (string, error) {
var entrypoints []string
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// get the relative path to the directory
rel, err := filepath.Rel(dir, path)
if err != nil {
return err
}
// only look for files in the top level or the cmd folder
if dir := filepath.Dir(rel); !filepath.HasPrefix(dir, "cmd") && dir != "." {
return nil
}
// only look for main.go files
if filepath.Base(rel) == "main.go" {
entrypoints = append(entrypoints, rel)
}
return nil
})
switch len(entrypoints) {
case 0:
return "", errors.New("No entrypoint found")
case 1:
return entrypoints[0], nil
default:
return "", errors.New("More than one entrypoint found")
}
}