github.com/kastenhq/syft@v0.0.0-20230821225854-0710af25cdbe/cmd/syft/cli/convert.go (about)

     1  package cli
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/spf13/cobra"
     8  	"github.com/spf13/viper"
     9  
    10  	"github.com/kastenhq/syft/cmd/syft/cli/convert"
    11  	"github.com/kastenhq/syft/cmd/syft/cli/options"
    12  	"github.com/kastenhq/syft/internal"
    13  	"github.com/kastenhq/syft/internal/config"
    14  )
    15  
    16  const (
    17  	convertExample = `  {{.appName}} {{.command}} img.syft.json -o spdx-json                      convert a syft SBOM to spdx-json, output goes to stdout
    18    {{.appName}} {{.command}} img.syft.json -o cyclonedx-json=img.cdx.json    convert a syft SBOM to CycloneDX, output is written to the file "img.cdx.json""
    19    {{.appName}} {{.command}} - -o spdx-json                                  convert an SBOM from STDIN to spdx-json
    20  `
    21  )
    22  
    23  //nolint:dupl
    24  func Convert(v *viper.Viper, app *config.Application, ro *options.RootOptions, po *options.PackagesOptions) *cobra.Command {
    25  	cmd := &cobra.Command{
    26  		Use:   "convert [SOURCE-SBOM] -o [FORMAT]",
    27  		Short: "Convert between SBOM formats",
    28  		Long:  "[Experimental] Convert SBOM files to, and from, SPDX, CycloneDX and Syft's format. For more info about data loss between formats see https://github.com/anchore/syft#format-conversion-experimental",
    29  		Example: internal.Tprintf(convertExample, map[string]interface{}{
    30  			"appName": internal.ApplicationName,
    31  			"command": "convert",
    32  		}),
    33  		Args: func(cmd *cobra.Command, args []string) error {
    34  			if err := app.LoadAllValues(v, ro.Config); err != nil {
    35  				return fmt.Errorf("invalid application config: %w", err)
    36  			}
    37  			newLogWrapper(app)
    38  			logApplicationConfig(app)
    39  			return validateArgs(cmd, args)
    40  		},
    41  		SilenceUsage:  true,
    42  		SilenceErrors: true,
    43  		RunE: func(cmd *cobra.Command, args []string) error {
    44  			if app.CheckForAppUpdate {
    45  				checkForApplicationUpdate()
    46  				// TODO: this is broke, the bus isn't available yet
    47  			}
    48  			return convert.Run(cmd.Context(), app, args)
    49  		},
    50  	}
    51  
    52  	err := po.AddFlags(cmd, v)
    53  	if err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	return cmd
    58  }