From f72823f6dccda9a29b3f39802150a72caec96568 Mon Sep 17 00:00:00 2001 From: Maximilian Blatt Date: Mon, 22 Jan 2024 11:09:47 +0100 Subject: [PATCH] refactor: Move code from main into sub packages Move internal logic into `internal/fn` and expose constructor in `pkg/fn` that can be importet in other modules. Signed-off-by: Maximilian Blatt --- fn.go => internal/fn/fn.go | 31 ++++++++++++++++++- fn_test.go => internal/fn/fn_test.go | 11 +++---- .../fn/function_maps.go | 6 ++-- .../fn/function_maps_test.go | 4 +-- template.go => internal/fn/template.go | 2 +- .../templates/..shouldBeSkipped/_helpers.tpl | 0 .../templates/..shouldBeSkipped/resource.yaml | 0 .../fn/testdata}/templates/templates.yaml | 0 main.go | 8 ++--- pkg/fn/fn.go | 17 ++++++++++ 10 files changed, 60 insertions(+), 19 deletions(-) rename fn.go => internal/fn/fn.go (92%) rename fn_test.go => internal/fn/fn_test.go (99%) rename function_maps.go => internal/fn/function_maps.go (98%) rename function_maps_test.go => internal/fn/function_maps_test.go (99%) rename template.go => internal/fn/template.go (99%) rename {testdata => internal/fn/testdata}/templates/..shouldBeSkipped/_helpers.tpl (100%) rename {testdata => internal/fn/testdata}/templates/..shouldBeSkipped/resource.yaml (100%) rename {testdata => internal/fn/testdata}/templates/templates.yaml (100%) create mode 100644 pkg/fn/fn.go diff --git a/fn.go b/internal/fn/fn.go similarity index 92% rename from fn.go rename to internal/fn/fn.go index 1575ab6..ddb0ba0 100644 --- a/fn.go +++ b/internal/fn/fn.go @@ -1,4 +1,4 @@ -package main +package fn import ( "bytes" @@ -43,6 +43,35 @@ type Function struct { fsys fs.FS } +// FunctionOpt can modify a Function upon creation. +type FunctionOpt func(f *Function) + +// WithLogger adds a logger to a Function. +func WithLogger(log logging.Logger) FunctionOpt { + return func(f *Function) { f.log = log } +} + +// WithFilesystem adds a filesystem to a Function. +func WithFileSystem(fsys fs.FS) FunctionOpt { + return func(f *Function) { f.fsys = fsys } +} + +// NewFunction creates a new Function with the given options. +func NewFunction(opts ...FunctionOpt) *Function { + f := &Function{} + for _, opt := range opts { + opt(f) + } + // Set defaults + if f.fsys == nil { + f.fsys = &osFS{} + } + if f.log == nil { + f.log = logging.NewNopLogger() + } + return f +} + const ( annotationKeyCompositionResourceName = "gotemplating.fn.crossplane.io/composition-resource-name" annotationKeyReady = "gotemplating.fn.crossplane.io/ready" diff --git a/fn_test.go b/internal/fn/fn_test.go similarity index 99% rename from fn_test.go rename to internal/fn/fn_test.go index 6d69d0d..aa08099 100644 --- a/fn_test.go +++ b/internal/fn/fn_test.go @@ -1,4 +1,4 @@ -package main +package fn import ( "context" @@ -11,8 +11,6 @@ import ( "google.golang.org/protobuf/testing/protocmp" "google.golang.org/protobuf/types/known/durationpb" - "github.com/crossplane/crossplane-runtime/pkg/logging" - fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1" "github.com/crossplane/function-sdk-go/resource" "github.com/crossplane/function-sdk-go/response" @@ -591,10 +589,9 @@ func TestRunFunction(t *testing.T) { for name, tc := range cases { t.Run(name, func(t *testing.T) { - f := &Function{ - log: logging.NewNopLogger(), - fsys: testdataFS, - } + f := NewFunction( + WithFileSystem(testdataFS), + ) rsp, err := f.RunFunction(tc.args.ctx, tc.args.req) if diff := cmp.Diff(tc.want.rsp, rsp, protocmp.Transform()); diff != "" { diff --git a/function_maps.go b/internal/fn/function_maps.go similarity index 98% rename from function_maps.go rename to internal/fn/function_maps.go index fa180ec..9fd85ae 100644 --- a/function_maps.go +++ b/internal/fn/function_maps.go @@ -1,4 +1,4 @@ -package main +package fn import ( "fmt" @@ -41,7 +41,7 @@ func GetNewTemplateWithFunctionMaps(delims *v1beta1.Delims) *template.Template { tpl.Funcs(f) } tpl.Funcs(template.FuncMap{ - "include": initInclude(tpl), + "include": initInclude(tpl), }) tpl.Funcs(sprig.FuncMap()) @@ -103,4 +103,4 @@ func initInclude(t *template.Template) func(string, interface{}) (string, error) return buf.String(), err } -} \ No newline at end of file +} diff --git a/function_maps_test.go b/internal/fn/function_maps_test.go similarity index 99% rename from function_maps_test.go rename to internal/fn/function_maps_test.go index df631b9..ff38c41 100644 --- a/function_maps_test.go +++ b/internal/fn/function_maps_test.go @@ -1,4 +1,4 @@ -package main +package fn import ( "bytes" @@ -32,7 +32,7 @@ func Test_fromYaml(t *testing.T) { complexDictionary: scalar1: true list: - - abc + - abc - def`, }, want: want{ diff --git a/template.go b/internal/fn/template.go similarity index 99% rename from template.go rename to internal/fn/template.go index 706e51b..8dc017e 100644 --- a/template.go +++ b/internal/fn/template.go @@ -1,4 +1,4 @@ -package main +package fn import ( "io/fs" diff --git a/testdata/templates/..shouldBeSkipped/_helpers.tpl b/internal/fn/testdata/templates/..shouldBeSkipped/_helpers.tpl similarity index 100% rename from testdata/templates/..shouldBeSkipped/_helpers.tpl rename to internal/fn/testdata/templates/..shouldBeSkipped/_helpers.tpl diff --git a/testdata/templates/..shouldBeSkipped/resource.yaml b/internal/fn/testdata/templates/..shouldBeSkipped/resource.yaml similarity index 100% rename from testdata/templates/..shouldBeSkipped/resource.yaml rename to internal/fn/testdata/templates/..shouldBeSkipped/resource.yaml diff --git a/testdata/templates/templates.yaml b/internal/fn/testdata/templates/templates.yaml similarity index 100% rename from testdata/templates/templates.yaml rename to internal/fn/testdata/templates/templates.yaml diff --git a/main.go b/main.go index 8939db0..d0ce54c 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "github.com/alecthomas/kong" "github.com/crossplane/function-sdk-go" + + "github.com/crossplane-contrib/function-go-templating/pkg/fn" ) // CLI of this Function. @@ -24,11 +26,7 @@ func (c *CLI) Run() error { return err } - return function.Serve( - &Function{ - log: log, - fsys: &osFS{}, - }, + return function.Serve(fn.NewFunction(fn.WithLogger(log)), function.Listen(c.Network, c.Address), function.MTLSCertificates(c.TLSCertsDir), function.Insecure(c.Insecure)) diff --git a/pkg/fn/fn.go b/pkg/fn/fn.go new file mode 100644 index 0000000..560bdcb --- /dev/null +++ b/pkg/fn/fn.go @@ -0,0 +1,17 @@ +// Package fn defines the public interface for patch-and-transform functions. +package fn + +import ( + fninternal "github.com/crossplane-contrib/function-go-templating/internal/fn" +) + +var ( + // NewFunction creates a new Function with the given options. + NewFunction = fninternal.NewFunction + + // WithLogger adds a logger to a Function. + WithLogger = fninternal.WithLogger + + // WithFilesystem adds a filesystem to a Function. + WithFileSystem = fninternal.WithFileSystem +)