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 22, 2024
1 parent 98fc5fb commit 2686d47
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 9 deletions.
19 changes: 18 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 @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion 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 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 (
"os"
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion 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,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))
Expand Down
14 changes: 14 additions & 0 deletions pkg/fn/fn.go
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit 2686d47

Please sign in to comment.