Skip to content

Commit d1fa2e3

Browse files
committed
Store default source in the Function struct
Read the default source envvar as part of initialization in `main` and store it in the `Function` struct rather than using a global variable. This is tidier and lets us run our tests in parallel. Signed-off-by: Adam Wolfe Gordon <[email protected]>
1 parent 82780ae commit d1fa2e3

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

fn.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
"github.com/crossplane/function-sdk-go/response"
2727
)
2828

29-
var defaultSource = os.Getenv("FUNCTION_GO_TEMPLATING_DEFAULT_SOURCE")
30-
3129
// osFS is a dead-simple implementation of [io/fs.FS] that just wraps around
3230
// [os.Open].
3331
type osFS struct{}
@@ -40,8 +38,9 @@ func (*osFS) Open(name string) (fs.File, error) {
4038
type Function struct {
4139
fnv1.UnimplementedFunctionRunnerServiceServer
4240

43-
log logging.Logger
44-
fsys fs.FS
41+
log logging.Logger
42+
fsys fs.FS
43+
defaultSource string
4544
}
4645

4746
const (
@@ -62,10 +61,10 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1.RunFunctionRequest)
6261
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function input from %T", req))
6362
return rsp, nil
6463
}
65-
if in.Source == "" && defaultSource != "" {
64+
if in.Source == "" && f.defaultSource != "" {
6665
in.Source = v1beta1.FileSystemSource
6766
in.FileSystem = &v1beta1.TemplateSourceFileSystem{
68-
DirPath: defaultSource,
67+
DirPath: f.defaultSource,
6968
}
7069
}
7170

fn_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,10 @@ func TestRunFunction(t *testing.T) {
973973

974974
for name, tc := range cases {
975975
t.Run(name, func(t *testing.T) {
976-
// NOTE: This means we can't run tests in parallel.
977-
defaultSource = tc.args.defaultSource
978-
979976
f := &Function{
980-
log: logging.NewNopLogger(),
981-
fsys: testdataFS,
977+
log: logging.NewNopLogger(),
978+
fsys: testdataFS,
979+
defaultSource: tc.args.defaultSource,
982980
}
983981
rsp, err := f.RunFunction(tc.args.ctx, tc.args.req)
984982

main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
package main
33

44
import (
5+
"os"
6+
57
"github.com/alecthomas/kong"
68

79
"github.com/crossplane/function-sdk-go"
@@ -26,8 +28,9 @@ func (c *CLI) Run() error {
2628

2729
return function.Serve(
2830
&Function{
29-
log: log,
30-
fsys: &osFS{},
31+
log: log,
32+
fsys: &osFS{},
33+
defaultSource: os.Getenv("FUNCTION_GO_TEMPLATING_DEFAULT_SOURCE"),
3134
},
3235
function.Listen(c.Network, c.Address),
3336
function.MTLSCertificates(c.TLSCertsDir),

0 commit comments

Comments
 (0)