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