Skip to content

Commit 2686d47

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 98fc5fb commit 2686d47

File tree

10 files changed

+42
-9
lines changed

10 files changed

+42
-9
lines changed

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

Lines changed: 18 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"
@@ -32,6 +32,23 @@ type Function struct {
3232
log logging.Logger
3333
}
3434

35+
// FunctionOpt can modify a Function upon creation.
36+
type FunctionOpt func(f *Function)
37+
38+
// WithLogger adds a logger to a Function.
39+
func WithLogger(log logging.Logger) FunctionOpt {
40+
return func(f *Function) { f.log = log }
41+
}
42+
43+
// NewFunction creates a new Function with the given options.
44+
func NewFunction(opts ...FunctionOpt) *Function {
45+
f := &Function{}
46+
for _, opt := range opts {
47+
opt(f)
48+
}
49+
return f
50+
}
51+
3552
const (
3653
annotationKeyCompositionResourceName = "gotemplating.fn.crossplane.io/composition-resource-name"
3754
annotationKeyReady = "gotemplating.fn.crossplane.io/ready"

fn_test.go renamed to internal/fn/fn_test.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
"context"

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
"os"
File renamed without changes.

main.go

Lines changed: 3 additions & 1 deletion
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,7 +26,7 @@ func (c *CLI) Run() error {
2426
return err
2527
}
2628

27-
return function.Serve(&Function{log: log},
29+
return function.Serve(fn.NewFunction(fn.WithLogger(log)),
2830
function.Listen(c.Network, c.Address),
2931
function.MTLSCertificates(c.TLSCertsDir),
3032
function.Insecure(c.Insecure))

pkg/fn/fn.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
)

0 commit comments

Comments
 (0)