github.com/kastenhq/syft@v0.0.0-20230821225854-0710af25cdbe/syft/file/cataloger/filemetadata/cataloger.go (about)

     1  package filemetadata
     2  
     3  import (
     4  	"github.com/wagoodman/go-partybus"
     5  	"github.com/wagoodman/go-progress"
     6  
     7  	"github.com/kastenhq/syft/internal/bus"
     8  	"github.com/kastenhq/syft/internal/log"
     9  	"github.com/kastenhq/syft/syft/event"
    10  	"github.com/kastenhq/syft/syft/file"
    11  )
    12  
    13  type Cataloger struct {
    14  }
    15  
    16  func NewCataloger() *Cataloger {
    17  	return &Cataloger{}
    18  }
    19  
    20  func (i *Cataloger) Catalog(resolver file.Resolver, coordinates ...file.Coordinates) (map[file.Coordinates]file.Metadata, error) {
    21  	results := make(map[file.Coordinates]file.Metadata)
    22  	var locations <-chan file.Location
    23  
    24  	if len(coordinates) == 0 {
    25  		locations = resolver.AllLocations()
    26  	} else {
    27  		locations = func() <-chan file.Location {
    28  			ch := make(chan file.Location)
    29  			go func() {
    30  				close(ch)
    31  				for _, c := range coordinates {
    32  					ch <- file.NewLocationFromCoordinates(c)
    33  				}
    34  			}()
    35  			return ch
    36  		}()
    37  	}
    38  
    39  	stage, prog := metadataCatalogingProgress(int64(len(locations)))
    40  	for location := range locations {
    41  		stage.Current = location.RealPath
    42  		metadata, err := resolver.FileMetadataByLocation(location)
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  
    47  		results[location.Coordinates] = metadata
    48  		prog.Increment()
    49  	}
    50  	log.Debugf("file metadata cataloger processed %d files", prog.Current())
    51  	prog.SetCompleted()
    52  	return results, nil
    53  }
    54  
    55  func metadataCatalogingProgress(locations int64) (*progress.Stage, *progress.Manual) {
    56  	stage := &progress.Stage{}
    57  	prog := progress.NewManual(locations)
    58  
    59  	bus.Publish(partybus.Event{
    60  		Type: event.FileMetadataCatalogerStarted,
    61  		Value: struct {
    62  			progress.Stager
    63  			progress.Progressable
    64  		}{
    65  			Stager:       progress.Stager(stage),
    66  			Progressable: prog,
    67  		},
    68  	})
    69  
    70  	return stage, prog
    71  }