Skip to content

Commit

Permalink
feat(planner): Implement ZBPACK_PLAN_TYPE flag (#417)
Browse files Browse the repository at this point in the history
#### Description (required)

```
pan93412@panmac2:/Volumes/Dev/Zeabur/zbpack/ > zbpack -i .         
2025/01/23 23:24:55 using submoduleName: zbpack

╔══════════════════════════════ Build Plan ═════════════════════════════╗
║ provider         │ go                                                 ║
║───────────────────────────────────────────────────────────────────────║
║ goVersion        │ 1.22.0                                             ║
║───────────────────────────────────────────────────────────────────────║
║ entry            │ cmd/zbpack/main.go                                 ║
╚═══════════════════════════════════════════════════════════════════════╝
pan93412@panmac2:/Volumes/Dev/Zeabur/zbpack/ > ZBPACK_PLAN_TYPE=nodejs zbpack -i .
2025/01/23 23:25:00 using submoduleName: zbpack
2025/01/23 23:25:00 Failed to read package.json: read file: read file package.json: open /Volumes/Dev/Zeabur/zbpack/package.json: no such file or directory
2025/01/23 23:25:00 invalid version empty version

╔══════════════════════════════ Build Plan ═════════════════════════════╗
║ provider         │ nodejs                                             ║
║───────────────────────────────────────────────────────────────────────║
║ packageManager   │ unknown                                            ║
║───────────────────────────────────────────────────────────────────────║
║ framework        │ none                                               ║
║───────────────────────────────────────────────────────────────────────║
║ nodeVersion      │ 20                                                 ║
║───────────────────────────────────────────────────────────────────────║
║ installCmd       │ COPY . .                                           ║
║                  │ RUN yarn install                                   ║
║───────────────────────────────────────────────────────────────────────║
║ startCmd         │ node index.js                                      ║
╚═══════════════════════════════════════════════════════════════════════╝
pan93412@panmac2:/Volumes/Dev/Zeabur/zbpack/ > ZBPACK_PLAN_TYPE=static zbpack -i .
2025/01/23 23:25:04 using submoduleName: zbpack

╔══════════════════════════════ Build Plan ═════════════════════════════╗
║ provider         │ static                                             ║
║───────────────────────────────────────────────────────────────────────║
║ serverless       │ true                                               ║
╚═══════════════════════════════════════════════════════════════════════╝
```

#### Related issues & labels (optional)

- Closes ZEA-4533
- Closes ZEA-4532
- Suggested label: enhancement
  • Loading branch information
yuaanlin authored Jan 24, 2025
2 parents 7849312 + 88e0a3a commit a3152da
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 a3152da

Please sign in to comment.