From 2686d479e64385bb48385b8727b72e4061d6b0c8 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 | 19 ++++++++++++++++++- fn_test.go => internal/fn/fn_test.go | 2 +- .../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 | 4 +++- pkg/fn/fn.go | 14 ++++++++++++++ 10 files changed, 42 insertions(+), 9 deletions(-) rename fn.go => internal/fn/fn.go (94%) 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 94% rename from fn.go rename to internal/fn/fn.go index 6400124..5a614cd 100644 --- a/fn.go +++ b/internal/fn/fn.go @@ -1,4 +1,4 @@ -package main +package fn import ( "bytes" @@ -32,6 +32,23 @@ type Function struct { log logging.Logger } +// 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 } +} + +// NewFunction creates a new Function with the given options. +func NewFunction(opts ...FunctionOpt) *Function { + f := &Function{} + for _, opt := range opts { + opt(f) + } + 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 de13b12..5bd125e 100644 --- a/fn_test.go +++ b/internal/fn/fn_test.go @@ -1,4 +1,4 @@ -package main +package fn import ( "context" 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 ccfa4c1..9755cef 100644 --- a/template.go +++ b/internal/fn/template.go @@ -1,4 +1,4 @@ -package main +package fn import ( "os" 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 88f0def..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,7 +26,7 @@ func (c *CLI) Run() error { return err } - return function.Serve(&Function{log: log}, + 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..6f56118 --- /dev/null +++ b/pkg/fn/fn.go @@ -0,0 +1,14 @@ +// 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 +)