Skip to content

Commit

Permalink
Merge pull request #166 from zeabur/pan93412/ZEA-2059
Browse files Browse the repository at this point in the history
feat(planner): Add Continue() flag
  • Loading branch information
yuaanlin authored Oct 24, 2023
2 parents 2e36b19 + 9d46b3c commit 73b9e75
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
8 changes: 8 additions & 0 deletions internal/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/zeabur/zbpack/pkg/plan"
)

func TestGenerateDockerFile(t *testing.T) {
Expand All @@ -20,3 +21,10 @@ func TestGenerateDockerFile(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, expectedContent, actualContent)
}

func TestNoMatchedDockerfile(t *testing.T) {
fs := afero.NewMemMapFs()
ctx := GetMeta(GetMetaOptions{Src: fs})

assert.Equal(t, plan.Continue(), ctx)
}
3 changes: 2 additions & 1 deletion internal/dockerfile/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/zeabur/zbpack/pkg/plan"
"golang.org/x/text/cases"

"github.com/moznion/go-optional"
Expand Down Expand Up @@ -161,7 +162,7 @@ func GetMeta(opt GetMetaOptions) types.PlanMeta {
dockerfileContent, err := ReadDockerfile(ctx)
if err != nil {
log.Println(err)
dockerfileContent = []byte{} // no Dockerfile
return plan.Continue()
}

exposePort := GetExposePort(ctx)
Expand Down
19 changes: 18 additions & 1 deletion pkg/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,27 @@ func NewPlanner(opt *NewPlannerOptions, identifiers ...Identifier) Planner {
}
}

var continuePlanMeta = types.PlanMeta{
"__INTERNAL_STATE": "CONTINUE",
}

// Continue is a pseudo PlanMeta, indicating the planner
// executor to find the next matched one.
func Continue() types.PlanMeta {
return continuePlanMeta
}

func (b planner) Plan() (types.PlanType, types.PlanMeta) {
for _, identifier := range b.identifiers {
if identifier.Match(b.Source) {
return identifier.PlanType(), identifier.PlanMeta(b.NewPlannerOptions)
pt, pm := identifier.PlanType(), identifier.PlanMeta(b.NewPlannerOptions)

// If the planner returns a Continue flag, we find the next matched.
if v, ok := pm["__INTERNAL_STATE"]; ok && v == "CONTINUE" {
continue
}

return pt, pm
}
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/plan/plan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package plan_test

import (
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/zeabur/zbpack/pkg/plan"
"github.com/zeabur/zbpack/pkg/types"
)

type alwaysMatchIdentifier struct {
meta types.PlanMeta
}

func (mi alwaysMatchIdentifier) PlanType() types.PlanType {
return ""
}

func (mi alwaysMatchIdentifier) Match(_ afero.Fs) bool {
return true
}

func (mi alwaysMatchIdentifier) PlanMeta(_ plan.NewPlannerOptions) types.PlanMeta {
return mi.meta
}

func TestPlan_Continue(t *testing.T) {
fs := afero.NewMemMapFs()

executor := plan.NewPlanner(
&plan.NewPlannerOptions{
Source: fs,
SubmoduleName: "",
},
alwaysMatchIdentifier{plan.Continue()},
alwaysMatchIdentifier{types.PlanMeta{"__INTERNAL_STATE": "TestPassed"}},
)

_, planMeta := executor.Plan()
v, ok := planMeta["__INTERNAL_STATE"]

assert.True(t, ok)
assert.Equal(t, "TestPassed", v)
}

0 comments on commit 73b9e75

Please sign in to comment.