2021-12-19 21:39:53 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2021-12-23 19:30:20 +00:00
|
|
|
"fmt"
|
2021-12-19 21:39:53 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
"github.com/trustwallet/assets-go-libs/strings"
|
2021-12-19 21:39:53 +00:00
|
|
|
"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$`)
|
|
|
|
|
2022-02-03 20:36:45 +00:00
|
|
|
regexTokenListFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/tokenlist.json$`)
|
|
|
|
regexTokenListExtendedFile = regexp.MustCompile(`./blockchains/(\w+[\-]\w+|\w+)/tokenlist-extended.json$`)
|
2021-12-19 21:39:53 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
|
2022-02-03 20:36:45 +00:00
|
|
|
TypeTokenListFile: regexTokenListFile,
|
|
|
|
TypeTokenListExtendedFile: regexTokenListExtendedFile,
|
2021-12-19 21:39:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-12-20 15:03:08 +00:00
|
|
|
if strings.Contains(path, filesToSkip) {
|
2021-12-19 21:39:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-23 19:30:20 +00:00
|
|
|
paths = append(paths, fmt.Sprintf("./%s", path))
|
2021-12-19 21:39:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths, nil
|
|
|
|
}
|