Skip to content

Commit

Permalink
Merge branch 'main' into output-folder
Browse files Browse the repository at this point in the history
Signed-off-by: Mikhail Svobodan <[email protected]>
  • Loading branch information
mirxcle authored Mar 11, 2024
2 parents c31cfbb + 34cb27b commit e1bf595
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ kissbom convert test.cyclonedx.json //where test.cyclonedx.json is a valid Cyclo
|---|---|
|```--format=json``` | Outputs all 4 KissBOM fields in JSON format. This is the default output format |
|```--format=yaml``` | Outputs all 4 KissBOM fields in YAML format |
|```--format=csv``` | Outputs all 3 KissBOM fields into a CSV formatted file |
|```--format=minimal``` | Outputs just the KissBOM required fields into a JSON formatted file |
|```--format=csv``` | Outputs all 4 KissBOM fields into a CSV formatted file |
|```--format=minimal``` | Outputs just the KissBOM required fields into a JSON formatted file (Purl) |
|```--format=compatible``` | Outputs all 4 KissBOM fields in a CycloneDX formatted JSON file |

### Debugging
Expand Down
3 changes: 3 additions & 0 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ var (
converter.OutputFormat = selectedFormat
converter.OutputFolder = outputFolder

log.Println("starting conversion")
err := converter.Convert(args[0])
if err != nil {
util.PrintErr(err)
os.Exit(1)
}

log.Println("finished")
util.PrintInfof("Saved KISSBOM as: %v\n", converter.OutputFileName)
util.PrintSuccess("DONE!")
os.Exit(0)
},
}
Expand Down
30 changes: 16 additions & 14 deletions lib/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (c *Converter) Convert(filename string) error {

log.Printf("bytes: %v", len(source))

kissbom, filename, err := c.transform(source)
kissbom, err := c.transform(source)
if err != nil {
return err
}
Expand All @@ -56,7 +56,7 @@ func (c *Converter) Convert(filename string) error {
// transform takes a byte slice representing a CycloneDX Bill of Materials (BOM) in JSON format,
// decodes it into a CycloneDX BOM object, and then transforms it into a KissBOM object along
// with a filename. Any decoding errors are returned as an error.
func (c *Converter) transform(source []byte) (kissbom models.KissBOM, filename string, err error) {
func (c *Converter) transform(source []byte) (kissbom models.KissBOM, err error) {
var cdx cyclonedx.BOM

err = cyclonedx.NewBOMDecoder(bytes.NewReader(source), cyclonedx.BOMFileFormatJSON).Decode(&cdx)
Expand All @@ -66,7 +66,9 @@ func (c *Converter) transform(source []byte) (kissbom models.KissBOM, filename s

log.Println("transformed to kissbom")

return models.NewKissBOMFromCycloneDX(&cdx), c.buildOutputFilename(&cdx), nil
c.OutputFileName = c.buildOutputFilename(&cdx)

return models.NewKissBOMFromCycloneDX(&cdx), nil
}

// buildOutputFilename builds the output filename from the provided CycloneDX BOM
Expand All @@ -79,35 +81,35 @@ func (c *Converter) buildOutputFilename(cdx *cyclonedx.BOM) string {
subject := cdx.Metadata.Component.Name
publisher := cdx.Metadata.Component.Publisher
timestamp := cdx.Metadata.Timestamp
return fmt.Sprintf("%s_%s_%s", subject, publisher, timestamp)
c.OutputFileName = fmt.Sprintf("%s_%s_%s", subject, publisher, timestamp)
}
t := time.Now()
return fmt.Sprint(t.Format("20060102150405"))
}

// Function to write the KissBOM to a file based on the specified output format
func (c *Converter) writeToFile(kissbom models.KissBOM, outputFormat string, filename string) error {
func (c *Converter) writeToFile(kissbom models.KissBOM) error {
var outputData []byte
var err error

switch outputFormat {
switch c.OutputFormat {
case models.OptionJSON:
outputData, err = kissbom.JSON()
filename += ".json"
c.OutputFileName += ".json"
case models.OptionYAML:
outputData, err = kissbom.YAML()
filename += ".yaml"
c.OutputFileName += ".yaml"
case models.OptionCSV:
outputData, err = kissbom.CSV()
filename += ".csv"
c.OutputFileName += ".csv"
case models.OptionMinimal:
outputData, err = kissbom.Minimal()
filename += ".json"
c.OutputFileName += ".json"
case models.OptionCompatible:
outputData, err = kissbom.Compatible()
filename += ".cyclonedx.json"
c.OutputFileName += ".cyclonedx.json"
default:
err = fmt.Errorf("unsupported output format: %s", outputFormat)
err = fmt.Errorf("unsupported output format: %s", c.OutputFormat)
}

if err != nil {
Expand All @@ -117,7 +119,7 @@ func (c *Converter) writeToFile(kissbom models.KissBOM, outputFormat string, fil
log.Printf("final bytes: %v", len(outputData))

// Use afero to write the output data to the file
err = afero.WriteFile(c.Afs, filename, outputData, 0644)
log.Printf("saved: %v", filename)
err = afero.WriteFile(c.Afs, c.OutputFileName, outputData, 0644)
log.Printf("saved: %v", c.OutputFileName)
return err
}
17 changes: 9 additions & 8 deletions lib/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ func TestTransform_Success(t *testing.T) {
OutputFileName: "test",
}

kissBom, filename, err := converter.transform([]byte(jsonContent))
kissBom, err := converter.transform([]byte(jsonContent))

assert.NoError(t, err, "Expected no error")
assert.NotNil(t, kissBom, "Expected KissBOM object to be not nil")
assert.NotEmpty(t, filename, "Expected filename to be not empty")
//assert.Len(t, kissBom.Packages, 1)
assert.NotEmpty(t, converter.OutputFileName, "Expected filename to be not empty")
assert.Equal(t, converter.OutputFileName, "test")
assert.Len(t, kissBom.Packages, 1)
assert.Equal(t, kissBom.Packages[0].Purl, "pkg:pypi/[email protected]")
}

Expand All @@ -150,7 +151,7 @@ func TestTransform_DecodeError(t *testing.T) {

invalidCycloneDxJSON := []byte(`{"fake""}`)

_, _, err := converter.transform(invalidCycloneDxJSON)
_, err := converter.transform(invalidCycloneDxJSON)

assert.Error(t, err, "Expected an error due to invalid JSON")
}
Expand Down Expand Up @@ -181,7 +182,9 @@ func TestBuildOutputFilename(t *testing.T) {

func TestConverter_writeToFile(t *testing.T) {
converter := Converter{
Afs: &afero.Afero{Fs: afero.NewMemMapFs()},
Afs: &afero.Afero{Fs: afero.NewMemMapFs()},
OutputFileName: "&*%/\"",
OutputFormat: models.OptionMinimal,
}

kissBOM := models.KissBOM{
Expand All @@ -190,9 +193,7 @@ func TestConverter_writeToFile(t *testing.T) {
},
}

filename := "&*%/\""

err := converter.writeToFile(kissBOM, "minimal", filename)
err := converter.writeToFile(kissBOM)
assert.NoError(t, err)

}

0 comments on commit e1bf595

Please sign in to comment.