ent

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2024 License: Apache-2.0 Imports: 21 Imported by: 1

Documentation

Overview

-------------------------------------------------------------- SPDX-FileCopyrightText: Copyright © 2024 The Protobom Authors SPDX-FileType: SOURCE SPDX-License-Identifier: Apache-2.0 --------------------------------------------------------------

-------------------------------------------------------------- SPDX-FileCopyrightText: Copyright © 2024 The Protobom Authors SPDX-FileType: SOURCE SPDX-License-Identifier: Apache-2.0 --------------------------------------------------------------

-------------------------------------------------------------- SPDX-FileCopyrightText: Copyright © 2024 The Protobom Authors SPDX-FileType: SOURCE SPDX-License-Identifier: Apache-2.0 --------------------------------------------------------------

-------------------------------------------------------------- SPDX-FileCopyrightText: Copyright © 2024 The Protobom Authors SPDX-FileType: SOURCE SPDX-License-Identifier: Apache-2.0 --------------------------------------------------------------

-------------------------------------------------------------- SPDX-FileCopyrightText: Copyright © 2024 The Protobom Authors SPDX-FileType: SOURCE SPDX-License-Identifier: Apache-2.0 --------------------------------------------------------------

Example
package main

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"

	"github.com/protobom/protobom/pkg/reader"

	"github.com/protobom/storage/backends/ent"
)

func main() {
	cwd, err := os.Getwd()
	if err != nil {
		panic(err)
	}

	dbFile := filepath.Join(cwd, "example.db")

	// Remove example.db if it already exists.
	if _, err := os.Stat(dbFile); err == nil {
		os.Remove(dbFile)
	}

	rdr := reader.New()
	backend := ent.NewBackend().WithDatabaseFile(dbFile)

	if err := backend.InitClient(); err != nil {
		panic(err)
	}

	defer backend.CloseClient()

	sbom, err := rdr.ParseFile(filepath.Join(cwd, "testdata", "sbom.cdx.json"))
	if err != nil {
		panic(err)
	}

	if err := backend.Store(sbom, nil); err != nil {
		panic(err)
	}

	retrieved, err := backend.Retrieve(sbom.Metadata.Id, nil)
	if err != nil {
		panic(err)
	}

	output, err := json.MarshalIndent(retrieved, "", "  ")
	if err != nil {
		panic(err)
	}

	fmt.Println(string(output))

}
Output:

{
  "metadata": {
    "id": "urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79",
    "version": "1",
    "date": {}
  },
  "node_list": {
    "nodes": [
      {
        "id": "pkg:npm/acme/component@1.0.0",
        "name": "tomcat-catalina",
        "version": "9.0.14",
        "licenses": [
          "Apache-2.0"
        ],
        "license_concluded": "Apache-2.0",
        "identifiers": {
          "1": "pkg:npm/acme/component@1.0.0"
        },
        "hashes": {
          "1": "3942447fac867ae5cdb3229b658f4d48",
          "2": "e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a",
          "3": "f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b",
          "5": "e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282"
        },
        "primary_purpose": [
          16
        ]
      },
      {
        "id": "protobom-auto--000000001",
        "name": "Acme Application",
        "version": "9.1.1",
        "primary_purpose": [
          1
        ]
      },
      {
        "id": "protobom-auto--000000003",
        "name": "mylibrary",
        "version": "1.0.0",
        "primary_purpose": [
          16
        ]
      }
    ],
    "edges": [
      {
        "type": 5,
        "from": "protobom-auto--000000001",
        "to": [
          "pkg:npm/acme/component@1.0.0",
          "protobom-auto--000000003"
        ]
      }
    ],
    "root_elements": [
      "protobom-auto--000000001"
    ]
  }
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotation added in v0.1.5

type Annotation = ent.Annotation

Annotation is the model entity for the Annotation schema.

type Annotations added in v0.1.5

type Annotations = ent.Annotations

Annotations is a parsable slice of Annotation.

type Backend

type Backend struct {

	// Options is the set of options common to all ent Backends.
	Options *BackendOptions
	// contains filtered or unexported fields
}

Backend implements the protobom.pkg.storage.Backend interface.

func NewBackend

func NewBackend(opts ...Option) *Backend

func (*Backend) AddAnnotationToDocuments added in v0.1.4

func (backend *Backend) AddAnnotationToDocuments(name, value string, documentIDs ...string) error

AddAnnotationToDocuments applies a single named annotation value to multiple documents.

func (*Backend) AddAnnotations added in v0.1.4

func (backend *Backend) AddAnnotations(documentID, name string, values ...string) error

AddAnnotations applies multiple named annotation values to a single document.

func (*Backend) ClearAnnotations added in v0.1.4

func (backend *Backend) ClearAnnotations(documentIDs ...string) error

ClearAnnotations removes all annotations from the specified documents.

func (*Backend) CloseClient

func (backend *Backend) CloseClient()

func (*Backend) Debug

func (backend *Backend) Debug() *Backend

func (*Backend) GetDocumentAnnotations added in v0.1.4

func (backend *Backend) GetDocumentAnnotations(documentID string, names ...string) (ent.Annotations, error)

GetDocumentAnnotations gets all annotations for the specified document, limited to a set of annotation names if specified.

func (*Backend) GetDocumentUniqueAnnotation added in v0.1.5

func (backend *Backend) GetDocumentUniqueAnnotation(documentID, name string) (string, error)

GetDocumentUniqueAnnotation gets the value for a unique annotation.

func (*Backend) GetDocumentsByAnnotation added in v0.1.4

func (backend *Backend) GetDocumentsByAnnotation(name string, values ...string) ([]*sbom.Document, error)

GetDocumentsByAnnotation gets all documents having the specified named annotation, limited to a set of annotation values if specified.

func (*Backend) GetDocumentsByID added in v0.1.1

func (backend *Backend) GetDocumentsByID(ids ...string) ([]*sbom.Document, error)

func (*Backend) GetExternalReferencesByDocumentID added in v0.1.1

func (backend *Backend) GetExternalReferencesByDocumentID(
	id string, types ...string,
) ([]*sbom.ExternalReference, error)

func (*Backend) InitClient

func (backend *Backend) InitClient() error

func (*Backend) RemoveAnnotations added in v0.1.4

func (backend *Backend) RemoveAnnotations(documentID, name string, values ...string) error

RemoveAnnotations removes all annotations with the specified name from the document, limited to a set of annotation values if specified.

func (*Backend) Retrieve

func (backend *Backend) Retrieve(id string, _ *storage.RetrieveOptions) (doc *sbom.Document, err error)

Retrieve implements the storage.Retriever interface.

func (*Backend) SetAnnotations added in v0.1.4

func (backend *Backend) SetAnnotations(documentID, name string, values ...string) error

SetAnnotations explicitly sets the named annotations for the specified document.

func (*Backend) SetUniqueAnnotation added in v0.1.5

func (backend *Backend) SetUniqueAnnotation(documentID, name, value string) error

SetUniqueAnnotation sets a named annotation value that is unique to the specified document.

func (*Backend) Store

func (backend *Backend) Store(doc *sbom.Document, opts *storage.StoreOptions) error

Store implements the storage.Storer interface.

func (*Backend) WithAnnotation added in v0.1.5

func (backend *Backend) WithAnnotation(name, value string, unique bool) *Backend

func (*Backend) WithAnnotations added in v0.1.5

func (backend *Backend) WithAnnotations(annotations Annotations) *Backend

func (*Backend) WithBackendOptions

func (backend *Backend) WithBackendOptions(opts *BackendOptions) *Backend

func (*Backend) WithDatabaseFile

func (backend *Backend) WithDatabaseFile(file string) *Backend

type BackendOptions

type BackendOptions struct {
	// DatabaseFile is the file path of the SQLite database to be created.
	DatabaseFile string

	// Annotations is a slice of annotations to apply to stored document.
	Annotations

	// Debug configures the ent client to output all SQL statements during execution.
	Debug bool
}

BackendOptions contains options specific to the protobom ent backend.

func NewBackendOptions

func NewBackendOptions() *BackendOptions

NewBackendOptions creates a new BackendOptions for the backend.

type Option

type Option func(*Backend)

Option represents a single configuration option for the ent backend.

func Debug

func Debug() Option

func WithBackendOptions

func WithBackendOptions(opts *BackendOptions) Option

func WithDatabaseFile

func WithDatabaseFile(file string) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL