8000 feat: import command by jhoward-lm · Pull Request #113 · bomctl/bomctl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: import command #113

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

Merged
merged 5 commits into from
Aug 7, 2024
Merged
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
16 changes: 0 additions & 16 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/protobom/protobom/pkg/native/serializers"
"github.com/protobom/protobom/pkg/writer"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/bomctl/bomctl/internal/pkg/export"
"github.com/bomctl/bomctl/internal/pkg/options"
Expand Down Expand Up @@ -182,21 +181,6 @@ func parseFormat(fs, encoding string) (formats.Format, error) {
return format, nil
}

func preRun(opts *options.Options) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, _ []string) {
cfgFile, err := cmd.Flags().GetString("config")
cobra.CheckErr(err)

verbosity, err := cmd.Flags().GetCount("verbose")
cobra.CheckErr(err)

opts.
WithCacheDir(viper.GetString("cache_dir")).
WithConfigFile(cfgFile).
WithDebug(verbosity >= minDebugLevel)
}
}

func validateEncoding(fs, encoding string) error {
if !slices.Contains(encodingOptions()[fs], encoding) {
return errEncodingNotSupported
Expand Down
31 changes: 8 additions & 23 deletions cmd/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/bomctl/bomctl/internal/pkg/fetch"
"github.com/bomctl/bomctl/internal/pkg/options"
Expand All @@ -35,30 +34,16 @@ func fetchCmd() *cobra.Command {
}

outputFile := outputFileValue("")
sbomURLs := urlSliceValue{}

fetchCmd := &cobra.Command{
Use: "fetch [flags] SBOM_URL...",
Args: cobra.MinimumNArgs(1),
Short: "Fetch SBOM file(s) from HTTP(S), OCI, or Git URLs",
Long: "Fetch SBOM file(s) from HTTP(S), OCI, or Git URLs",
PreRun: func(_ *cobra.Command, args []string) {
sbomURLs = append(sbomURLs, args...)
},
Run: func(cmd *cobra.Command, _ []string) {
cfgFile, err := cmd.Flags().GetString("config")
cobra.CheckErr(err)

verbosity, err := cmd.Flags().GetCount("verbose")
cobra.CheckErr(err)

opts.
WithCacheDir(viper.GetString("cache_dir")).
WithConfigFile(cfgFile).
WithDebug(verbosity >= minDebugLevel)

Use: "fetch [flags] SBOM_URL...",
Args: cobra.MinimumNArgs(1),
Short: "Fetch SBOM file(s) from HTTP(S), OCI, or Git URLs",
Long: "Fetch SBOM file(s) from HTTP(S), OCI, or Git URLs",
PreRun: preRun(opts.Options),
Run: func(_ *cobra.Command, args []string) {
if string(outputFile) != "" {
if len(sbomURLs) > 1 {
if len(args) > 1 {
opts.Logger.Fatal("The --output-file option cannot be used when more than one URL is provided.")
}

Expand All @@ -72,7 +57,7 @@ func fetchCmd() *cobra.Command {
defer opts.OutputFile.Close()
}

for _, url := range sbomURLs {
for _, url := range args {
if err := fetch.Fetch(url, opts); err != nil {
opts.Logger.Fatal(err)
}
Expand Down
70 changes: 70 additions & 0 deletions cmd/import.go
10000
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ------------------------------------------------------------------------
// SPDX-FileCopyrightText: Copyright © 2024 bomctl a Series of LF Projects, LLC
// SPDX-FileName: cmd/import.go
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: Apache-2.0
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package cmd

import (
"os"
"slices"

"github.com/spf13/cobra"

imprt "github.com/bomctl/bomctl/internal/pkg/import"
"github.com/bomctl/bomctl/internal/pkg/options"
"github.com/bomctl/bomctl/internal/pkg/utils"
)

func importCmd() *cobra.Command {
opts := &imprt.ImportOptions{
Options: options.New(options.WithLogger(utils.NewLogger("import"))),
}

importCmd := &cobra.Command{
Use: "import [flags] { - | FILE...}",
Args: cobra.MinimumNArgs(1),
Short: "Import SBOM file(s) from stdin or local filesystem",
Long: "Import SBOM file(s) from stdin or local filesystem",
PreRun: preRun(opts.Options),
Run: func(_ *cobra.Command, args []string) {
if slices.Contains(args, "-") && len(args) > 1 {
opts.Logger.Fatal("Piped input and file path args cannot be specified simultaneously.")
}

for idx := range args {
if args[idx] == "-" {
opts.InputFiles = append(opts.InputFiles, os.Stdin)
} else {
file, err := os.Open(args[idx])
if err != nil {
opts.Logger.Fatal("failed to open input file", "err", err, "file", file)
}

opts.InputFiles = append(opts.InputFiles, file)

defer file.Close() //nolint:revive
}
}

if err := imprt.Import(opts); err != nil {
opts.Logger.Fatal(err)
}
},
}

return importCmd
}
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func listCmd() *cobra.Command {
documentIDs := []string{}

listCmd := &cobra.Command{
Use: "list",
Use: "list [flags] SBOM_ID...",
Aliases: []string{"ls"},
Short: "List SBOM documents in local cache",
Long: "List SBOM documents in local cache",
Expand Down
18 changes: 18 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/bomctl/bomctl/internal/pkg/options"
)

const (
Expand Down Expand Up @@ -111,12 +113,28 @@ func rootCmd() *cobra.Command {

rootCmd.AddCommand(exportCmd())
rootCmd.AddCommand(fetchCmd())
rootCmd.AddCommand(importCmd())
rootCmd.AddCommand(listCmd())
rootCmd.AddCommand(versionCmd())

return rootCmd
}

func preRun(opts *options.Options) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, _ []string) {
cfgFile, err := cmd.Flags().GetString("config")
cobra.CheckErr(err)

verbosity, err := cmd.Flags().GetCount("verbose")
cobra.CheckErr(err)

opts.
WithCacheDir(viper.GetString("cache_dir")).
WithConfigFile(cfgFile).
WithDebug(verbosity >= minDebugLevel)
}
}

func Execute() {
cobra.CheckErr(rootCmd().Execute())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/protobom/protobom v0.4.3
github.com/protobom/storage v0.1.3
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.9.0
oras.land/oras-go/v2 v2.5.0
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
github.com/spdx/tools-golang v0.5.4 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
Expand Down
70 changes: 70 additions & 0 deletions internal/pkg/import/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ------------------------------------------------------------------------
// SPDX-FileCopyrightText: Copyright © 2024 bomctl a Series of LF Projects, LLC
// SPDX-FileName: internal/pkg/import/import.go
// SPDX-FileType: SOURCE
// SPDX-License-Identifier: Apache-2.0
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package imprt

import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"

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

"github.com/bomctl/bomctl/internal/pkg/db"
"github.com/bomctl/bomctl/internal/pkg/options"
)

type ImportOptions struct {
*options.Options
InputFiles []*os.File
}

func Import(opts *ImportOptions) error {
backend := db.NewBackend().
Debug(opts.Debug).
WithDatabaseFile(filepath.Join(opts.CacheDir, db.DatabaseFile)).
WithLogger(opts.Logger)

if err := backend.InitClient(); err != nil {
return fmt.Errorf("failed to initialize backend client: %w", err)
}

defer backend.CloseClient()

sbomReader := reader.New()

for idx := range opts.InputFiles {
data, err := io.ReadAll(opts.InputFiles[idx])
if err != nil {
return fmt.Errorf("failed to read from %s: %w", opts.InputFiles[idx].Name(), err)
}

document, err := sbomReader.ParseStream(bytes.NewReader(data))
if err != nil {
return fmt.Errorf("failed to parse %s: %w", opts.InputFiles[idx].Name(), err)
}

if err := backend.AddDocument(document); err != nil {
return fmt.Errorf("failed to store document: %w", err)
}
}

return nil
}
0