forked from anchore/syft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for multiple output files in different formats (anchore#732)
Signed-off-by: fsl <1171313930@qq.com>
- Loading branch information
1 parent
0217622
commit fb5340d
Showing
51 changed files
with
640 additions
and
241 deletions.
There are no files selected for viewing
This file contains 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 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 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,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/anchore/syft/internal/formats" | ||
"github.com/anchore/syft/internal/output" | ||
"github.com/anchore/syft/syft/format" | ||
"github.com/anchore/syft/syft/sbom" | ||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
// makeWriter creates a sbom.Writer for output or returns an error. this will either return a valid writer | ||
// or an error but neither both and if there is no error, sbom.Writer.Close() should be called | ||
func makeWriter(outputs []string, defaultFile string) (sbom.Writer, error) { | ||
outputOptions, err := parseOptions(outputs, defaultFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
writer, err := output.MakeWriter(outputOptions...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return writer, nil | ||
} | ||
|
||
// parseOptions utility to parse command-line option strings and retain the existing behavior of default format and file | ||
func parseOptions(outputs []string, defaultFile string) (out []output.WriterOption, errs error) { | ||
// always should have one option -- we generally get the default of "table", but just make sure | ||
if len(outputs) == 0 { | ||
outputs = append(outputs, string(format.TableOption)) | ||
} | ||
|
||
for _, name := range outputs { | ||
name = strings.TrimSpace(name) | ||
|
||
// split to at most two parts for <format>=<file> | ||
parts := strings.SplitN(name, "=", 2) | ||
|
||
// the format option is the first part | ||
name = parts[0] | ||
|
||
// default to the --file or empty string if not specified | ||
file := defaultFile | ||
|
||
// If a file is specified as part of the output option, use that | ||
if len(parts) > 1 { | ||
file = parts[1] | ||
} | ||
|
||
option := format.ParseOption(name) | ||
if option == format.UnknownFormatOption { | ||
errs = multierror.Append(errs, fmt.Errorf("bad output format: '%s'", name)) | ||
continue | ||
} | ||
|
||
encoder := formats.ByOption(option) | ||
if encoder == nil { | ||
errs = multierror.Append(errs, fmt.Errorf("unknown format: %s", outputFormat)) | ||
continue | ||
} | ||
|
||
out = append(out, output.WriterOption{ | ||
Format: *encoder, | ||
Path: file, | ||
}) | ||
} | ||
return out, errs | ||
} |
This file contains 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,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOutputWriterConfig(t *testing.T) { | ||
tmp := t.TempDir() + "/" | ||
|
||
tests := []struct { | ||
outputs []string | ||
file string | ||
err bool | ||
expected []string | ||
}{ | ||
{ | ||
outputs: []string{}, | ||
expected: []string{""}, | ||
}, | ||
{ | ||
outputs: []string{"json"}, | ||
expected: []string{""}, | ||
}, | ||
{ | ||
file: "test-1.json", | ||
expected: []string{"test-1.json"}, | ||
}, | ||
{ | ||
outputs: []string{"json=test-2.json"}, | ||
expected: []string{"test-2.json"}, | ||
}, | ||
{ | ||
outputs: []string{"json=test-3-1.json", "spdx-json=test-3-2.json"}, | ||
expected: []string{"test-3-1.json", "test-3-2.json"}, | ||
}, | ||
{ | ||
outputs: []string{"text", "json=test-4.json"}, | ||
expected: []string{"", "test-4.json"}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(fmt.Sprintf("%s/%s", test.outputs, test.file), func(t *testing.T) { | ||
outputs := test.outputs | ||
for i, val := range outputs { | ||
outputs[i] = strings.Replace(val, "=", "="+tmp, 1) | ||
} | ||
|
||
file := test.file | ||
if file != "" { | ||
file = tmp + file | ||
} | ||
|
||
_, err := makeWriter(test.outputs, file) | ||
|
||
if test.err { | ||
assert.Error(t, err) | ||
return | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
|
||
for _, expected := range test.expected { | ||
if expected != "" { | ||
assert.FileExists(t, tmp+expected) | ||
} else if file != "" { | ||
assert.FileExists(t, file) | ||
} else { | ||
assert.NoFileExists(t, expected) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains 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
Oops, something went wrong.