diff --git a/pkg/file/cache.go b/pkg/file/cache.go index 112ba13..51f3177 100644 --- a/pkg/file/cache.go +++ b/pkg/file/cache.go @@ -4,12 +4,12 @@ import ( "sync" ) -type FileProvider struct { +type FileService struct { mu *sync.RWMutex cache map[string]*AssetFile } -func NewFileProvider(filePaths ...string) *FileProvider { +func NewFileService(filePaths ...string) *FileService { var filesMap = make(map[string]*AssetFile) for _, path := range filePaths { @@ -17,20 +17,20 @@ func NewFileProvider(filePaths ...string) *FileProvider { filesMap[path] = assetF } - return &FileProvider{ + return &FileService{ mu: &sync.RWMutex{}, cache: filesMap, } } -func (f *FileProvider) GetAssetFile(path string) (*AssetFile, error) { +func (f *FileService) GetAssetFile(path string) (*AssetFile, error) { f.mu.RLock() defer f.mu.RUnlock() return f.getFile(path) } -func (f *FileProvider) getFile(path string) (*AssetFile, error) { +func (f *FileService) getFile(path string) (*AssetFile, error) { if file, exists := f.cache[path]; exists { err := file.open() if err != nil { diff --git a/src/processor/service.go b/src/processor/service.go index 4a46e34..af89417 100644 --- a/src/processor/service.go +++ b/src/processor/service.go @@ -5,62 +5,81 @@ import ( "github.com/trustwallet/assets-go-libs/pkg/file" "github.com/trustwallet/assets-go-libs/pkg/validation" + "github.com/trustwallet/assets-go-libs/src/reporter" "github.com/trustwallet/assets-go-libs/src/validator" ) +const ( + reportSanityCheckKey = "sanity-check" +) + type Service struct { - fileStorage *file.FileProvider + fileService *file.FileService validatorsService *validator.Service + reporterService *reporter.Service } -func NewService(storage *file.FileProvider, service *validator.Service) *Service { +func NewService(fs *file.FileService, vs *validator.Service, rs *reporter.Service) *Service { return &Service{ - fileStorage: storage, - validatorsService: service, + fileService: fs, + validatorsService: vs, + reporterService: rs, } } func (s *Service) RunSanityCheck(paths []string) error { + report := s.reporterService.GetOrNew(reportSanityCheckKey) + for _, path := range paths { - f, err := s.fileStorage.GetAssetFile(path) + f, err := s.fileService.GetAssetFile(path) if err != nil { log.WithError(err).Error() return err } - validator := s.validatorsService.GetValidatorForFilesAndFolders(f) + report.TotalFiles += 1 + + validator := s.validatorsService.GetFoldersFilesValidator(f) if validator != nil { err = validator.Run(f) if err != nil { - HandleError(err, f.Info, validator.ValidationName) + HandleError(err, f.Info, validator.ValidationName, report) } } err = f.Close() if err != nil { log.WithError(err).Error() - return err } } + err := s.reporterService.Update(reportSanityCheckKey, report) + if err != nil { + log.WithError(err).Error() + return err + } + return nil } -func HandleError(err error, info *file.AssetInfo, valName string) { +func HandleError(err error, info *file.AssetInfo, valName string, report *reporter.Report) { errors := UnwrapComposite(err) for _, err := range errors { if warn, ok := err.(*validation.Warning); ok { + report.Warnings += 1 HandleWarning(warn, info) continue } else { - log.WithField("file_type", info.Type()). - WithField("file_chain", info.Chain().Handle). - WithField("file_asset", info.Asset()). - WithField("file_path", info.Path()). - WithField("validation_name", valName). - Errorf("%+v", err) + report.Errors += 1 + + log.WithField("type", info.Type()). + WithField("chain", info.Chain().Handle). + WithField("asset", info.Asset()). + WithField("path", info.Path()). + WithField("validation", valName). + Error(err) } switch err { diff --git a/src/reporter/service.go b/src/reporter/service.go index 8c19388..5f57fa8 100644 --- a/src/reporter/service.go +++ b/src/reporter/service.go @@ -40,12 +40,12 @@ func (s *Service) Update(key string, report *Report) error { if r, ok := s.reports[key]; ok { r.Fixed += report.Fixed - r.Fixed += report.Errors - r.Fixed += report.Warnings - r.Fixed += report.TotalFiles + r.Errors += report.Errors + r.Warnings += report.Warnings + r.TotalFiles += report.TotalFiles } - s.reports["key"] = report + s.reports[key] = report return nil } @@ -57,26 +57,11 @@ func (s *Service) GetReports() ReportList { return s.reports } +type ReportList map[string]*Report + type Report struct { TotalFiles uint Errors uint Warnings uint Fixed uint } - -type ReportList map[string]*Report - -func (rl ReportList) WithTotal() map[string]*Report { - var totalReport = Report{} - - for _, v := range rl { - totalReport.TotalFiles += v.TotalFiles - totalReport.Errors += v.Errors - totalReport.Warnings += v.Warnings - totalReport.Fixed += v.Fixed - } - - rl["Total"] = &totalReport - - return rl -} diff --git a/src/validator/service.go b/src/validator/service.go index 4a47ab0..a302563 100644 --- a/src/validator/service.go +++ b/src/validator/service.go @@ -5,18 +5,19 @@ import ( ) type Service struct { - fileProvider *file.FileProvider + fileService *file.FileService } -func NewService(fileProvider *file.FileProvider) (*Service, error) { +func NewService(fileProvider *file.FileService) (*Service, error) { return &Service{ - fileProvider: fileProvider, + fileService: fileProvider, }, nil } // nolint:funlen -func (s *Service) GetValidatorForFilesAndFolders(f *file.AssetFile) *Validator { +func (s *Service) GetFoldersFilesValidator(f *file.AssetFile) *Validator { fileType := f.Info.Type() + switch fileType { case file.TypeRootFolder: return &Validator{ diff --git a/src/validator/validators.go b/src/validator/validators.go index e99900b..0b88773 100644 --- a/src/validator/validators.go +++ b/src/validator/validators.go @@ -105,7 +105,7 @@ func (s *Service) ValidateAssetFolder(file *file.AssetFile) error { errLogo := validation.ValidateHasFiles(dirFiles, []string{"logo.png"}) if errLogo != nil || errInfo != nil { - infoFile, err := s.fileProvider.GetAssetFile(fmt.Sprintf("%s/info.json", file.Info.Path())) + infoFile, err := s.fileService.GetAssetFile(fmt.Sprintf("%s/info.json", file.Info.Path())) if err != nil { return err } @@ -264,7 +264,7 @@ func (s *Service) ValidateValidatorsListFile(file *file.AssetFile) error { } assetsPath := fmt.Sprintf("blockchains/%s/validators/assets", file.Info.Chain().Handle) - assetFolder, err := s.fileProvider.GetAssetFile(assetsPath) + assetFolder, err := s.fileService.GetAssetFile(assetsPath) if err != nil { return err }