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

     1  package convert
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"os"
     8  
     9  	"github.com/nextlinux/gosbom/cmd/gosbom/cli/options"
    10  	"github.com/nextlinux/gosbom/gosbom/formats"
    11  	"github.com/nextlinux/gosbom/internal/config"
    12  	"github.com/nextlinux/gosbom/internal/log"
    13  )
    14  
    15  func Run(_ context.Context, app *config.Application, args []string) error {
    16  	log.Warn("convert is an experimental feature, run `gosbom convert -h` for help")
    17  	writer, err := options.MakeSBOMWriter(app.Outputs, app.File, app.OutputTemplatePath)
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	// this can only be a SBOM file
    23  	userInput := args[0]
    24  
    25  	var reader io.ReadCloser
    26  
    27  	if userInput == "-" {
    28  		reader = os.Stdin
    29  	} else {
    30  		f, err := os.Open(userInput)
    31  		if err != nil {
    32  			return fmt.Errorf("failed to open SBOM file: %w", err)
    33  		}
    34  		defer func() {
    35  			_ = f.Close()
    36  		}()
    37  		reader = f
    38  	}
    39  
    40  	sbom, _, err := formats.Decode(reader)
    41  	if err != nil {
    42  		return fmt.Errorf("failed to decode SBOM: %w", err)
    43  	}
    44  
    45  	return writer.Write(*sbom)
    46  }