|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/viper" |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/bom-squad/sbom-convert/cmd/cli/options" |
| 13 | + "github.com/bom-squad/sbom-convert/pkg/diff" |
| 14 | + "github.com/bom-squad/sbom-convert/pkg/format" |
| 15 | +) |
| 16 | + |
| 17 | +func DiffCommand() *cobra.Command { |
| 18 | + co := &options.DiffOptions{} |
| 19 | + c := &cobra.Command{ |
| 20 | + Use: "diff", |
| 21 | + Aliases: []string{"df"}, |
| 22 | + SuggestFor: []string{"diff"}, |
| 23 | + Short: "Diff between two SBOMS, agnostic to format CycloneDX and SPDX SBOMs", |
| 24 | + Long: `Diff between two SBOMS, agonstic to format CycloneDX and SPDX.`, |
| 25 | + Example: ` |
| 26 | +sbom-diff diff sbom_1.cdx.json sbom_2.cdx.json output to stdout in inverse format |
| 27 | +sbom-diff diff sbom_1.spdx.json sbom_2.cdx.json -o sbom.cdx.json output to a file |
| 28 | +sbom-diff diff sbom_1.cdx.json sbom_2.cdx.json -f spdx-2.3 select to a specific format |
| 29 | +sbom-diff diff sbom_1.cdx.json sbom_2.cdx.json -f spdx -e text select specific encoding |
| 30 | + `, |
| 31 | + SilenceErrors: true, |
| 32 | + SilenceUsage: true, |
| 33 | + DisableAutoGenTag: true, |
| 34 | + Args: cobra.ExactArgs(2), |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + return runDiff(cmd.Context(), co, args) |
| 37 | + }, |
| 38 | + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 39 | + if err := cmd.Parent().PersistentPreRunE(cmd.Parent(), args); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + if err := options.BindConfig(viper.GetViper(), cmd); err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + return validateDiffOptions(co, args) |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + co.AddFlags(c) |
| 52 | + |
| 53 | + return c |
| 54 | +} |
| 55 | + |
| 56 | +func validateDiffOptions(_ *options.DiffOptions, _ []string) error { |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +// runDiff is the main entrypoint for the `diff` command |
| 61 | +func runDiff(ctx context.Context, co *options.DiffOptions, args []string) error { |
| 62 | + log := zap.S() |
| 63 | + log.Info("Running Diff command ...") |
| 64 | + path1 := args[0] |
| 65 | + path2 := args[1] |
| 66 | + |
| 67 | + f1, err := os.Open(path1) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + defer f1.Close() |
| 72 | + |
| 73 | + f2, err := os.Open(path2) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + defer f2.Close() |
| 78 | + |
| 79 | + output_frmt, err := parseFormatNoInverse(co.Format, co.Encoding, f1) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + log.Debugf("Output format %s", output_frmt) |
| 84 | + |
| 85 | + df1, err := format.Detect(f1) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + |
| 90 | + log.Debugf("First '%s' format '%s'", path1, df1) |
| 91 | + |
| 92 | + df2, err := format.Detect(f1) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + log.Debugf("Second '%s' format '%s'", path2, df2) |
| 97 | + |
| 98 | + overwrited := true |
| 99 | + out1, outPath1, err := createOutputStream(fmt.Sprintf("%s.added", co.OutputPath), output_frmt) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + out2, outPath2, err := createOutputStream(fmt.Sprintf("%s.removed", co.OutputPath), output_frmt) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + dfs := diff.NewService( |
| 109 | + diff.WithFormat(output_frmt), |
| 110 | + ) |
| 111 | + |
| 112 | + if err := dfs.Diff(ctx, f1, f2, out1, out2); err != nil { |
| 113 | + out1.Close() |
| 114 | + out2.Close() |
| 115 | + |
| 116 | + if !overwrited { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + log.Debugf("removing output file %s", *outPath1) |
| 121 | + if rerr := os.Remove(*outPath1); rerr != nil { |
| 122 | + log.Info("failed to remove output file", zap.String("path", *outPath1), zap.Error(rerr)) |
| 123 | + } |
| 124 | + log.Debugf("removing output file %s", *outPath2) |
| 125 | + if rerr := os.Remove(*outPath2); rerr != nil { |
| 126 | + log.Info("failed to remove output file", zap.String("path", *outPath2), zap.Error(rerr)) |
| 127 | + } |
| 128 | + |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments