Skip to content

Commit

Permalink
adds output folder
Browse files Browse the repository at this point in the history
  • Loading branch information
djschleen authored Mar 11, 2024
1 parent eb4df31 commit 1dbb9d1
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 45 deletions.
6 changes: 2 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@
"ahebrank.yaml2json",
"amazonwebservices.aws-toolkit-vscode",
"markis.code-coverage",
//"defaltd.go-coverage-viewer",
"Gruntfuggly.todo-tree", // Highlights TODO comments",
"Semgrep.semgrep"
"Gruntfuggly.todo-tree" // Highlights TODO comments",
]
}
},
"postCreateCommand": "/usr/bin/bash ./.devcontainer/post-create.sh > ~/post-create.log"
}
}
121 changes: 82 additions & 39 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,97 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"name": "Debug (output default)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["--debug=true", "convert", "./_TESTDATA_/juiceshop.cyclonedx.json"]
"args": [
"--debug=true",
"convert",
"./_TESTDATA_/juiceshop.cyclonedx.json"
]
},
{
"name": "Debug (output json)",
"name": "Debug (output default, output folder)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["--debug=true", "convert","--format", "yaml", "./_TESTDATA_/juiceshop.cyclonedx.json"]
},
{
"name": "Debug (output yaml)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["--debug=true", "convert","--format", "yaml", "./_TESTDATA_/juiceshop.cyclonedx.json"]
},
{
"name": "Debug (output csv)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["--debug=true", "convert","--format", "csv", "./_TESTDATA_/juiceshop.cyclonedx.json"]
},
{
"name": "Debug (output minimal)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["--debug=true", "convert","--format", "minimal", "./_TESTDATA_/juiceshop.cyclonedx.json"]
},
{
"name": "Debug (output compatible)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": ["--debug=true", "convert","--format", "compatible", "./_TESTDATA_/juiceshop.cyclonedx.json"]
},
]
}
"args": [
"--debug=true",
"--output-folder=./_TESTDATA_/", "convert", "./_TESTDATA_/juiceshop.cyclonedx.json"]
},
{
"name": "Debug (output json)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"--debug=true",
"convert",
"--format",
"yaml",
"./_TESTDATA_/juiceshop.cyclonedx.json"
]
},
{
"name": "Debug (output yaml)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"--debug=true",
"convert",
"--format",
"yaml",
"./_TESTDATA_/juiceshop.cyclonedx.json"
]
},
{
"name": "Debug (output csv)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"--debug=true",
"convert",
"--format",
"csv",
"./_TESTDATA_/juiceshop.cyclonedx.json"
]
},
{
"name": "Debug (output minimal)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"--debug=true",
"convert",
"--format",
"minimal",
"./_TESTDATA_/juiceshop.cyclonedx.json"
]
},
{
"name": "Debug (output compatible)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"--debug=true",
"convert",
"--format",
"compatible",
"./_TESTDATA_/juiceshop.cyclonedx.json"
]
},
]
}
3 changes: 3 additions & 0 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
outputFormats = []string{"json", "yaml", "csv", "minimal", "compatible"}

selectedFormat string
outputFolder string
convertCmd = &cobra.Command{
Use: "convert",
Short: "Converts a provided CycloneDX file to a KISSBOM format",
Expand All @@ -28,6 +29,7 @@ var (
Run: func(cmd *cobra.Command, args []string) {
converter := lib.NewConverter()
converter.OutputFormat = selectedFormat
converter.OutputFolder = outputFolder

err := converter.Convert(args[0])
if err != nil {
Expand All @@ -44,6 +46,7 @@ var (
func init() {
rootCmd.AddCommand(convertCmd)
convertCmd.Flags().StringVarP(&selectedFormat, "format", "f", "json", fmt.Sprintf("select one of the valid options: %s", outputFormats))
convertCmd.Flags().StringVarP(&outputFolder, "output-folder", "o", ".", "the output folder for the converted file")
_ = rootCmd.Flags().SetAnnotation("format", cobra.BashCompOneRequiredFlag, []string{"true"})

}
3 changes: 3 additions & 0 deletions lib/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"fmt"
"log"
"path"

"github.com/CycloneDX/cyclonedx-go"
"github.com/spf13/afero"
Expand All @@ -16,6 +17,7 @@ import (
type Converter struct {
Afs *afero.Afero // Afero file system abstraction for file operations.
OutputFileName string // Name of the output file.
OutputFolder string //The folder in which to save the generated file.
OutputFormat string // Desired output format.
}

Expand Down Expand Up @@ -46,6 +48,7 @@ func (c *Converter) Convert(filename string) error {
return err
}

filename = path.Join(c.OutputFolder, filename)
return c.writeToFile(kissbom, c.OutputFormat, filename)
}

Expand Down
38 changes: 36 additions & 2 deletions lib/converter_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package lib

import (
"io"
"os"
"testing"
"time"

Expand Down Expand Up @@ -36,7 +38,23 @@ func TestConvert_Success(t *testing.T) {
Afs: &afero.Afero{Fs: afero.NewMemMapFs()},
}

converter.Afs.WriteFile("test.json", []byte(jsonContent), 0644)
e := func() error {
var data []byte = []byte(jsonContent)
f, err := (converter.Afs).Fs.OpenFile("test.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0644))
if err != nil {
return err
}
n, err := f.Write(data)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}()

assert.NoError(t, e)

converter.OutputFormat = "json" // Choose a valid output format for testing
converter.OutputFileName = "test_output"
Expand All @@ -63,7 +81,23 @@ func TestConvert_Success(t *testing.T) {
err = converter.Convert("test.json")
assert.Error(t, err, "Expected no error")

converter.Afs.WriteFile("test.json", []byte("<>test"), 0644)
e = func() error {
var data []byte = []byte("<>test")
f, err := (*converter.Afs).Fs.OpenFile("test.json", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0644))
if err != nil {
return err
}
n, err := f.Write(data)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}()

assert.NoError(t,e)
converter.OutputFormat = "csv" // Choose a valid output format for testing
err = converter.Convert("test.json")
assert.Error(t, err, "Expected no error")
Expand Down

0 comments on commit 1dbb9d1

Please sign in to comment.