github.com/diadata-org/diadata@v1.4.593/pkg/dia/helpers/configCollectors/configCollectors.go (about)

     1  package configCollectors
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/user"
     7  	"path"
     8  
     9  	"github.com/diadata-org/diadata/pkg/dia"
    10  	logrus "github.com/sirupsen/logrus"
    11  	"github.com/tkanos/gonfig"
    12  )
    13  
    14  var log = logrus.New()
    15  
    16  func (c *ConfigCollectors) Exchanges() []string {
    17  	return []string{dia.BinanceExchange, dia.BitfinexExchange, dia.CoinBaseExchange, dia.KrakenExchange, dia.UnknownExchange}
    18  }
    19  
    20  type ConfigCollectors struct {
    21  	Coins []dia.ExchangePair
    22  }
    23  
    24  func (c *ConfigCollectors) AllPairs() []dia.ExchangePair {
    25  	founds := map[string]bool{}
    26  	result := []dia.ExchangePair{}
    27  	for _, configPair := range c.Coins {
    28  		if _, ok := founds[configPair.ForeignName]; !ok {
    29  			founds[configPair.ForeignName] = true
    30  			result = append(result, configPair)
    31  		}
    32  	}
    33  	return result
    34  }
    35  
    36  func (c *ConfigCollectors) IsSymbolInConfig(symbol string) bool {
    37  	for _, configPair := range c.Coins {
    38  		if configPair.Symbol == symbol {
    39  			return true
    40  		}
    41  	}
    42  	return false
    43  }
    44  
    45  // ConfigFileConnectors returns a path to folder @exchange in config folder if @filteype is empty.
    46  // If @filteype is a filetype it returns the path to file @exchange as a @filteype file.
    47  func ConfigFileConnectors(exchange string, filetype string) string {
    48  	usr, _ := user.Current()
    49  	dir := usr.HomeDir
    50  	if dir == "/root" || dir == "/home" {
    51  		return "/config/" + exchange + filetype //hack for docker...
    52  	}
    53  	if dir == "/home/travis" {
    54  		return "../config/" + exchange + filetype //hack for travis
    55  	}
    56  	if configDir := os.Getenv("DIA_CONFIG_DIR"); configDir != "" { //hack for local tests.
    57  		return path.Join(configDir, exchange+filetype)
    58  	}
    59  	return os.Getenv("GOPATH") + "/src/github.com/diadata-org/diadata/config/" + exchange + filetype
    60  }
    61  
    62  func NewConfigCollectorsIfExists(exchange string, filetype string) *ConfigCollectors {
    63  	var connectorConfig = ConfigCollectors{
    64  		Coins: []dia.ExchangePair{},
    65  	}
    66  
    67  	file := ConfigFileConnectors(exchange, filetype)
    68  	err := gonfig.GetConf(file, &connectorConfig)
    69  	if err != nil {
    70  		log.Error("error loading <", file, "> ", err)
    71  		return nil
    72  	} else {
    73  		log.Printf("loaded  <%v>", file)
    74  	}
    75  
    76  	return &connectorConfig
    77  }
    78  
    79  func NewConfigCollectors(exchange string, filetype string) *ConfigCollectors {
    80  	cc := NewConfigCollectorsIfExists(exchange, filetype)
    81  	if cc == nil {
    82  		log.Fatal("error in NewConfigCollectors")
    83  	}
    84  	return cc
    85  }
    86  
    87  // ReadJSONFromConfig reads a json file from the config folder and returns the byte slice of items.
    88  func ReadJSONFromConfig(filename string) (content []byte, err error) {
    89  	var (
    90  		jsonFile *os.File
    91  	)
    92  	jsonFile, err = os.Open(ConfigFileConnectors(filename, ".json"))
    93  	if err != nil {
    94  		return
    95  	}
    96  	defer func() {
    97  		cerr := jsonFile.Close()
    98  		if err == nil {
    99  			err = cerr
   100  		}
   101  	}()
   102  
   103  	content, err = ioutil.ReadAll(jsonFile)
   104  	if err != nil {
   105  		return
   106  	}
   107  	return
   108  }