Skip to content

Commit

Permalink
refactor: Move code from main into sub packages
Browse files Browse the repository at this point in the history
Move internal logic into `internal/fn` and expose constructor in
`pkg/fn` that can be importet in other modules.

Signed-off-by: Maximilian Blatt <maximilian.blatt-extern@deutschebahn.com>
  • Loading branch information
MisterMX committed Jan 24, 2024
1 parent b1998a7 commit f72823f
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 19 deletions.
31 changes: 30 additions & 1 deletion fn.go → internal/fn/fn.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package fn

import (
"bytes"
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 4 additions & 7 deletions fn_test.go → internal/fn/fn_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package fn

import (
"context"
Expand All @@ -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"
Expand Down Expand Up @@ -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 != "" {
Expand Down
6 changes: 3 additions & 3 deletions function_maps.go → internal/fn/function_maps.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package fn

import (
"fmt"
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -103,4 +103,4 @@ func initInclude(t *template.Template) func(string, interface{}) (string, error)
return buf.String(), err
}

}
}
4 changes: 2 additions & 2 deletions function_maps_test.go → internal/fn/function_maps_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package fn

import (
"bytes"
Expand Down Expand Up @@ -32,7 +32,7 @@ func Test_fromYaml(t *testing.T) {
complexDictionary:
scalar1: true
list:
- abc
- abc
- def`,
},
want: want{
Expand Down
2 changes: 1 addition & 1 deletion template.go → internal/fn/template.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package fn

import (
"io/fs"
Expand Down
File renamed without changes.
8 changes: 3 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down
17 changes: 17 additions & 0 deletions pkg/fn/fn.go
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit f72823f

Please sign in to comment.