Skip to content

Commit

Permalink
feat(planner/golang): Allow specifying the entry point of a Go applic…
Browse files Browse the repository at this point in the history
…ation (#403)
  • Loading branch information
pan93412 authored Jan 4, 2025
1 parent 80108ba commit 8e64801
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/golang/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ import (
"github.com/zeabur/zbpack/pkg/types"
)

// ConfigGoEntry specifies the entry point of the a Go application.
//
// You should specify a full path to the entry point file, for example,
// "cmd/server/main.go" or "app.go".
//
// If this key is not set, we discover it from "/main.go" and
// "/cmd/<submodule>/main.go"
const ConfigGoEntry = "go.entry"

type goPlanContext struct {
Src afero.Fs
Config plan.ImmutableProjectConfiguration
Expand Down Expand Up @@ -87,6 +96,11 @@ func getEntry(ctx *goPlanContext) string {
return entry
}

if entry, err := plan.Cast(ctx.Config.Get(ConfigGoEntry), cast.ToStringE).Take(); err == nil {
*ent = optional.Some(entry)
return ent.Unwrap()
}

// in a basic go project, we assume the entrypoint is main.go in root directory
if utils.HasFile(ctx.Src, "main.go") {
*ent = optional.Some("")
Expand Down
70 changes: 70 additions & 0 deletions internal/golang/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,76 @@ import (
"github.com/zeabur/zbpack/pkg/plan"
)

func TestGetEntry(t *testing.T) {
t.Parallel()

t.Run("without entry", func(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
config := plan.NewProjectConfigurationFromFs(fs, "test")

ctx := &goPlanContext{
Src: fs,
Config: config,
}

entry := getEntry(ctx)
assert.Equal(t, "", entry)
})

t.Run("with root main.go", func(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
config := plan.NewProjectConfigurationFromFs(fs, "test")
_ = afero.WriteFile(fs, "main.go", nil, 0o644)

ctx := &goPlanContext{
Src: fs,
Config: config,
SubmoduleName: "server",
}

entry := getEntry(ctx)
assert.Equal(t, "", entry)
})

t.Run("with submodule", func(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
config := plan.NewProjectConfigurationFromFs(fs, "test")

_ = afero.WriteFile(fs, "cmd/server/main.go", nil, 0o644)

ctx := &goPlanContext{
Src: fs,
Config: config,
SubmoduleName: "server",
}

entry := getEntry(ctx)
assert.Equal(t, "cmd/server/main.go", entry)
})

t.Run("with entry", func(t *testing.T) {
t.Parallel()

fs := afero.NewMemMapFs()
config := plan.NewProjectConfigurationFromFs(fs, "test")
config.Set(ConfigGoEntry, "cmd/server/main.go")

ctx := &goPlanContext{
Src: fs,
Config: config,
}

entry := getEntry(ctx)
assert.Equal(t, "cmd/server/main.go", entry)
})
}

func TestGetBuildCommand(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 8e64801

Please sign in to comment.