From 172846bdb582e70771f0cde442c12d1695727180 Mon Sep 17 00:00:00 2001 From: Luke Watts Date: Tue, 1 Oct 2024 09:54:31 +0200 Subject: [PATCH] feat: adds logger to cue transformation --- internal/cueutils/transformer.go | 29 +++++++++++++++++-- .../data_transformation_workflow.go | 7 +++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/internal/cueutils/transformer.go b/internal/cueutils/transformer.go index dbceadb8..790c07d0 100644 --- a/internal/cueutils/transformer.go +++ b/internal/cueutils/transformer.go @@ -11,6 +11,7 @@ import ( "cuelang.org/go/cue" "cuelang.org/go/cue/ast" "cuelang.org/go/cue/load" + "github.com/rs/zerolog" ) const ( @@ -21,12 +22,14 @@ const ( ) type Transformer struct { - inst cue.Value + inst cue.Value + logger *zerolog.Logger } -func NewTransformer(ctx *cue.Context, name string) (*Transformer, error) { +func NewTransformer(ctx *cue.Context, name string, options ...OptionFunc) (*Transformer, error) { var devnull bytes.Buffer overlay := map[string]load.Source{} + err := fs.WalkDir(EmbeddedFilesystem, ".", func(path string, d fs.DirEntry, err error) error { if err != nil { return err @@ -56,7 +59,19 @@ func NewTransformer(ctx *cue.Context, name string) (*Transformer, error) { if err = inst.Err(); err != nil { return nil, fmt.Errorf("failed to instanstiate transforms: %w", err) } - return &Transformer{inst: inst}, nil + + t := &Transformer{ + inst: inst, + logger: &zerolog.Logger{}, + } + + for _, option := range options { + option(t) + } + + t.logger.Debug().Msgf("NewTransformer: %s", name) + + return t, nil } func (t *Transformer) Apply(input ast.Expr) (cue.Value, error) { @@ -84,3 +99,11 @@ func (t *Transformer) ApplyValue(v cue.Value) (cue.Value, error) { } return withOutput, nil } + +type OptionFunc func(*Transformer) + +func WithLogger(logger *zerolog.Logger) OptionFunc { + return func(t *Transformer) { + t.logger = logger + } +} diff --git a/pkg/local_workflows/data_transformation_workflow.go b/pkg/local_workflows/data_transformation_workflow.go index ca17b15d..4681f7fd 100644 --- a/pkg/local_workflows/data_transformation_workflow.go +++ b/pkg/local_workflows/data_transformation_workflow.go @@ -7,6 +7,7 @@ import ( "cuelang.org/go/cue/cuecontext" cuejson "cuelang.org/go/pkg/encoding/json" + "github.com/rs/zerolog" cueutil "github.com/snyk/go-application-framework/internal/cueutils" "github.com/snyk/go-application-framework/pkg/configuration" "github.com/snyk/go-application-framework/pkg/local_workflows/content_type" @@ -60,7 +61,7 @@ func dataTransformationEntryPoint(invocationCtx workflow.InvocationContext, inpu logger.Trace().Msg("incomplete input data for transformation") return output, nil } - findingsModel, err = transformSarifData(sarifInput) + findingsModel, err = transformSarifData(sarifInput, logger) if err != nil { logger.Err(err).Msg(err.Error()) return output, err @@ -90,7 +91,7 @@ func dataTransformationEntryPoint(invocationCtx workflow.InvocationContext, inpu return output, nil } -func transformSarifData(singleData workflow.Data) (localFinding local_models.LocalFinding, err error) { +func transformSarifData(singleData workflow.Data, logger *zerolog.Logger) (localFinding local_models.LocalFinding, err error) { jsonData, ok := singleData.GetPayload().([]byte) if !ok { return localFinding, err @@ -102,7 +103,7 @@ func transformSarifData(singleData workflow.Data) (localFinding local_models.Loc } ctx := cuecontext.New() - sarif2apiTransformer, transformerError := cueutil.NewTransformer(ctx, cueutil.ToTestApiFromSarif) + sarif2apiTransformer, transformerError := cueutil.NewTransformer(ctx, cueutil.ToTestApiFromSarif, cueutil.WithLogger(logger)) if transformerError != nil { return localFinding, transformerError }