Skip to content

Commit

Permalink
Split up common.go, file path fixes and much more
Browse files Browse the repository at this point in the history
  • Loading branch information
thrasher- committed Jun 4, 2019
1 parent 8c62316 commit e965e54
Show file tree
Hide file tree
Showing 74 changed files with 524 additions and 617 deletions.
2 changes: 1 addition & 1 deletion cmd/documentation/common_templates/common_readme.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "github.com/thrasher-/gocryptotrader/common"

testString := "aAaAa"

upper := common.StringToUpper(testString)
upper := strings.ToUpper(testString)

// upper == "AAAAA"
```
Expand Down
37 changes: 24 additions & 13 deletions cmd/documentation/documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"html/template"
"log"
"os"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -118,7 +119,7 @@ func main() {
codebasePaths = make(map[string]string)
codebaseTemplatePath = make(map[string]string)
codebaseReadme = make(map[string]readme)
path = common.GetOSPathSlash()
path = getOSPathSlash()

if err := getContributorList(); err != nil {
log.Fatal("GoCryptoTrader: Exchange documentation tool GET error ", err)
Expand All @@ -139,6 +140,16 @@ func main() {
fmt.Println("\nTool finished")
}

// getOSPathSlash returns the slash used by the operating systems
// file system
// TO-DO: Change all paths to not use this
func getOSPathSlash() string {
if runtime.GOOS == "windows" {
return "\\"
}
return "/"
}

// updateReadme iterates through codebase paths to check for readme files and either adds
// or replaces with new readme files.
func updateReadme() error {
Expand Down Expand Up @@ -295,18 +306,18 @@ func getslashFromName(packageName string) string {
}

var globS = []string{
fmt.Sprintf("common_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("communications_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("config_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("currency_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("events_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("exchanges_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("portfolio_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("root_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("sub_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("testdata_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("tools_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("web_templates%s*", common.GetOSPathSlash()),
fmt.Sprintf("common_templates%s*", getOSPathSlash()),
fmt.Sprintf("communications_templates%s*", getOSPathSlash()),
fmt.Sprintf("config_templates%s*", getOSPathSlash()),
fmt.Sprintf("currency_templates%s*", getOSPathSlash()),
fmt.Sprintf("events_templates%s*", getOSPathSlash()),
fmt.Sprintf("exchanges_templates%s*", getOSPathSlash()),
fmt.Sprintf("portfolio_templates%s*", getOSPathSlash()),
fmt.Sprintf("root_templates%s*", getOSPathSlash()),
fmt.Sprintf("sub_templates%s*", getOSPathSlash()),
fmt.Sprintf("testdata_templates%s*", getOSPathSlash()),
fmt.Sprintf("tools_templates%s*", getOSPathSlash()),
fmt.Sprintf("web_templates%s*", getOSPathSlash()),
}

// addTemplates adds all the template files
Expand Down
41 changes: 16 additions & 25 deletions cmd/exchange_template/exchange_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"log"
"os"
"os/exec"

"github.com/thrasher-/gocryptotrader/currency"
"github.com/thrasher-/gocryptotrader/exchanges/assets"
"path/filepath"
"strings"

"github.com/thrasher-/gocryptotrader/common"
"github.com/thrasher-/gocryptotrader/config"
"github.com/thrasher-/gocryptotrader/currency"
"github.com/thrasher-/gocryptotrader/exchanges/assets"
)

const (
Expand All @@ -22,9 +23,9 @@ const (
packageMain = "%s.go"
packageReadme = "README.md"

exchangePackageLocation = "..%s..%sexchanges%s"
exchangeLocation = "..%s..%sexchange.go"
exchangeConfigPath = "..%s..%stestdata%sconfigtest.json"
exchangePackageLocation = "../../exchanges"
exchangeLocation = "../../exchange.go"
exchangeConfigPath = "../../testdata/configtest.json"
)

var (
Expand Down Expand Up @@ -84,9 +85,9 @@ func main() {
log.Fatal("GoCryptoTrader: Exchange templating tool stopped...")
}

newExchangeName = common.StringToLower(newExchangeName)
newExchangeName = strings.ToLower(newExchangeName)
v := newExchangeName[:1]
capName := common.StringToUpper(v) + newExchangeName[1:]
capName := strings.ToUpper(v) + newExchangeName[1:]

exch := exchange{
Name: newExchangeName,
Expand All @@ -97,18 +98,14 @@ func main() {
FIX: fixSupport,
}

osPathSlash := common.GetOSPathSlash()
exchangeJSON := fmt.Sprintf(exchangeConfigPath, osPathSlash, osPathSlash, osPathSlash)

configTestFile := config.GetConfig()
err = configTestFile.LoadConfig(exchangeJSON)
err = configTestFile.LoadConfig(exchangeConfigPath)
if err != nil {
log.Fatal("GoCryptoTrader: Exchange templating configuration retrieval error ", err)
}
// NOTE need to nullify encrypt configuration

var configTestExchanges []string

for x := range configTestFile.Exchanges {
configTestExchanges = append(configTestExchanges, configTestFile.Exchanges[x].Name)
}
Expand Down Expand Up @@ -137,18 +134,12 @@ func main() {
log.Fatal("GoCryptoTrader: Exchange templating configuration error - cannot save")
}

exchangeDirectory = fmt.Sprintf(
exchangePackageLocation+newExchangeName+"%s",
osPathSlash,
osPathSlash,
osPathSlash,
osPathSlash)

exchangeTest = fmt.Sprintf(exchangeDirectory+packageTests, newExchangeName)
exchangeTypes = fmt.Sprintf(exchangeDirectory+packageTypes, newExchangeName)
exchangeWrapper = fmt.Sprintf(exchangeDirectory+packageWrapper, newExchangeName)
exchangeMain = fmt.Sprintf(exchangeDirectory+packageMain, newExchangeName)
exchangeReadme = exchangeDirectory + packageReadme
exchangeDirectory = filepath.Join(exchangePackageLocation, newExchangeName)
exchangeTest = filepath.Join(exchangeDirectory, fmt.Sprintf(packageTests, newExchangeName))
exchangeTypes = filepath.Join(exchangeDirectory, fmt.Sprintf(packageTypes, newExchangeName))
exchangeWrapper = filepath.Join(exchangeDirectory, fmt.Sprintf(packageWrapper, newExchangeName))
exchangeMain = filepath.Join(exchangeDirectory, fmt.Sprintf(packageMain, newExchangeName))
exchangeReadme = filepath.Join(exchangeDirectory, packageReadme)

err = os.Mkdir(exchangeDirectory, 0700)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/exchange_template/main_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func ({{.Variable}} *{{.CapitalName}}) Setup(exch *config.ExchangeConfig) error
{{.Variable}}.SetHTTPClientUserAgent(exch.HTTPUserAgent)
{{.Variable}}.Verbose = exch.Verbose
{{.Variable}}.Websocket.SetWsStatusAndConnection(exch.Features.Enabled.Websocket)
{{.Variable}}.BaseCurrencies = common.SplitStrings(exch.BaseCurrencies, ",")
{{.Variable}}.AvailablePairs = common.SplitStrings(exch.AvailablePairs, ",")
{{.Variable}}.EnabledPairs = common.SplitStrings(exch.EnabledPairs, ",")
{{.Variable}}.BaseCurrencies = strings.Split(exch.BaseCurrencies, ",")
{{.Variable}}.AvailablePairs = strings.Split(exch.AvailablePairs, ",")
{{.Variable}}.EnabledPairs = strings.Split(exch.EnabledPairs, ",")
err := {{.Variable}}.SetCurrencyPairFormat()
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import "github.com/thrasher-/gocryptotrader/common"

testString := "aAaAa"

upper := common.StringToUpper(testString)
upper := strings.ToUpper(testString)

// upper == "AAAAA"
```
Expand Down
108 changes: 6 additions & 102 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -81,12 +80,6 @@ func StringSliceDifference(slice1, slice2 []string) []string {
return diff
}

// StringContains checks a substring if it contains your input then returns a
// bool
func StringContains(input, substring string) bool {
return strings.Contains(input, substring)
}

// StringDataContains checks the substring array with an input and returns a bool
func StringDataContains(haystack []string, needle string) bool {
data := strings.Join(haystack, ",")
Expand Down Expand Up @@ -118,45 +111,13 @@ func StringDataCompareInsensitive(haystack []string, needle string) bool {
// a bool irrespective of lower or upper case strings
func StringDataContainsInsensitive(haystack []string, needle string) bool {
for _, data := range haystack {
if strings.Contains(StringToUpper(data), StringToUpper(needle)) {
if strings.Contains(strings.ToUpper(data), strings.ToUpper(needle)) {
return true
}
}
return false
}

// JoinStrings joins an array together with the required separator and returns
// it as a string
func JoinStrings(input []string, separator string) string {
return strings.Join(input, separator)
}

// SplitStrings splits blocks of strings from string into a string array using
// a separator ie "," or "_"
func SplitStrings(input, separator string) []string {
return strings.Split(input, separator)
}

// TrimString trims unwanted prefixes or postfixes
func TrimString(input, cutset string) string {
return strings.Trim(input, cutset)
}

// ReplaceString replaces a string with another
func ReplaceString(input, old, newStr string, n int) string {
return strings.Replace(input, old, newStr, n)
}

// StringToUpper changes strings to uppercase
func StringToUpper(input string) string {
return strings.ToUpper(input)
}

// StringToLower changes strings to lowercase
func StringToLower(input string) string {
return strings.ToLower(input)
}

// IsEnabled takes in a boolean param and returns a string if it is enabled
// or disabled
func IsEnabled(isEnabled bool) string {
Expand All @@ -170,7 +131,7 @@ func IsEnabled(isEnabled bool) string {
// regexp package // Validation issues occurring because "3" is contained in
// litecoin and Bitcoin addresses - non-fatal
func IsValidCryptoAddress(address, crypto string) (bool, error) {
switch StringToLower(crypto) {
switch strings.ToLower(crypto) {
case "btc":
return regexp.MatchString("^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$", address)
case "ltc":
Expand All @@ -184,7 +145,7 @@ func IsValidCryptoAddress(address, crypto string) (bool, error) {

// YesOrNo returns a boolean variable to check if input is "y" or "yes"
func YesOrNo(input string) bool {
if StringToLower(input) == "y" || StringToLower(input) == "yes" {
if strings.ToLower(input) == "y" || strings.ToLower(input) == "yes" {
return true
}
return false
Expand Down Expand Up @@ -276,7 +237,7 @@ func JSONEncode(v interface{}) ([]byte, error) {

// JSONDecode decodes JSON data into a structure
func JSONDecode(data []byte, to interface{}) error {
if !StringContains(reflect.ValueOf(to).Type().String(), "*") {
if !strings.Contains(reflect.ValueOf(to).Type().String(), "*") {
return errors.New("json decode error - memory address not supplied")
}
return json.Unmarshal(data, to)
Expand All @@ -294,7 +255,7 @@ func EncodeURLValues(urlPath string, values url.Values) string {

// ExtractHost returns the hostname out of a string
func ExtractHost(address string) string {
host := SplitStrings(address, ":")[0]
host := strings.Split(address, ":")[0]
if host == "" {
return "localhost"
}
Expand All @@ -303,7 +264,7 @@ func ExtractHost(address string) string {

// ExtractPort returns the port name out of a string
func ExtractPort(host string) int {
portStr := SplitStrings(host, ":")[1]
portStr := strings.Split(host, ":")[1]
port, _ := strconv.Atoi(portStr)
return port
}
Expand Down Expand Up @@ -386,15 +347,6 @@ func GetExecutablePath() (string, error) {
return filepath.Dir(ex), nil
}

// GetOSPathSlash returns the slash used by the operating systems
// file system
func GetOSPathSlash() string {
if runtime.GOOS == "windows" {
return "\\"
}
return "/"
}

// UnixMillis converts a UnixNano timestamp to milliseconds
func UnixMillis(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
Expand All @@ -405,54 +357,6 @@ func RecvWindow(d time.Duration) int64 {
return int64(d) / int64(time.Millisecond)
}

// FloatFromString format
func FloatFromString(raw interface{}) (float64, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
flt, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, fmt.Errorf("could not convert value: %s Error: %s", str, err)
}
return flt, nil
}

// IntFromString format
func IntFromString(raw interface{}) (int, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
n, err := strconv.Atoi(str)
if err != nil {
return 0, fmt.Errorf("unable to parse as int: %T", raw)
}
return n, nil
}

// Int64FromString format
func Int64FromString(raw interface{}) (int64, error) {
str, ok := raw.(string)
if !ok {
return 0, fmt.Errorf("unable to parse, value not string: %T", raw)
}
n, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0, fmt.Errorf("unable to parse as int64: %T", raw)
}
return n, nil
}

// TimeFromUnixTimestampFloat format
func TimeFromUnixTimestampFloat(raw interface{}) (time.Time, error) {
ts, ok := raw.(float64)
if !ok {
return time.Time{}, fmt.Errorf("unable to parse, value not float64: %T", raw)
}
return time.Unix(0, int64(ts)*int64(time.Millisecond)), nil
}

// GetDefaultDataDir returns the default data directory
// Windows - C:\Users\%USER%\AppData\Roaming\GoCryptoTrader
// Linux/Unix or OSX - $HOME/.gocryptotrader
Expand Down
Loading

0 comments on commit e965e54

Please sign in to comment.