2021-12-19 21:39:53 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-02-09 11:03:36 +00:00
|
|
|
"github.com/trustwallet/assets-go-libs/file"
|
2021-12-20 15:03:08 +00:00
|
|
|
"github.com/trustwallet/assets-go-libs/validation"
|
2021-12-19 21:39:53 +00:00
|
|
|
"github.com/trustwallet/assets/internal/processor"
|
2021-12-23 19:30:20 +00:00
|
|
|
"github.com/trustwallet/assets/internal/report"
|
2021-12-19 21:39:53 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Service struct {
|
|
|
|
fileService *file.Service
|
|
|
|
processorService *processor.Service
|
2021-12-23 19:30:20 +00:00
|
|
|
reportService *report.Service
|
2022-01-14 14:02:39 +00:00
|
|
|
paths []string
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 14:02:39 +00:00
|
|
|
func NewService(fs *file.Service, cs *processor.Service, rs *report.Service, paths []string) *Service {
|
2021-12-19 21:39:53 +00:00
|
|
|
return &Service{
|
|
|
|
fileService: fs,
|
|
|
|
processorService: cs,
|
2021-12-23 19:30:20 +00:00
|
|
|
reportService: rs,
|
2022-01-14 14:02:39 +00:00
|
|
|
paths: paths,
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-14 14:02:39 +00:00
|
|
|
func (s *Service) RunJob(job func(*file.AssetFile)) {
|
|
|
|
for _, path := range s.paths {
|
2021-12-19 21:39:53 +00:00
|
|
|
f := s.fileService.GetAssetFile(path)
|
2021-12-23 19:30:20 +00:00
|
|
|
s.reportService.IncTotalFiles()
|
2022-01-14 14:02:39 +00:00
|
|
|
job(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
reportMsg := s.reportService.GetReport()
|
|
|
|
if s.reportService.IsFailed() {
|
|
|
|
log.Fatal(reportMsg)
|
|
|
|
} else {
|
|
|
|
log.Info(reportMsg)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Check(f *file.AssetFile) {
|
2022-01-07 11:45:16 +00:00
|
|
|
validators := s.processorService.GetValidator(f)
|
2021-12-19 21:39:53 +00:00
|
|
|
|
2022-01-07 11:45:16 +00:00
|
|
|
for _, validator := range validators {
|
2021-12-19 21:39:53 +00:00
|
|
|
if err := validator.Run(f); err != nil {
|
2021-12-23 19:30:20 +00:00
|
|
|
s.handleError(err, f, validator.Name)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Fix(f *file.AssetFile) {
|
|
|
|
fixers := s.processorService.GetFixers(f)
|
|
|
|
|
|
|
|
for _, fixer := range fixers {
|
|
|
|
if err := fixer.Run(f); err != nil {
|
2021-12-23 19:30:20 +00:00
|
|
|
s.handleError(err, f, fixer.Name)
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) RunUpdateAuto() {
|
|
|
|
updaters := s.processorService.GetUpdatersAuto()
|
|
|
|
s.runUpdaters(updaters)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) runUpdaters(updaters []processor.Updater) {
|
|
|
|
for _, updater := range updaters {
|
|
|
|
err := updater.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 19:30:20 +00:00
|
|
|
func (s *Service) handleError(err error, info *file.AssetFile, valName string) {
|
2021-12-19 21:39:53 +00:00
|
|
|
errors := UnwrapComposite(err)
|
|
|
|
|
|
|
|
for _, err := range errors {
|
2021-12-20 15:03:08 +00:00
|
|
|
log.WithFields(log.Fields{
|
2021-12-19 21:39:53 +00:00
|
|
|
"type": info.Type(),
|
|
|
|
"chain": info.Chain().Handle,
|
|
|
|
"asset": info.Asset(),
|
|
|
|
"path": info.Path(),
|
|
|
|
"validation": valName,
|
2021-12-20 15:03:08 +00:00
|
|
|
}).Error(err)
|
2021-12-23 19:30:20 +00:00
|
|
|
|
|
|
|
s.reportService.IncErrors()
|
2021-12-19 21:39:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UnwrapComposite(err error) []error {
|
|
|
|
compErr, ok := err.(*validation.ErrComposite)
|
|
|
|
if !ok {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errors []error
|
|
|
|
for _, e := range compErr.GetErrors() {
|
|
|
|
errors = append(errors, UnwrapComposite(e)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors
|
|
|
|
}
|