Transfer file package to assets-go-libs (#17984)

This commit is contained in:
daniel 2022-02-09 14:03:36 +03:00 committed by GitHub
parent 035fce8126
commit 004e5cdbb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 29 additions and 308 deletions

2
go.mod
View File

@ -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
)

4
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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"
)

View File

@ -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"

View File

@ -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

View File

@ -1,7 +1,7 @@
package processor
import (
"github.com/trustwallet/assets/internal/file"
"github.com/trustwallet/assets-go-libs/file"
)
type (

View File

@ -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 {

View File

@ -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
}

View File

@ -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"