Skip to content

Commit

Permalink
Merge pull request #188 from zeabur/pan93412/ZEA-2417-zbpack-serverless
Browse files Browse the repository at this point in the history
ZEA-2417: ZBPACK_SERVERLESS=1 with zbpack.json
  • Loading branch information
Yuanlin Lin authored Jan 17, 2024
2 parents c132b9b + 63fb456 commit 458d2cb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 35 deletions.
1 change: 1 addition & 0 deletions internal/golang/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (i *identify) PlanMeta(options plan.NewPlannerOptions) types.PlanMeta {
return GetMeta(
GetMetaOptions{
Src: options.Source,
Config: options.Config,
SubmoduleName: options.SubmoduleName,
},
)
Expand Down
25 changes: 11 additions & 14 deletions internal/golang/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package golang

import (
"bufio"
"os"
"path"

"github.com/moznion/go-optional"
"github.com/spf13/afero"
"github.com/zeabur/zbpack/internal/utils"
"github.com/zeabur/zbpack/pkg/plan"
"github.com/zeabur/zbpack/pkg/types"
)

type goPlanContext struct {
Src afero.Fs
Src afero.Fs
Config plan.ImmutableProjectConfiguration

SubmoduleName string

GoVersion optional.Option[string]
Expand Down Expand Up @@ -80,26 +82,21 @@ func getEntry(ctx *goPlanContext) string {
// GetMetaOptions is the options for GetMeta.
type GetMetaOptions struct {
Src afero.Fs
Config plan.ImmutableProjectConfiguration
SubmoduleName string
}

func getServerless(ctx *goPlanContext) bool {
fcEnv := os.Getenv("FORCE_CONTAINERIZED")
if fcEnv == "true" || fcEnv == "1" {
return false
}

zsEnv := os.Getenv("ZBPACK_SERVERLESS")
if zsEnv == "true" || zsEnv == "1" {
return true
}

return false
return utils.GetExplicitServerlessConfig(ctx.Config).TakeOr(false)
}

// GetMeta gets the metadata of the Go project.
func GetMeta(opt GetMetaOptions) types.PlanMeta {
ctx := &goPlanContext{Src: opt.Src, SubmoduleName: opt.SubmoduleName}
ctx := &goPlanContext{
Src: opt.Src,
Config: opt.Config,
SubmoduleName: opt.SubmoduleName,
}
meta := types.PlanMeta{}

goVersion := getGoVersion(ctx)
Expand Down
11 changes: 2 additions & 9 deletions internal/nodejs/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nodejs
import (
"encoding/json"
"log"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -667,14 +666,8 @@ func GetStaticOutputDir(ctx *nodePlanContext) string {
}

func getServerless(ctx *nodePlanContext) bool {
fcEnv := os.Getenv("FORCE_CONTAINERIZED")
if fcEnv == "true" || fcEnv == "1" {
return false
}

zsEnv := os.Getenv("ZBPACK_SERVERLESS")
if zsEnv == "true" || zsEnv == "1" {
return true
if value, err := utils.GetExplicitServerlessConfig(ctx.Config).Take(); err == nil {
return value
}

sl := &ctx.Serverless
Expand Down
13 changes: 1 addition & 12 deletions internal/python/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -640,17 +639,7 @@ type GetMetaOptions struct {
}

func getServerless(ctx *pythonPlanContext) bool {
fcEnv := os.Getenv("FORCE_CONTAINERIZED")
if fcEnv == "true" || fcEnv == "1" {
return false
}

zsEnv := os.Getenv("ZBPACK_SERVERLESS")
if zsEnv == "true" || zsEnv == "1" {
return true
}

return false
return utils.GetExplicitServerlessConfig(ctx.Config).TakeOr(false)
}

// GetMeta returns the metadata of a Python project.
Expand Down
33 changes: 33 additions & 0 deletions internal/utils/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package utils

import (
"os"

"github.com/moznion/go-optional"
"github.com/spf13/cast"
"github.com/zeabur/zbpack/pkg/plan"
)

// GetExplicitServerlessConfig gets the serverless flag from the project configuration.
//
// When the serverless flag is not set, it will be determined by the FORCE_CONTAINERIZED
// and ZBPACK_SERVERLESS environment variables.
// If all of them are not set, it returns None for consumers to determine the default value.
func GetExplicitServerlessConfig(config plan.ImmutableProjectConfiguration) optional.Option[bool] {
fcEnv := os.Getenv("FORCE_CONTAINERIZED")
if fcEnv == "true" || fcEnv == "1" {
return optional.Some(true)
}

zsEnv := os.Getenv("ZBPACK_SERVERLESS")
if zsEnv == "true" || zsEnv == "1" {
return optional.Some(true)
}

serverlessConfig := plan.Cast(config.Get("serverless"), cast.ToBoolE)
if value, err := serverlessConfig.Take(); err == nil {
return optional.Some(value)
}

return optional.None[bool]()
}

0 comments on commit 458d2cb

Please sign in to comment.