2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-02-02 22:15:09 +00:00
|
|
|
package config
|
2017-10-19 14:22:12 +00:00
|
|
|
|
2019-01-31 01:38:56 +00:00
|
|
|
import (
|
2019-03-07 17:02:52 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-01-31 01:38:56 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2019-02-02 22:15:09 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/plugin/helpers"
|
2019-01-31 01:38:56 +00:00
|
|
|
)
|
|
|
|
|
2019-02-02 22:15:09 +00:00
|
|
|
type Plugin struct {
|
2019-02-13 19:04:07 +00:00
|
|
|
Transformers map[string]Transformer
|
2019-01-31 01:38:56 +00:00
|
|
|
FilePath string
|
|
|
|
FileName string
|
2019-01-31 23:14:42 +00:00
|
|
|
Save bool
|
2019-02-18 11:23:36 +00:00
|
|
|
Home string
|
2019-01-31 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:04:07 +00:00
|
|
|
type Transformer struct {
|
|
|
|
Path string
|
|
|
|
Type TransformerType
|
|
|
|
MigrationPath string
|
2019-03-12 15:46:42 +00:00
|
|
|
MigrationRank uint64
|
2019-02-13 19:04:07 +00:00
|
|
|
RepositoryPath string
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
func (pluginConfig *Plugin) GetPluginPaths() (string, string, error) {
|
|
|
|
path, err := helpers.CleanPath(pluginConfig.FilePath)
|
2019-01-31 01:38:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
name := strings.Split(pluginConfig.FileName, ".")[0]
|
2019-01-31 01:38:56 +00:00
|
|
|
goFile := filepath.Join(path, name+".go")
|
|
|
|
soFile := filepath.Join(path, name+".so")
|
|
|
|
|
|
|
|
return goFile, soFile, nil
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:02:52 +00:00
|
|
|
// Removes duplicate migration paths and returns them in ranked order
|
2019-03-13 16:14:35 +00:00
|
|
|
func (pluginConfig *Plugin) GetMigrationsPaths() ([]string, error) {
|
2019-03-12 15:46:42 +00:00
|
|
|
paths := make(map[uint64]string)
|
|
|
|
highestRank := -1
|
2019-03-13 16:14:35 +00:00
|
|
|
for name, transformer := range pluginConfig.Transformers {
|
2019-02-13 19:04:07 +00:00
|
|
|
repo := transformer.RepositoryPath
|
|
|
|
mig := transformer.MigrationPath
|
2019-03-13 16:14:35 +00:00
|
|
|
path := filepath.Join("$GOPATH/src", pluginConfig.Home, "vendor", repo, mig)
|
2019-02-02 22:15:09 +00:00
|
|
|
cleanPath, err := helpers.CleanPath(path)
|
2019-01-31 01:38:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-03-07 17:02:52 +00:00
|
|
|
// If there is a different path with the same rank then we have a conflict
|
|
|
|
_, ok := paths[transformer.MigrationRank]
|
|
|
|
if ok {
|
|
|
|
conflictingPath := paths[transformer.MigrationRank]
|
|
|
|
if conflictingPath != cleanPath {
|
|
|
|
return nil, errors.New(fmt.Sprintf("transformer %s has the same migration rank (%d) as another transformer", name, transformer.MigrationRank))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paths[transformer.MigrationRank] = cleanPath
|
2019-03-12 15:46:42 +00:00
|
|
|
if int(transformer.MigrationRank) >= highestRank {
|
|
|
|
highestRank = int(transformer.MigrationRank)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for gaps and duplicates
|
|
|
|
if len(paths) != (highestRank + 1) {
|
|
|
|
return []string{}, errors.New("number of distinct ranks does not match number of distinct migration paths")
|
|
|
|
}
|
|
|
|
if anyDupes(paths) {
|
|
|
|
return []string{}, errors.New("duplicate paths with different ranks present")
|
2019-03-07 17:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sortedPaths := make([]string, len(paths))
|
|
|
|
for rank, path := range paths {
|
|
|
|
sortedPaths[rank] = path
|
2019-01-31 01:38:56 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:02:52 +00:00
|
|
|
return sortedPaths, nil
|
2017-10-19 14:22:12 +00:00
|
|
|
}
|
2019-02-07 21:35:25 +00:00
|
|
|
|
2019-02-13 19:04:07 +00:00
|
|
|
// Removes duplicate repo paths before returning them
|
2019-03-13 16:14:35 +00:00
|
|
|
func (pluginConfig *Plugin) GetRepoPaths() map[string]bool {
|
2019-02-13 19:04:07 +00:00
|
|
|
paths := make(map[string]bool)
|
2019-03-13 16:14:35 +00:00
|
|
|
for _, transformer := range pluginConfig.Transformers {
|
2019-02-13 19:04:07 +00:00
|
|
|
paths[transformer.RepositoryPath] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransformerType int
|
2019-02-07 21:35:25 +00:00
|
|
|
|
|
|
|
const (
|
2019-02-13 19:04:07 +00:00
|
|
|
UnknownTransformerType TransformerType = iota
|
2019-02-07 21:35:25 +00:00
|
|
|
EthEvent
|
|
|
|
EthStorage
|
2019-03-13 16:14:35 +00:00
|
|
|
EthContract
|
2019-02-07 21:35:25 +00:00
|
|
|
)
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
func (transformerType TransformerType) String() string {
|
2019-02-07 21:35:25 +00:00
|
|
|
names := [...]string{
|
2019-02-13 21:00:09 +00:00
|
|
|
"Unknown",
|
2019-02-07 21:35:25 +00:00
|
|
|
"eth_event",
|
|
|
|
"eth_storage",
|
2019-03-13 16:14:35 +00:00
|
|
|
"eth_contract",
|
2019-02-07 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
if transformerType > EthContract || transformerType < EthEvent {
|
2019-02-07 21:35:25 +00:00
|
|
|
return "Unknown"
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
return names[transformerType]
|
2019-02-07 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 19:04:07 +00:00
|
|
|
func GetTransformerType(str string) TransformerType {
|
|
|
|
types := [...]TransformerType{
|
2019-02-07 21:35:25 +00:00
|
|
|
EthEvent,
|
|
|
|
EthStorage,
|
2019-03-13 16:14:35 +00:00
|
|
|
EthContract,
|
2019-02-07 21:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ty := range types {
|
2019-02-13 21:00:09 +00:00
|
|
|
if ty.String() == str {
|
2019-02-07 21:35:25 +00:00
|
|
|
return ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UnknownTransformerType
|
|
|
|
}
|
2019-03-12 15:46:42 +00:00
|
|
|
|
|
|
|
func anyDupes(list map[uint64]string) bool {
|
|
|
|
seen := make([]string, 0, len(list))
|
|
|
|
for _, str := range list {
|
|
|
|
dupe := inList(str, seen)
|
|
|
|
if dupe {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
seen = append(seen, str)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func inList(str string, list []string) bool {
|
|
|
|
for _, element := range list {
|
|
|
|
if str == element {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|