-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: houdini91 <mdstrauss91@gmail.com>
- Loading branch information
Showing
6 changed files
with
188 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"go.uber.org/zap" | ||
|
||
"github.com/bom-squad/sbom-convert/cmd/cli/options" | ||
"github.com/bom-squad/sbom-convert/pkg/diff" | ||
Check failure on line 13 in cmd/cli/diff.go GitHub Actions / govulncheck
Check failure on line 13 in cmd/cli/diff.go GitHub Actions / govulncheck
Check failure on line 13 in cmd/cli/diff.go GitHub Actions / ruleguard
|
||
"github.com/bom-squad/sbom-convert/pkg/format" | ||
) | ||
|
||
func DiffCommand() *cobra.Command { | ||
co := &options.DiffOptions{} | ||
c := &cobra.Command{ | ||
Use: "diff", | ||
Aliases: []string{"df"}, | ||
SuggestFor: []string{"diff"}, | ||
Short: "Diff between two SBOMS, agnostic to format CycloneDX and SPDX SBOMs", | ||
Long: `Diff between two SBOMS, agonstic to format CycloneDX and SPDX.`, | ||
Example: ` | ||
sbom-diff diff sbom_1.cdx.json sbom_2.cdx.json output to stdout in inverse format | ||
sbom-diff diff sbom_1.spdx.json sbom_2.cdx.json -o sbom.cdx.json output to a file | ||
sbom-diff diff sbom_1.cdx.json sbom_2.cdx.json -f spdx-2.3 select to a specific format | ||
sbom-diff diff sbom_1.cdx.json sbom_2.cdx.json -f spdx -e text select specific encoding | ||
`, | ||
SilenceErrors: true, | ||
SilenceUsage: true, | ||
DisableAutoGenTag: true, | ||
Args: cobra.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runDiff(cmd.Context(), co, args) | ||
}, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
if err := cmd.Parent().PersistentPreRunE(cmd.Parent(), args); err != nil { | ||
return err | ||
} | ||
|
||
if err := options.BindConfig(viper.GetViper(), cmd); err != nil { | ||
return err | ||
} | ||
|
||
return validateDiffOptions(co, args) | ||
}, | ||
} | ||
|
||
co.AddFlags(c) | ||
|
||
return c | ||
} | ||
|
||
func validateDiffOptions(_ *options.DiffOptions, _ []string) error { | ||
return nil | ||
} | ||
|
||
// runDiff is the main entrypoint for the `diff` command | ||
func runDiff(ctx context.Context, co *options.DiffOptions, args []string) error { | ||
log := zap.S() | ||
log.Info("Running Diff command ...") | ||
path1 := args[0] | ||
path2 := args[1] | ||
|
||
f1, err := os.Open(path1) | ||
if err != nil { | ||
return err | ||
} | ||
defer f1.Close() | ||
|
||
f2, err := os.Open(path2) | ||
if err != nil { | ||
return err | ||
} | ||
defer f2.Close() | ||
|
||
output_frmt, err := parseFormatNoInverse(co.Format, co.Encoding, f1) | ||
if err != nil { | ||
return err | ||
} | ||
log.Debugf("Output format %s", output_frmt) | ||
|
||
df1, err := format.Detect(f1) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Debugf("First '%s' format '%s'", path1, df1) | ||
|
||
df2, err := format.Detect(f1) | ||
if err != nil { | ||
return err | ||
} | ||
log.Debugf("Second '%s' format '%s'", path2, df2) | ||
|
||
overwrited := true | ||
out1, outPath1, err := createOutputStream(fmt.Sprintf("%s.added", co.OutputPath), output_frmt) | ||
if err != nil { | ||
return err | ||
} | ||
out2, outPath2, err := createOutputStream(fmt.Sprintf("%s.removed", co.OutputPath), output_frmt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dfs := diff.NewService( | ||
diff.WithFormat(output_frmt), | ||
) | ||
|
||
if err := dfs.Diff(ctx, f1, f2, out1, out2); err != nil { | ||
out1.Close() | ||
out2.Close() | ||
|
||
if !overwrited { | ||
return err | ||
} | ||
|
||
log.Debugf("removing output file %s", *outPath1) | ||
if rerr := os.Remove(*outPath1); rerr != nil { | ||
log.Info("failed to remove output file", zap.String("path", *outPath1), zap.Error(rerr)) | ||
} | ||
log.Debugf("removing output file %s", *outPath2) | ||
if rerr := os.Remove(*outPath2); rerr != nil { | ||
log.Info("failed to remove output file", zap.String("path", *outPath2), zap.Error(rerr)) | ||
} | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} |
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,19 @@ | ||
package options | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// ConvertOptions defines the options for the `convert` command | ||
type DiffOptions struct { | ||
Format string | ||
Encoding string | ||
OutputPath string | ||
} | ||
|
||
// AddFlags adds command line flags for the ConvertOptions struct | ||
func (o *DiffOptions) AddFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringVarP(&o.Format, "format", "f", "", "the output format [spdx, spdx-2.3, cyclonedx, cyclonedx-1.0, cyclonedx-1.1, cyclonedx-1.2, cyclonedx-1.3, cyclonedx-1.4, cyclonedx-1.5]") //nolint: lll | ||
cmd.Flags().StringVarP(&o.Encoding, "encoding", "e", "json", "the output encoding [spdx: [text, json] cyclonedx: [json]") | ||
cmd.Flags().StringVarP(&o.OutputPath, "output", "o", "", "path to write the diff SBOM") | ||
} |
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