|
| 1 | +// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package deps implements a data source that extracts dependencies from |
| 5 | +// a filesystem or file. |
| 6 | +package deps |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/go-git/go-billy/v5/helper/iofs" |
| 14 | + |
| 15 | + mdeps "github.com/mindersec/minder/internal/deps" |
| 16 | + minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" |
| 17 | + v1datasources "github.com/mindersec/minder/pkg/datasources/v1" |
| 18 | +) |
| 19 | + |
| 20 | +type depsDataSourceHandler struct { |
| 21 | + extractor mdeps.Extractor |
| 22 | +} |
| 23 | + |
| 24 | +func newHandlerFromDef(def *minderv1.DepsDataSource_Def) (*depsDataSourceHandler, error) { |
| 25 | + if def == nil { |
| 26 | + return nil, errors.New("rest data source handler definition is nil") |
| 27 | + } |
| 28 | + |
| 29 | + return &depsDataSourceHandler{}, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (_ *depsDataSourceHandler) ValidateArgs(_ any) error { return nil } |
| 33 | + |
| 34 | +func (_ *depsDataSourceHandler) ValidateUpdate(_ any) error { return nil } |
| 35 | + |
| 36 | +func (h *depsDataSourceHandler) Call(ctx context.Context, _ any) (any, error) { |
| 37 | + // Extract the ingestion results from the context |
| 38 | + var ctxData v1datasources.Context |
| 39 | + var ok bool |
| 40 | + if ctxData, ok = ctx.Value(v1datasources.ContextKey{}).(v1datasources.Context); !ok { |
| 41 | + return nil, fmt.Errorf("unable to read execution context") |
| 42 | + } |
| 43 | + |
| 44 | + if ctxData.Ingest.Fs == nil { |
| 45 | + return nil, fmt.Errorf("filesystem not found in execution context") |
| 46 | + } |
| 47 | + |
| 48 | + nl, err := h.extractor.ScanFilesystem(ctx, iofs.New(ctxData.Ingest.Fs)) |
| 49 | + if err != nil { |
| 50 | + return nil, fmt.Errorf("scanning filesystem for dependencies: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + return nl, nil |
| 54 | +} |
0 commit comments