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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 options.Namespace = defaultNamespace
} }
if len(options.Command) == 0 { if len(options.Command) == 0 {
ep, err := Entrypoint(s.Source)
if err != nil {
return err
}
options.Command = []string{"go"} options.Command = []string{"go"}
options.Args = []string{"run", "."} options.Args = []string{"run", ep}
} }
// pass secrets as env vars // pass secrets as env vars
@ -633,3 +638,42 @@ func (r *localRuntime) Stop() error {
func (r *localRuntime) String() string { func (r *localRuntime) String() string {
return "local" 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")
}
}

View File

@ -0,0 +1,33 @@
package local
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEntrypoint(t *testing.T) {
wd, _ := os.Getwd()
// test a service with idiomatic folder structure
result, err := Entrypoint(filepath.Join(wd, "test"))
assert.Nil(t, err, "Didn'expected entrypoint to return an error")
assert.Equal(t, "cmd/test/main.go", result, "Expected entrypoint to return cmd/test/main.go")
// test a service with a top level main.go
result, err = Entrypoint(filepath.Join(wd, "test/bar"))
assert.Nil(t, err, "Didn'expected entrypoint to return an error")
assert.Equal(t, "main.go", result, "Expected entrypoint to return main.go")
// test a service with multiple main.go files within the cmd folder
result, err = Entrypoint(filepath.Join(wd, "test/foo"))
assert.Error(t, err, "Expected entrypoint to return an error when multiple main.go files exist")
assert.Equal(t, "", result, "Expected entrypoint to not return a result")
// test a service with no main.go files
result, err = Entrypoint(filepath.Join(wd, "test/empty"))
assert.Error(t, err, "Expected entrypoint to return an error when no main.go files exist")
assert.Equal(t, "", result, "Expected entrypoint to not return a result")
}

View File

@ -0,0 +1,4 @@
// Package main is used to test the local runtime, specifically the entrypoint function
package main
func main() {}

View File

@ -0,0 +1,4 @@
// Package main is used to test the local runtime, specifically the entrypoint function
package main
func main() {}

View File

@ -0,0 +1 @@
package empty

View File

@ -0,0 +1,4 @@
// Package main is used to test the local runtime, specifically the entrypoint function
package main
func main() {}

View File

@ -0,0 +1,4 @@
// Package main is used to test the local runtime, specifically the entrypoint function
package main
func main() {}