Skip to content

Commit

Permalink
feat(planner): Implement ZBPACK_PLAN_TYPE flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pan93412 committed Jan 23, 2025
1 parent 7849312 commit 88e0a3a
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions pkg/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package plan
import (
"strconv"

"github.com/samber/lo"
"github.com/spf13/afero"
"github.com/spf13/cast"
"github.com/zeabur/zbpack/pkg/types"
)

Expand Down Expand Up @@ -53,7 +55,31 @@ func Continue() types.PlanMeta {
return continuePlanMeta
}

const (
// ConfigKeyServerless is the key to enable serverless mode
// (ZBPACK_SERVERLESS, ZBPACK_FORCE_CONTAINERIZED)
ConfigKeyServerless = "serverless"
// ConfigKeyPlanType is the key to specify plan type explicitly.
// (ZBPACK_PLAN_TYPE)
ConfigKeyPlanType = "plan_type"
)

func (b planner) Plan() (types.PlanType, types.PlanMeta) {
planType, planTypeErr := Cast(b.NewPlannerOptions.Config.Get(ConfigKeyPlanType), cast.ToStringE).Take()
serverless := Cast(b.NewPlannerOptions.Config.Get(ConfigKeyServerless), ToWeakBoolE).TakeOr(true)

if planTypeErr == nil {
// find a identifier that matches the specified plan type
identifier, ok := lo.Find(b.identifiers, func(i Identifier) bool {
return i.PlanType() == types.PlanType(planType)
})

// if found, return the plan type and meta of this identifier
if ok {
return identifier.PlanType(), identifier.PlanMeta(b.NewPlannerOptions)
}
}

for _, identifier := range b.identifiers {
if identifier.Match(b.Source) {
pt, pm := identifier.PlanType(), identifier.PlanMeta(b.NewPlannerOptions)
Expand All @@ -67,7 +93,5 @@ func (b planner) Plan() (types.PlanType, types.PlanMeta) {
}
}

serverless := Cast(b.NewPlannerOptions.Config.Get("serverless"), ToWeakBoolE).TakeOr(true)

return types.PlanTypeStatic, types.PlanMeta{"serverless": strconv.FormatBool(serverless)}
}

0 comments on commit 88e0a3a

Please sign in to comment.