Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply code style suggestions #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/cbomkit-theia.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cbomkit-theia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ func TestScan(t *testing.T) {
return a.Name < b.Name
}),
cmpopts.SortSlices(func(a cdx.Component, b cdx.Component) bool {
aHash := hash.HashCDXComponentWithoutRefs(a)
bHash := hash.HashCDXComponentWithoutRefs(b)
aHash := hash.CdxComponentWithoutRefs(a)
bHash := hash.CdxComponentWithoutRefs(b)
return hex.EncodeToString(aHash[:]) < hex.EncodeToString(bHash[:])
}),
cmpopts.SortSlices(func(a cdx.EvidenceOccurrence, b cdx.EvidenceOccurrence) bool {
Expand Down
9 changes: 6 additions & 3 deletions provider/cyclonedx/bom.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/xeipuuv/gojsonschema"
)

// Write bom to the file
// WriteBOM Write bom to the file
func WriteBOM(bom *cdx.BOM, writer io.Writer) error {
// Encode the BOM
err := cdx.NewBOMEncoder(writer, cdx.BOMFileFormatJSON).
Expand All @@ -40,7 +40,7 @@ func WriteBOM(bom *cdx.BOM, writer io.Writer) error {
return nil
}

// Parse and validate a CycloneDX BOM from path using the schema under schemaPath
// ParseBOM Parse and validate a CycloneDX BOM from path using the schema under schemaPath
func ParseBOM(bomReader io.Reader, schemaReader io.Reader) (*cdx.BOM, error) {
bomBytes, err := io.ReadAll(bomReader)
if err != nil {
Expand All @@ -62,7 +62,10 @@ func ParseBOM(bomReader io.Reader, schemaReader io.Reader) (*cdx.BOM, error) {
} else {
slog.Error("The BOM is not valid. see errors:")
for _, desc := range result.Errors() {
fmt.Fprintf(os.Stderr, "- %s\n", desc)
_, err = fmt.Fprintf(os.Stderr, "- %s\n", desc)
if err != nil {
slog.Error(err.Error())
}
}
return new(cdx.BOM), fmt.Errorf("provider: bom is not valid due to schema")
}
Expand Down
12 changes: 6 additions & 6 deletions provider/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type ActiveImage struct {
client *client.Client
}

// Defer to this function to destroy the ActiveImage after use
// TearDown Defer to this function to destroy the ActiveImage after use
func (image ActiveImage) TearDown() {
slog.Info("Removing Image", "id", image.id)

Expand All @@ -65,12 +65,12 @@ func (image ActiveImage) TearDown() {
}
}

// Get the image config of this image
// GetConfig Get the image config of this image
func (image ActiveImage) GetConfig() (config v1.Config, ok bool) {
return image.Metadata.Config.Config, true
}

// Build new image from a dockerfile;
// BuildNewImage Build new image from a dockerfile;
// Caller is responsible to call image.TearDown() after usage
func BuildNewImage(dockerfilePath string) (image ActiveImage, err error) {
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -138,7 +138,7 @@ func BuildNewImage(dockerfilePath string) (image ActiveImage, err error) {
}, err
}

// Parses a DockerImage from an identifier, possibly pulling it from a registry;
// GetPrebuiltImage Parses a DockerImage from an identifier, possibly pulling it from a registry;
// Caller is responsible to call image.TearDown() after usage
func GetPrebuiltImage(name string) (image ActiveImage, err error) {
slog.Info("Getting prebuilt image", "image", name)
Expand Down Expand Up @@ -176,12 +176,12 @@ func GetPrebuiltImage(name string) (image ActiveImage, err error) {
}, err
}

// Get a squashed filesystem at top layer
// GetSquashedFilesystem Get a squashed filesystem at top layer
func GetSquashedFilesystem(image ActiveImage) filesystem.Filesystem {
return GetSquashedFilesystemAtIndex(image, len(image.Layers)-1)
}

// Get a squashed filesystem at layer with index index
// GetSquashedFilesystemAtIndex Get a squashed filesystem at layer with index
func GetSquashedFilesystemAtIndex(image ActiveImage, index int) filesystem.Filesystem {
return Layer{
Layer: image.Layers[index],
Expand Down
14 changes: 7 additions & 7 deletions provider/docker/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ import (
"log/slog"

"github.com/IBM/cbomkit-theia/provider/filesystem"
scanner_errors "github.com/IBM/cbomkit-theia/scanner/errors"
scannererrors "github.com/IBM/cbomkit-theia/scanner/errors"

"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/filetree/filenode"
"github.com/anchore/stereoscope/pkg/image"
v1 "github.com/google/go-containerregistry/pkg/v1"
)

// Struct to represent a single layer in an ActiveImage
// Layer Struct to represent a single layer in an ActiveImage
type Layer struct { // implements Filesystem
*image.Layer
index int
image *ActiveImage
}

// Walk all files in the squashed layer using fn
// WalkDir Walk all files in the squashed layer using fn
func (layer Layer) WalkDir(fn filesystem.SimpleWalkDirFunc) error {
return layer.SquashedTree.Walk(
func(path file.Path, f filenode.FileNode) error {
Expand All @@ -48,7 +48,7 @@ func (layer Layer) WalkDir(fn filesystem.SimpleWalkDirFunc) error {

err := fn(string(path))

if errors.Is(err, scanner_errors.ErrParsingFailedAlthoughChecked) {
if errors.Is(err, scannererrors.ErrParsingFailedAlthoughChecked) {
slog.Warn(err.Error())
return nil
} else {
Expand All @@ -57,7 +57,7 @@ func (layer Layer) WalkDir(fn filesystem.SimpleWalkDirFunc) error {
}, nil)
}

// Read a file from this layer
// Open Read a file from this layer
func (layer Layer) Open(path string) (io.ReadCloser, error) {
readCloser, err := layer.OpenPathFromSquash(file.Path(path))
if err != nil {
Expand All @@ -67,12 +67,12 @@ func (layer Layer) Open(path string) (io.ReadCloser, error) {
return readCloser, err
}

// Get the image config
// GetConfig Get the image config
func (layer Layer) GetConfig() (config v1.Config, ok bool) {
return layer.image.GetConfig()
}

// Get a unique string for this layer in the image; can be used for logging etc.
// GetIdentifier Get a unique string for this layer in the image; can be used for logging etc.
func (layer Layer) GetIdentifier() string {
return fmt.Sprintf("Docker Image Layer (id:%v, layer:%v)", layer.image.id, layer.index)
}
20 changes: 10 additions & 10 deletions provider/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
package filesystem

import (
go_errors "errors"
goerrors "errors"
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"path/filepath"

scanner_errors "github.com/IBM/cbomkit-theia/scanner/errors"
scannererrors "github.com/IBM/cbomkit-theia/scanner/errors"

v1 "github.com/google/go-containerregistry/pkg/v1"
)

// A simple interface for a function to walk directories
// SimpleWalkDirFunc A simple interface for a function to walk directories
type SimpleWalkDirFunc func(path string) error

// Filesystem interface is mainly used to interact with all types of possible data source (e.g. directories, docker images etc.); for images this represents a squashed layer
Expand All @@ -41,19 +41,19 @@ type Filesystem interface {
GetIdentifier() string // Identifier for this specific filesystem; can be used for logging
}

// Simple plain filesystem that is constructed from the directory
// PlainFilesystem Simple plain filesystem that is constructed from the directory
type PlainFilesystem struct { // implements Filesystem
rootPath string
}

// Get a new PlainFilesystem from rootPath
// NewPlainFilesystem Get a new PlainFilesystem from rootPath
func NewPlainFilesystem(rootPath string) PlainFilesystem {
return PlainFilesystem{
rootPath: rootPath,
}
}

// Walk the whole PlainFilesystem using fn
// WalkDir Walk the whole PlainFilesystem using fn
func (plainFilesystem PlainFilesystem) WalkDir(fn SimpleWalkDirFunc) error {
return filepath.WalkDir(plainFilesystem.rootPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
Expand All @@ -72,7 +72,7 @@ func (plainFilesystem PlainFilesystem) WalkDir(fn SimpleWalkDirFunc) error {

err = fn(relativePath)

if go_errors.Is(err, scanner_errors.ErrParsingFailedAlthoughChecked) {
if goerrors.Is(err, scannererrors.ErrParsingFailedAlthoughChecked) {
slog.Warn(err.Error())
return nil
} else {
Expand All @@ -81,17 +81,17 @@ func (plainFilesystem PlainFilesystem) WalkDir(fn SimpleWalkDirFunc) error {
})
}

// Read a file from this filesystem; path should be relative to PlainFilesystem.rootPath
// Open Read a file from this filesystem; path should be relative to PlainFilesystem.rootPath
func (plainFilesystem PlainFilesystem) Open(path string) (io.ReadCloser, error) {
return os.Open(filepath.Join(plainFilesystem.rootPath, path))
}

// A plain directory does not have filesystem, so we return an empty object and false
// GetConfig A plain directory does not have filesystem, so we return an empty object and false
func (plainFilesystem PlainFilesystem) GetConfig() (config v1.Config, ok bool) {
return v1.Config{}, false
}

// Get a unique string for this PlainFilesystem; can be used for logging etc.
// GetIdentifier Get a unique string for this PlainFilesystem; can be used for logging etc.
func (plainFilesystem PlainFilesystem) GetIdentifier() string {
return fmt.Sprintf("Plain Filesystem (%v)", plainFilesystem.rootPath)
}
Expand Down
Loading
Loading