From 004e5cdbb3e2e1b4fa0db2a5e5534bcdc9461fe5 Mon Sep 17 00:00:00 2001 From: daniel <24758309+unanoc@users.noreply.github.com> Date: Wed, 9 Feb 2022 14:03:36 +0300 Subject: [PATCH] Transfer file package to assets-go-libs (#17984) --- go.mod | 2 +- go.sum | 4 +- internal/file/cache.go | 58 ----------- internal/file/path.go | 163 ------------------------------- internal/file/service.go | 29 ------ internal/file/types.go | 26 ----- internal/manager/manager.go | 7 +- internal/processor/fixers.go | 13 ++- internal/processor/model.go | 2 +- internal/processor/service.go | 2 +- internal/processor/validators.go | 29 +++--- internal/service/service.go | 2 +- 12 files changed, 29 insertions(+), 308 deletions(-) delete mode 100644 internal/file/cache.go delete mode 100644 internal/file/path.go delete mode 100644 internal/file/service.go delete mode 100644 internal/file/types.go diff --git a/go.mod b/go.mod index d1f177bd4..50ab3101d 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.3.0 - github.com/trustwallet/assets-go-libs v0.0.38 + github.com/trustwallet/assets-go-libs v0.1.0 github.com/trustwallet/go-libs v0.2.26 github.com/trustwallet/go-primitives v0.0.30 ) diff --git a/go.sum b/go.sum index ff4ce582a..b2781f05f 100644 --- a/go.sum +++ b/go.sum @@ -340,8 +340,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= -github.com/trustwallet/assets-go-libs v0.0.38 h1:18U6w+MuCaYdPFHLYu/YMHd0lfHq1Ey5T2abBMvqc9o= -github.com/trustwallet/assets-go-libs v0.0.38/go.mod h1:2kTQMbtDRmYJLMzB/HMIy7vVpUYvT3Bp3u3XIal+Zk8= +github.com/trustwallet/assets-go-libs v0.1.0 h1:MLbtjz8OxCFy2NRE4g6Ol50OUsusL82VjXhO29U6CPo= +github.com/trustwallet/assets-go-libs v0.1.0/go.mod h1:2kTQMbtDRmYJLMzB/HMIy7vVpUYvT3Bp3u3XIal+Zk8= github.com/trustwallet/go-libs v0.2.26 h1:WpDc7X23EQwdrMRZ1JqXWXUk15+8pfej4pTt/3hBIJo= github.com/trustwallet/go-libs v0.2.26/go.mod h1:7QdAp1lcteKKI0DYqGoaO8KO4eTNYjGmg8vHy0YXkKc= github.com/trustwallet/go-primitives v0.0.30 h1:KnNU7RSGlNeczD3P44NVRaYfAcxkbBELxNZD6kL4Qc0= diff --git a/internal/file/cache.go b/internal/file/cache.go deleted file mode 100644 index 277db0a7f..000000000 --- a/internal/file/cache.go +++ /dev/null @@ -1,58 +0,0 @@ -package file - -import ( - "path/filepath" - "strings" - "sync" -) - -type Service struct { - mu *sync.RWMutex - cache map[string]*AssetFile -} - -func NewService(filePaths ...string) *Service { - var filesMap = make(map[string]*AssetFile) - - for _, path := range filePaths { - assetFile := NewAssetFile(path) - filesMap[path] = assetFile - } - - return &Service{ - mu: &sync.RWMutex{}, - cache: filesMap, - } -} - -func (f *Service) GetAssetFile(path string) *AssetFile { - f.mu.RLock() - defer f.mu.RUnlock() - - return f.getFile(path) -} - -func (f *Service) UpdateFile(file *AssetFile, newFileBaseName string) { - f.mu.RLock() - defer f.mu.RUnlock() - - oldFileBaseName := filepath.Base(file.Path()) - - for path := range f.cache { - if strings.Contains(path, oldFileBaseName) { - newPath := strings.ReplaceAll(path, oldFileBaseName, newFileBaseName) - f.cache[path] = NewAssetFile(newPath) - } - } -} - -func (f *Service) getFile(path string) *AssetFile { - if file, exists := f.cache[path]; exists { - return file - } - - assetF := NewAssetFile(path) - f.cache[path] = assetF - - return assetF -} diff --git a/internal/file/path.go b/internal/file/path.go deleted file mode 100644 index c8d5d2883..000000000 --- a/internal/file/path.go +++ /dev/null @@ -1,163 +0,0 @@ -package file - -import ( - "fmt" - "os" - "path/filepath" - "regexp" - - "github.com/trustwallet/assets-go-libs/strings" - "github.com/trustwallet/go-primitives/coin" -) - -var ( - regexAssetInfoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/assets/(\w+[\-]\w+|\w+)/info.json$`) - regexAssetLogoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/assets/(\w+[\-]\w+|\w+)/logo.png$`) - - regexChainInfoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info/info.json$`) - regexChainLogoFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info/logo.png$`) - - regexTokenListFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/tokenlist.json$`) - regexTokenListExtendedFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/tokenlist-extended.json$`) - - regexValidatorsAssetLogo = regexp.MustCompile( - `./blockchains/(\w+[\-]\w+|\w+)/validators/assets/(\w+[\-]\w+|\w+)/logo.png$`) - regexValidatorsList = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/validators/list.json$`) - - regexDappsLogo = regexp.MustCompile(`./dapps/[a-zA-Z-.]+\.png$`) -) - -var ( - regexAssetFolder = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/assets/(\w+[\-]\w+|\w+)$`) - regexAssetsFolder = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/assets$`) - - regexValidatorsFolder = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/validators$`) - regexValidatorsAssetFolder = regexp.MustCompile( - `./blockchains/(\w+[\-]\w+|\w+)/validators/assets/(\w+[\-]\w+|\w+)$`) - regexValidatorsAssetsFolder = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/validators/assets$`) - - regexChainFolder = regexp.MustCompile(`./blockchains/(\w+[^/])$`) - regexChainInfoFolder = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/info$`) - regexChainsFolder = regexp.MustCompile(`./blockchains$`) - - regexDappsFolder = regexp.MustCompile(`./dapps$`) - regexRoot = regexp.MustCompile(`./$`) -) - -var regexes = map[string]*regexp.Regexp{ - TypeAssetInfoFile: regexAssetInfoFile, - TypeAssetLogoFile: regexAssetLogoFile, - - TypeChainInfoFile: regexChainInfoFile, - TypeChainLogoFile: regexChainLogoFile, - - TypeTokenListFile: regexTokenListFile, - TypeTokenListExtendedFile: regexTokenListExtendedFile, - - TypeValidatorsListFile: regexValidatorsList, - TypeValidatorsLogoFile: regexValidatorsAssetLogo, - - TypeDappsLogoFile: regexDappsLogo, - - TypeAssetFolder: regexAssetFolder, - TypeAssetsFolder: regexAssetsFolder, - - TypeChainFolder: regexChainFolder, - TypeChainsFolder: regexChainsFolder, - TypeChainInfoFolder: regexChainInfoFolder, - - TypeDappsFolder: regexDappsFolder, - TypeRootFolder: regexRoot, - - TypeValidatorsFolder: regexValidatorsFolder, - TypeValidatorsAssetsFolder: regexValidatorsAssetsFolder, - TypeValidatorsAssetFolder: regexValidatorsAssetFolder, -} - -type Path struct { - path string - chain coin.Coin - asset string - fileType string -} - -func NewPath(path string) *Path { - p := Path{path: path} - - fileType, reg := defineFileType(path) - if reg == nil { - p.fileType = TypeUnknown - - return &p - } - - match := reg.FindStringSubmatch(path) - if fileType != TypeUnknown { - p.fileType = fileType - } - - if len(match) >= 2 { - chain, err := coin.GetCoinForId(match[1]) - if err != nil { - p.chain = coin.Coin{Handle: match[1]} - } else { - p.chain = chain - } - } - - if len(match) == 3 { - p.asset = match[2] - } - - return &p -} - -func (p Path) Type() string { - return p.fileType -} - -func (p Path) String() string { - return p.path -} - -func (p Path) Chain() coin.Coin { - return p.chain -} - -func (p Path) Asset() string { - return p.asset -} - -func defineFileType(p string) (string, *regexp.Regexp) { - for t, r := range regexes { - if r.MatchString(p) { - return t, r - } - } - - return TypeUnknown, nil -} - -func ReadLocalFileStructure(root string, filesToSkip []string) ([]string, error) { - var paths = []string{"./"} - err := filepath.Walk(root, - func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if strings.Contains(path, filesToSkip) { - return nil - } - - paths = append(paths, fmt.Sprintf("./%s", path)) - - return nil - }) - - if err != nil { - return nil, err - } - - return paths, nil -} diff --git a/internal/file/service.go b/internal/file/service.go deleted file mode 100644 index a274e2028..000000000 --- a/internal/file/service.go +++ /dev/null @@ -1,29 +0,0 @@ -package file - -import ( - "github.com/trustwallet/go-primitives/coin" -) - -type AssetFile struct { - path *Path -} - -func NewAssetFile(path string) *AssetFile { - return &AssetFile{path: NewPath(path)} -} - -func (i *AssetFile) Path() string { - return i.path.String() -} - -func (i *AssetFile) Type() string { - return i.path.fileType -} - -func (i *AssetFile) Chain() coin.Coin { - return i.path.chain -} - -func (i *AssetFile) Asset() string { - return i.path.asset -} diff --git a/internal/file/types.go b/internal/file/types.go deleted file mode 100644 index a606ac2fb..000000000 --- a/internal/file/types.go +++ /dev/null @@ -1,26 +0,0 @@ -package file - -const ( - TypeAssetFolder = "asset_folder" - TypeAssetsFolder = "assets_folder" - TypeChainFolder = "chain_folder" - TypeChainInfoFolder = "chain_info_folder" - TypeChainsFolder = "chains_folder" - TypeDappsFolder = "dapps_folder" - TypeRootFolder = "root_folder" - TypeValidatorsFolder = "validators_folder" - TypeValidatorsAssetFolder = "validators_asset_folder" - TypeValidatorsAssetsFolder = "validators_assets_folder" - - TypeAssetInfoFile = "asset_info_file" - TypeAssetLogoFile = "asset_logo_file" - TypeChainInfoFile = "chain_info_file" - TypeChainLogoFile = "chain_logo_file" - TypeTokenListFile = "chain_tokenlist_file" - TypeTokenListExtendedFile = "chain_tokenlist_extended_file" - TypeDappsLogoFile = "dapps_logo_file" - TypeValidatorsListFile = "validators_list_file" - TypeValidatorsLogoFile = "validators_logo_file" - - TypeUnknown = "unknown_file" -) diff --git a/internal/manager/manager.go b/internal/manager/manager.go index a5903b18c..1f393c994 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -3,15 +3,14 @@ package manager import ( "os" + "github.com/trustwallet/assets-go-libs/file" "github.com/trustwallet/assets-go-libs/path" - "github.com/trustwallet/go-primitives/asset" - "github.com/trustwallet/go-primitives/coin" - "github.com/trustwallet/assets/internal/config" - "github.com/trustwallet/assets/internal/file" "github.com/trustwallet/assets/internal/processor" "github.com/trustwallet/assets/internal/report" "github.com/trustwallet/assets/internal/service" + "github.com/trustwallet/go-primitives/asset" + "github.com/trustwallet/go-primitives/coin" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" diff --git a/internal/processor/fixers.go b/internal/processor/fixers.go index 6164c72b5..896d41261 100644 --- a/internal/processor/fixers.go +++ b/internal/processor/fixers.go @@ -6,12 +6,11 @@ import ( "path/filepath" "strings" - fileLib "github.com/trustwallet/assets-go-libs/file" + "github.com/trustwallet/assets-go-libs/file" "github.com/trustwallet/assets-go-libs/image" "github.com/trustwallet/assets-go-libs/path" "github.com/trustwallet/assets-go-libs/validation" "github.com/trustwallet/assets-go-libs/validation/info" - "github.com/trustwallet/assets/internal/file" "github.com/trustwallet/go-primitives/address" "github.com/trustwallet/go-primitives/coin" "github.com/trustwallet/go-primitives/types" @@ -20,7 +19,7 @@ import ( ) func (s *Service) FixJSON(f *file.AssetFile) error { - return fileLib.FormatJSONFile(f.Path()) + return file.FormatJSONFile(f.Path()) } func (s *Service) FixETHAddressChecksum(f *file.AssetFile) error { @@ -103,7 +102,7 @@ func calculateTargetDimension(width, height int) (targetW, targetH int) { func (s *Service) FixChainInfoJSON(f *file.AssetFile) error { var chainInfo info.CoinModel - err := fileLib.ReadJSONFile(f.Path(), &chainInfo) + err := file.ReadJSONFile(f.Path(), &chainInfo) if err != nil { return err } @@ -112,7 +111,7 @@ func (s *Service) FixChainInfoJSON(f *file.AssetFile) error { if chainInfo.Type == nil || *chainInfo.Type != expectedType { chainInfo.Type = &expectedType - return fileLib.CreateJSONFile(f.Path(), &chainInfo) + return file.CreateJSONFile(f.Path(), &chainInfo) } return nil @@ -121,7 +120,7 @@ func (s *Service) FixChainInfoJSON(f *file.AssetFile) error { func (s *Service) FixAssetInfo(f *file.AssetFile) error { var assetInfo info.AssetModel - err := fileLib.ReadJSONFile(f.Path(), &assetInfo) + err := file.ReadJSONFile(f.Path(), &assetInfo) if err != nil { return err } @@ -166,7 +165,7 @@ func (s *Service) FixAssetInfo(f *file.AssetFile) error { } if isModified { - return fileLib.CreateJSONFile(f.Path(), &assetInfo) + return file.CreateJSONFile(f.Path(), &assetInfo) } return nil diff --git a/internal/processor/model.go b/internal/processor/model.go index 2c2d267bc..b743a6bfa 100644 --- a/internal/processor/model.go +++ b/internal/processor/model.go @@ -1,7 +1,7 @@ package processor import ( - "github.com/trustwallet/assets/internal/file" + "github.com/trustwallet/assets-go-libs/file" ) type ( diff --git a/internal/processor/service.go b/internal/processor/service.go index ef36ad1f9..e2774a62a 100644 --- a/internal/processor/service.go +++ b/internal/processor/service.go @@ -2,8 +2,8 @@ package processor import ( assetsmanager "github.com/trustwallet/assets-go-libs/client/assets-manager" + "github.com/trustwallet/assets-go-libs/file" "github.com/trustwallet/assets/internal/config" - "github.com/trustwallet/assets/internal/file" ) type Service struct { diff --git a/internal/processor/validators.go b/internal/processor/validators.go index 73db0f8b8..0788ddcad 100644 --- a/internal/processor/validators.go +++ b/internal/processor/validators.go @@ -5,14 +5,13 @@ import ( "fmt" "os" - filelib "github.com/trustwallet/assets-go-libs/file" + "github.com/trustwallet/assets-go-libs/file" "github.com/trustwallet/assets-go-libs/path" "github.com/trustwallet/assets-go-libs/validation" "github.com/trustwallet/assets-go-libs/validation/info" "github.com/trustwallet/assets-go-libs/validation/list" "github.com/trustwallet/assets-go-libs/validation/tokenlist" "github.com/trustwallet/assets/internal/config" - "github.com/trustwallet/assets/internal/file" "github.com/trustwallet/go-primitives/coin" ) @@ -38,7 +37,7 @@ func (s *Service) ValidateJSON(f *file.AssetFile) error { } func (s *Service) ValidateRootFolder(f *file.AssetFile) error { - dirFiles, err := filelib.ReadDir(f.Path()) + dirFiles, err := file.ReadDir(f.Path()) if err != nil { return err } @@ -110,7 +109,7 @@ func (s *Service) ValidateImage(f *file.AssetFile) error { } func (s *Service) ValidateAssetFolder(f *file.AssetFile) error { - dirFiles, err := filelib.ReadDir(f.Path()) + dirFiles, err := file.ReadDir(f.Path()) if err != nil { return err } @@ -134,7 +133,7 @@ func (s *Service) ValidateAssetFolder(f *file.AssetFile) error { assetInfoPath := path.GetAssetInfoPath(f.Chain().Handle, f.Asset()) var infoJson info.AssetModel - if err = filelib.ReadJSONFile(assetInfoPath, &infoJson); err != nil { + if err = file.ReadJSONFile(assetInfoPath, &infoJson); err != nil { return err } @@ -151,7 +150,7 @@ func (s *Service) ValidateAssetFolder(f *file.AssetFile) error { } func (s *Service) ValidateDappsFolder(f *file.AssetFile) error { - dirFiles, err := filelib.ReadDir(f.Path()) + dirFiles, err := file.ReadDir(f.Path()) if err != nil { return err } @@ -179,7 +178,7 @@ func (s *Service) ValidateDappsFolder(f *file.AssetFile) error { func (s *Service) ValidateChainInfoFile(f *file.AssetFile) error { var coinInfo info.CoinModel - if err := filelib.ReadJSONFile(f.Path(), &coinInfo); err != nil { + if err := file.ReadJSONFile(f.Path(), &coinInfo); err != nil { return err } @@ -203,7 +202,7 @@ func (s *Service) ValidateChainInfoFile(f *file.AssetFile) error { func (s *Service) ValidateAssetInfoFile(f *file.AssetFile) error { var assetInfo info.AssetModel - if err := filelib.ReadJSONFile(f.Path(), &assetInfo); err != nil { + if err := file.ReadJSONFile(f.Path(), &assetInfo); err != nil { return err } @@ -221,7 +220,7 @@ func (s *Service) ValidateValidatorsListFile(f *file.AssetFile) error { } var model []list.Model - if err := filelib.ReadJSONFile(f.Path(), &model); err != nil { + if err := file.ReadJSONFile(f.Path(), &model); err != nil { return err } @@ -238,7 +237,7 @@ func (s *Service) ValidateValidatorsListFile(f *file.AssetFile) error { assetsPath := path.GetValidatorAssetsPath(f.Chain().Handle) assetFolder := s.fileService.GetAssetFile(assetsPath) - dirFiles, err := filelib.ReadDir(assetFolder.Path()) + dirFiles, err := file.ReadDir(assetFolder.Path()) if err != nil { return err } @@ -277,14 +276,14 @@ func (s *Service) ValidateTokenListExtendedFile(f *file.AssetFile) error { func validateTokenList(path1, path2 string, chain1 coin.Coin) error { var tokenList1 tokenlist.Model - err := filelib.ReadJSONFile(path1, &tokenList1) + err := file.ReadJSONFile(path1, &tokenList1) if err != nil { return err } - if filelib.Exists(path2) { + if file.Exists(path2) { var tokenList2 tokenlist.Model - err = filelib.ReadJSONFile(path2, &tokenList2) + err = file.ReadJSONFile(path2, &tokenList2) if err != nil { return err } @@ -311,7 +310,7 @@ func validateTokenList(path1, path2 string, chain1 coin.Coin) error { } func (s *Service) ValidateInfoFolder(f *file.AssetFile) error { - dirFiles, err := filelib.ReadDir(f.Path()) + dirFiles, err := file.ReadDir(f.Path()) if err != nil { return err } @@ -325,7 +324,7 @@ func (s *Service) ValidateInfoFolder(f *file.AssetFile) error { } func (s *Service) ValidateValidatorsAssetFolder(f *file.AssetFile) error { - dirFiles, err := filelib.ReadDir(f.Path()) + dirFiles, err := file.ReadDir(f.Path()) if err != nil { return err } diff --git a/internal/service/service.go b/internal/service/service.go index e2c68686a..e9cd31382 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -1,8 +1,8 @@ package service import ( + "github.com/trustwallet/assets-go-libs/file" "github.com/trustwallet/assets-go-libs/validation" - "github.com/trustwallet/assets/internal/file" "github.com/trustwallet/assets/internal/processor" "github.com/trustwallet/assets/internal/report"