@@ -23,20 +23,25 @@ import (
2323 "knative.dev/func/pkg/builders"
2424 "knative.dev/func/pkg/docker"
2525 fn "knative.dev/func/pkg/functions"
26+ "knative.dev/func/pkg/scaffolding"
2627)
2728
2829// DefaultName when no WithName option is provided to NewBuilder
2930const DefaultName = builders .Pack
3031
3132var DefaultBaseBuilder = "ghcr.io/knative/builder-jammy-base:latest"
33+
34+ // var DefaultGoBaseBuilder = "paketobuildpacks/builder-jammy-base:latest"
35+
3236var DefaultTinyBuilder = "ghcr.io/knative/builder-jammy-tiny:latest"
37+ var DefaultGoTinyBuilder = "paketobuildpacks/builder-jammy-tiny:latest"
3338
3439var (
3540 DefaultBuilderImages = map [string ]string {
3641 "node" : DefaultBaseBuilder ,
3742 "nodejs" : DefaultBaseBuilder ,
3843 "typescript" : DefaultBaseBuilder ,
39- "go" : DefaultTinyBuilder ,
44+ "go" : DefaultGoTinyBuilder ,
4045 "python" : DefaultBaseBuilder ,
4146 "quarkus" : DefaultTinyBuilder ,
4247 "rust" : DefaultBaseBuilder ,
@@ -134,7 +139,6 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
134139 if len (buildpacks ) == 0 {
135140 buildpacks = defaultBuildpacks [f .Runtime ]
136141 }
137-
138142 // Reading .funcignore file
139143 var excludes []string
140144 filePath := filepath .Join (f .Root , ".funcignore" )
@@ -171,6 +175,16 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
171175 Volumes []string
172176 }{Network : "" , Volumes : nil },
173177 }
178+
179+ // NOTE: gauron99 - this might be even created into a Client function and
180+ // ran before the client.Build() all together (in the CLI). There are gonna
181+ // be commonalitites across the builders for scaffolding with some nuances
182+ // which could be handled by each "scaffolder" - similarly to builders.
183+ // scaffold
184+ if err = scaffold (f ); err != nil {
185+ return
186+ }
187+
174188 if b .withTimestamp {
175189 now := time .Now ()
176190 opts .CreationTime = & now
@@ -186,6 +200,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
186200 opts .Env ["BPE_DEFAULT_LISTEN_ADDRESS" ] = "[::]:8080"
187201 }
188202
203+ if f .Runtime == "go" {
204+ if _ , ok := opts .Env ["BP_GO_WORKDIR" ]; ! ok {
205+ opts .Env ["BP_GO_WORKDIR" ] = ".func/builds/last"
206+ }
207+ }
208+
189209 var bindings = make ([]string , 0 , len (f .Build .Mounts ))
190210 for _ , m := range f .Build .Mounts {
191211 bindings = append (bindings , fmt .Sprintf ("%s:%s" , m .Source , m .Destination ))
@@ -312,3 +332,34 @@ type ErrRuntimeNotSupported struct {
312332func (e ErrRuntimeNotSupported ) Error () string {
313333 return fmt .Sprintf ("Pack builder has no default builder image for the '%v' language runtime. Please provide one." , e .Runtime )
314334}
335+
336+ // TODO: gauron99 - unify this with other builders; temporary for the go pack
337+ // to work for release.
338+ //
339+ // scaffold the project
340+ // Returns a config with settings suitable for building runtimes which
341+ // support scaffolding.
342+ func scaffold (f fn.Function ) error {
343+ // Scafffolding is currently only supported by the Go and Python runtimes
344+ if f .Runtime != "go" && f .Runtime != "python" {
345+ return nil
346+ }
347+
348+ contextDir := filepath .Join (".func" , "builds" , "last" )
349+ appRoot := filepath .Join (f .Root , contextDir )
350+ _ = os .RemoveAll (appRoot )
351+
352+ // The enbedded repository contains the scaffolding code itself which glues
353+ // together the middleware and a function via main
354+ embeddedRepo , err := fn .NewRepository ("" , "" ) // default is the embedded fs
355+ if err != nil {
356+ return fmt .Errorf ("unable to load the embedded scaffolding. %w" , err )
357+ }
358+
359+ // Write scaffolding to .func/builds/last
360+ err = scaffolding .Write (appRoot , f .Root , f .Runtime , f .Invoke , embeddedRepo .FS ())
361+ if err != nil {
362+ return fmt .Errorf ("unable to build due to a scaffold error. %w" , err )
363+ }
364+ return nil
365+ }
0 commit comments