-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Export command - Export SBOM from storage #75
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
idunbarh
merged 15 commits into
bomctl:main
from
scribe-security:suggestion/save-command
Jun 26, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7f73dab
temporary Save command - Save SBOM to File By ID
houdini91 a4999b4
save to export command
houdini91 307d6a0
tidy
houdini91 8a2f85e
save to export command
houdini91 cde5308
save to export command
houdini91 e2c4437
save to export command
houdini91 0d1c421
allow multiple sbomIDs
houdini91 cd4a6a6
Update internal/pkg/utils/format/format_test.go
houdini91 ae10243
Update cmd/export.go
houdini91 52e3d37
Update internal/pkg/utils/format/format.go
houdini91 588d33b
fix lint
houdini91 5375d40
normalize to list PR
houdini91 db96d08
inital test export
houdini91 7d2049d
Export command squashed
houdini91 d1aa01b
Export command squashed
houdini91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// ------------------------------------------------------------------------ | ||
// SPDX-FileCopyrightText: Copyright © 2024 bomctl authors | ||
// SPDX-FileName: cmd/export.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" | ||
"path/filepath" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/bomctl/bomctl/internal/pkg/db" | ||
"github.com/bomctl/bomctl/internal/pkg/export" | ||
"github.com/bomctl/bomctl/internal/pkg/utils" | ||
"github.com/bomctl/bomctl/internal/pkg/utils/format" | ||
) | ||
|
||
func exportCmd() *cobra.Command { | ||
documentIDs := []string{} | ||
opts := &export.ExportOptions{ | ||
Logger: utils.NewLogger("export"), | ||
} | ||
|
||
outputFile := OutputFileValue("") | ||
formatString := FormatStringValue(format.DefaultFormatString()) | ||
formatEncoding := FormatEncodingValue(format.DefaultEncoding()) | ||
|
||
exportCmd := &cobra.Command{ | ||
Use: "export [flags] SBOM_URL...", | ||
Args: cobra.MinimumNArgs(1), | ||
Short: "Export SBOM file(s) from Storage", | ||
Long: "Export SBOM file(s) from Storage", | ||
PreRun: func(_ *cobra.Command, args []string) { | ||
documentIDs = append(documentIDs, args...) | ||
}, | ||
Run: func(cmd *cobra.Command, _ []string) { | ||
cfgFile, err := cmd.Flags().GetString("config") | ||
cobra.CheckErr(err) | ||
|
||
idunbarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
initOpts(opts, cfgFile, string(formatString), string(formatEncoding)) | ||
backend := initBackend(opts) | ||
|
||
if string(outputFile) != "" { | ||
if len(documentIDs) > 1 { | ||
opts.Logger.Fatal("The --output-file option cannot be used when more than one SBOM is provided.") | ||
} | ||
|
||
out, err := os.Create(string(outputFile)) | ||
if err != nil { | ||
opts.Logger.Fatal("error creating output file", "outputFile", outputFile) | ||
} | ||
|
||
opts.OutputFile = out | ||
|
||
defer opts.OutputFile.Close() | ||
} | ||
Export(documentIDs, opts, backend) | ||
}, | ||
} | ||
|
||
exportCmd.Flags().VarP( | ||
&outputFile, | ||
"output-file", | ||
"o", | ||
"Path to output file", | ||
) | ||
exportCmd.Flags().VarP( | ||
&formatString, | ||
"format", | ||
"f", | ||
format.FormatStringOptions) | ||
exportCmd.Flags().VarP( | ||
&formatEncoding, | ||
"encoding", | ||
"e", | ||
"the output encoding [spdx: [text, json] cyclonedx: [json]") | ||
idunbarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return exportCmd | ||
} | ||
|
||
func Export(documentIDs []string, opts *export.ExportOptions, backend *db.Backend) { | ||
for _, id := range documentIDs { | ||
if err := export.Export(id, opts, backend); err != nil { | ||
opts.Logger.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func initOpts(opts *export.ExportOptions, cfgFile, formatString, formatEncoding string) { | ||
opts.CacheDir = viper.GetString("cache_dir") | ||
opts.ConfigFile = cfgFile | ||
opts.FormatString = formatString | ||
opts.Encoding = formatEncoding | ||
} | ||
|
||
func initBackend(opts *export.ExportOptions) *db.Backend { | ||
backend := db.NewBackend(func(b *db.Backend) { | ||
b.Options.DatabaseFile = filepath.Join(opts.CacheDir, db.DatabaseFile) | ||
b.Logger = utils.NewLogger("export") | ||
}) | ||
idunbarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if err := backend.InitClient(); err != nil { | ||
backend.Logger.Fatalf("failed to initialize backend client: %v", err) | ||
} | ||
|
||
return backend | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// ------------------------------------------------------------------------ | ||
// SPDX-FileCopyrightText: Copyright © 2024 bomctl authors | ||
// SPDX-FileName: internal/pkg/export/export.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 export | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/charmbracelet/log" | ||
"github.com/protobom/protobom/pkg/writer" | ||
|
||
"github.com/bomctl/bomctl/internal/pkg/db" | ||
"github.com/bomctl/bomctl/internal/pkg/utils" | ||
"github.com/bomctl/bomctl/internal/pkg/utils/format" | ||
) | ||
|
||
type ( | ||
ExportOptions struct { | ||
Logger *log.Logger | ||
OutputFile *os.File | ||
FormatString string | ||
Encoding string | ||
CacheDir string | ||
ConfigFile string | ||
} | ||
idunbarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
func Export(sbomID string, opts *ExportOptions, backend *db.Backend) error { | ||
logger := utils.NewLogger("export") | ||
|
||
logger.Info(fmt.Sprintf("Exporting %s SBOM ID", sbomID)) | ||
idunbarh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
parsedFormat, err := format.Parse(opts.FormatString, opts.Encoding) | ||
if err != nil { | ||
return fmt.Errorf("%w", err) | ||
} | ||
|
||
wr := writer.New( | ||
writer.WithFormat(parsedFormat), | ||
) | ||
|
||
document, err := backend.GetDocumentByID(sbomID) | ||
if err != nil { | ||
return fmt.Errorf("%w", err) | ||
} | ||
|
||
if opts.OutputFile != nil { | ||
// Write the SBOM document bytes to file. | ||
if err := wr.WriteFile(document, opts.OutputFile.Name()); err != nil { | ||
return fmt.Errorf("%w", err) | ||
} | ||
} else { | ||
// Write the SBOM document bytes to stdout. | ||
if err := wr.WriteStream(document, os.Stdout); err != nil { | ||
return fmt.Errorf("%w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.