github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/text/encoder.go (about)

     1  package text
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"text/tabwriter"
     7  
     8  	"github.com/anchore/syft/syft/sbom"
     9  	"github.com/anchore/syft/syft/source"
    10  )
    11  
    12  func encoder(output io.Writer, s sbom.SBOM) error {
    13  	// init the tabular writer
    14  	w := new(tabwriter.Writer)
    15  	w.Init(output, 0, 8, 0, '\t', tabwriter.AlignRight)
    16  
    17  	switch metadata := s.Source.Metadata.(type) {
    18  	case source.DirectorySourceMetadata:
    19  		fmt.Fprintf(w, "[Path: %s]\n", metadata.Path)
    20  	case source.FileSourceMetadata:
    21  		fmt.Fprintf(w, "[Path: %s]\n", metadata.Path)
    22  	case source.StereoscopeImageSourceMetadata:
    23  		fmt.Fprintln(w, "[Image]")
    24  
    25  		for idx, l := range metadata.Layers {
    26  			fmt.Fprintln(w, " Layer:\t", idx)
    27  			fmt.Fprintln(w, " Digest:\t", l.Digest)
    28  			fmt.Fprintln(w, " Size:\t", l.Size)
    29  			fmt.Fprintln(w, " MediaType:\t", l.MediaType)
    30  			fmt.Fprintln(w)
    31  			w.Flush()
    32  		}
    33  	default:
    34  		return fmt.Errorf("unsupported source: %T", s.Source.Metadata)
    35  	}
    36  
    37  	// populate artifacts...
    38  	rows := 0
    39  	for _, p := range s.Artifacts.Packages.Sorted() {
    40  		fmt.Fprintf(w, "[%s]\n", p.Name)
    41  		fmt.Fprintln(w, " Version:\t", p.Version)
    42  		fmt.Fprintln(w, " Type:\t", string(p.Type))
    43  		fmt.Fprintln(w, " Found by:\t", p.FoundBy)
    44  		fmt.Fprintln(w)
    45  		w.Flush()
    46  		rows++
    47  	}
    48  
    49  	if rows == 0 {
    50  		fmt.Fprintln(output, "No packages discovered")
    51  		return nil
    52  	}
    53  
    54  	return nil
    55  }