Skip to content

Commit f72823f

Browse files
committed
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 <[email protected]>
1 parent b1998a7 commit f72823f

File tree

10 files changed

+60
-19
lines changed

10 files changed

+60
-19
lines changed

fn.go renamed to internal/fn/fn.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package fn
22

33
import (
44
"bytes"
@@ -43,6 +43,35 @@ type Function struct {
4343
fsys fs.FS
4444
}
4545

46+
// FunctionOpt can modify a Function upon creation.
47+
type FunctionOpt func(f *Function)
48+
49+
// WithLogger adds a logger to a Function.
50+
func WithLogger(log logging.Logger) FunctionOpt {
51+
return func(f *Function) { f.log = log }
52+
}
53+
54+
// WithFilesystem adds a filesystem to a Function.
55+
func WithFileSystem(fsys fs.FS) FunctionOpt {
56+
return func(f *Function) { f.fsys = fsys }
57+
}
58+
59+
// NewFunction creates a new Function with the given options.
60+
func NewFunction(opts ...FunctionOpt) *Function {
61+
f := &Function{}
62+
for _, opt := range opts {
63+
opt(f)
64+
}
65+
// Set defaults
66+
if f.fsys == nil {
67+
f.fsys = &osFS{}
68+
}
69+
if f.log == nil {
70+
f.log = logging.NewNopLogger()
71+
}
72+
return f
73+
}
74+
4675
const (
4776
annotationKeyCompositionResourceName = "gotemplating.fn.crossplane.io/composition-resource-name"
4877
annotationKeyReady = "gotemplating.fn.crossplane.io/ready"

fn_test.go renamed to internal/fn/fn_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package fn
22

33
import (
44
"context"
@@ -11,8 +11,6 @@ import (
1111
"google.golang.org/protobuf/testing/protocmp"
1212
"google.golang.org/protobuf/types/known/durationpb"
1313

14-
"github.com/crossplane/crossplane-runtime/pkg/logging"
15-
1614
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
1715
"github.com/crossplane/function-sdk-go/resource"
1816
"github.com/crossplane/function-sdk-go/response"
@@ -591,10 +589,9 @@ func TestRunFunction(t *testing.T) {
591589

592590
for name, tc := range cases {
593591
t.Run(name, func(t *testing.T) {
594-
f := &Function{
595-
log: logging.NewNopLogger(),
596-
fsys: testdataFS,
597-
}
592+
f := NewFunction(
593+
WithFileSystem(testdataFS),
594+
)
598595
rsp, err := f.RunFunction(tc.args.ctx, tc.args.req)
599596

600597
if diff := cmp.Diff(tc.want.rsp, rsp, protocmp.Transform()); diff != "" {

function_maps.go renamed to internal/fn/function_maps.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package fn
22

33
import (
44
"fmt"
@@ -41,7 +41,7 @@ func GetNewTemplateWithFunctionMaps(delims *v1beta1.Delims) *template.Template {
4141
tpl.Funcs(f)
4242
}
4343
tpl.Funcs(template.FuncMap{
44-
"include": initInclude(tpl),
44+
"include": initInclude(tpl),
4545
})
4646
tpl.Funcs(sprig.FuncMap())
4747

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

106-
}
106+
}

function_maps_test.go renamed to internal/fn/function_maps_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package fn
22

33
import (
44
"bytes"
@@ -32,7 +32,7 @@ func Test_fromYaml(t *testing.T) {
3232
complexDictionary:
3333
scalar1: true
3434
list:
35-
- abc
35+
- abc
3636
- def`,
3737
},
3838
want: want{

template.go renamed to internal/fn/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package fn
22

33
import (
44
"io/fs"
File renamed without changes.

main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"github.com/alecthomas/kong"
66

77
"github.com/crossplane/function-sdk-go"
8+
9+
"github.com/crossplane-contrib/function-go-templating/pkg/fn"
810
)
911

1012
// CLI of this Function.
@@ -24,11 +26,7 @@ func (c *CLI) Run() error {
2426
return err
2527
}
2628

27-
return function.Serve(
28-
&Function{
29-
log: log,
30-
fsys: &osFS{},
31-
},
29+
return function.Serve(fn.NewFunction(fn.WithLogger(log)),
3230
function.Listen(c.Network, c.Address),
3331
function.MTLSCertificates(c.TLSCertsDir),
3432
function.Insecure(c.Insecure))

pkg/fn/fn.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Package fn defines the public interface for patch-and-transform functions.
2+
package fn
3+
4+
import (
5+
fninternal "github.com/crossplane-contrib/function-go-templating/internal/fn"
6+
)
7+
8+
var (
9+
// NewFunction creates a new Function with the given options.
10+
NewFunction = fninternal.NewFunction
11+
12+
// WithLogger adds a logger to a Function.
13+
WithLogger = fninternal.WithLogger
14+
15+
// WithFilesystem adds a filesystem to a Function.
16+
WithFileSystem = fninternal.WithFileSystem
17+
)

0 commit comments

Comments
 (0)