github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/cmd/gosbom/cli/convert.go (about)

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