Skip to content

Commit

Permalink
Improved code quality (thrasher-corp#154)
Browse files Browse the repository at this point in the history
* Removed package-lock.json form gitignore as it ensures specific package versions

* Updated all @angular web dependencies

* Resolved tslint errors using autofix option

* Resolved some more tslint issues

* Added lint scripts to package.json to easy lint the ts files

* Updated codelyzer and tslint

* Run web on travis using node 10 and run the lint task

* Resolved some more tslint issues after upgrading tslint and codelyzer

* Resolved golint issues with regards to exchange comments

* Resolved spelling errors shown by goreportcard.com

* Resolved gofmt warnings using goreportcard.com

* Resolved golint issue by removing unrequired else statement

* Refactored slack.go to reduce cyclomatic complexity

* Fixed govet issue where Slack was passed as value instead of reference
  • Loading branch information
marcofranssen authored and thrasher- committed Jul 18, 2018
1 parent a5f5132 commit 0f20916
Show file tree
Hide file tree
Showing 71 changed files with 1,025 additions and 971 deletions.
50 changes: 30 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
language: go
matrix:
include:
- language: node_js
node_js:
- '10'
- '8'
- '6'
before_install:
- cd web/
install:
- npm install
script:
- npm run lint
- npm run build

go:
- 1.10.x

before_install:
- go get -t -v ./...

script:
- ./testdata/test.sh

install:
- go get github.com/gorilla/websocket
- go get github.com/toorop/go-pusher
- go get github.com/thrasher-/socketio
- go get github.com/beatgammit/turnpike
- go get github.com/gorilla/mux
- go get golang.org/x/crypto/scrypt

after_success:
- bash <(curl -s https://codecov.io/bash)
- language: go
go:
- 1.10.x
before_install:
- go get -t -v ./...
script:
- ./testdata/test.sh
install:
- go get github.com/gorilla/websocket
- go get github.com/toorop/go-pusher
- go get github.com/thrasher-/socketio
- go get github.com/beatgammit/turnpike
- go get github.com/gorilla/mux
- go get golang.org/x/crypto/scrypt
after_success:
- bash <(curl -s https://codecov.io/bash)
2 changes: 1 addition & 1 deletion common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func initialiseHTTPClient() {
}
}

// NewHTTPClientWithTimeout initalises a new HTTP client with the specified
// NewHTTPClientWithTimeout initialises a new HTTP client with the specified
// timeout duration
func NewHTTPClientWithTimeout(t time.Duration) *http.Client {
h := &http.Client{Timeout: t}
Expand Down
133 changes: 82 additions & 51 deletions communications/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Slack struct {
}

// Setup takes in a slack configuration, sets bots target channel and
// sets verfication token to access workspace
// sets verification token to access workspace
func (s *Slack) Setup(config config.CommunicationsConfig) {
s.Name = config.SlackConfig.Name
s.Enabled = config.SlackConfig.Enabled
Expand Down Expand Up @@ -213,82 +213,113 @@ func (s *Slack) WebsocketReader() {
switch data.Type {

case "error":
if data.Error.Msg == "Socket URL has expired" {
if s.Verbose {
log.Println("Slack websocket URL has expired.. Reconnecting")
}
if err = s.WebsocketConn.Close(); err != nil {
log.Println(err)
}
s.ReconnectURL = ""
s.Connected = false
if err := s.NewConnection(); err != nil {
log.Fatal(err)
}
return
}
s.handleErrorResponse(data)

case "hello":
if s.Verbose {
log.Println("Websocket connected successfully.")
}
s.Connected = true
go s.WebsocketKeepAlive()
s.handleHelloResponse(data)

case "reconnect_url":
type reconnectResponse struct {
URL string `json:"url"`
}
var recURL reconnectResponse
err = common.JSONDecode(resp, &recURL)
err = s.handleReconnectResponse(resp)
if err != nil {
continue
}
s.ReconnectURL = recURL.URL
if s.Verbose {
log.Printf("Reconnect URL set to %s\n", s.ReconnectURL)
}

case "presence_change":
var pres PresenceChange
err = common.JSONDecode(resp, &pres)
err = s.handlePresenceChange(resp)
if err != nil {
continue
}
if s.Verbose {
log.Printf("Presence change. User %s [%s] changed status to %s\n",
s.GetUsernameByID(pres.User),
pres.User, pres.Presence)
}

case "message":
if data.ReplyTo != 0 {
continue
}
var msg Message
err = common.JSONDecode(resp, &msg)
err = s.handleMessageResponse(resp, data)
if err != nil {
continue
}
if s.Verbose {
log.Printf("Msg received by %s [%s] with text: %s\n",
s.GetUsernameByID(msg.User),
msg.User, msg.Text)
}
if string(msg.Text[0]) == "!" {
s.HandleMessage(msg)
}

case "pong":
if s.Verbose {
log.Println("Pong recieved from server")
log.Println("Pong received from server")
}
default:
log.Println(string(resp))
}
}
}

func (s *Slack) handlePresenceChange(resp []byte) error {
var pres PresenceChange
err := common.JSONDecode(resp, &pres)
if err != nil {
return err
}
if s.Verbose {
log.Printf("Presence change. User %s [%s] changed status to %s\n",
s.GetUsernameByID(pres.User),
pres.User, pres.Presence)
}
return nil
}

func (s *Slack) handleMessageResponse(resp []byte, data WebsocketResponse) error {
if data.ReplyTo != 0 {
return fmt.Errorf("ReplyTo != 0")
}
var msg Message
err := common.JSONDecode(resp, &msg)
if err != nil {
return err
}
if s.Verbose {
log.Printf("Msg received by %s [%s] with text: %s\n",
s.GetUsernameByID(msg.User),
msg.User, msg.Text)
}
if string(msg.Text[0]) == "!" {
s.HandleMessage(msg)
}
return nil
}
func (s *Slack) handleErrorResponse(data WebsocketResponse) {
if data.Error.Msg == "Socket URL has expired" {
if s.Verbose {
log.Println("Slack websocket URL has expired.. Reconnecting")
}
if err := s.WebsocketConn.Close(); err != nil {
log.Println(err)
}
s.ReconnectURL = ""
s.Connected = false
if err := s.NewConnection(); err != nil {
log.Fatal(err)
}
return
}
}

func (s *Slack) handleHelloResponse(data WebsocketResponse) {
if s.Verbose {
log.Println("Websocket connected successfully.")
}
s.Connected = true
go s.WebsocketKeepAlive()
}

func (s *Slack) handleReconnectResponse(resp []byte) error {
type reconnectResponse struct {
URL string `json:"url"`
}
var recURL reconnectResponse
err := common.JSONDecode(resp, &recURL)
if err != nil {
return err
}
s.ReconnectURL = recURL.URL
if s.Verbose {
log.Printf("Reconnect URL set to %s\n", s.ReconnectURL)
}
return nil
}

// WebsocketKeepAlive sends a ping every 5 minutes to keep connection alive
func (s *Slack) WebsocketKeepAlive() {
ticker := time.NewTicker(5 * time.Minute)
Expand Down Expand Up @@ -319,7 +350,7 @@ func (s *Slack) WebsocketSend(eventType, text string) error {
}

// HandleMessage handles incoming messages and/or commands from slack
func (s Slack) HandleMessage(msg Message) {
func (s *Slack) HandleMessage(msg Message) {
switch {
case common.StringContains(msg.Text, cmdStatus):
s.WebsocketSend("message", s.GetStatus())
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (c *Config) CheckCommunicationsConfig() error {
Password: "test",

Contacts: []SMSContact{
SMSContact{
{
Name: "bob",
Number: "1234",
Enabled: false,
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestUpdateExchangeBankAccounts(t *testing.T) {
t.Error("Test failed. UpdateDepositBankAccounts LoadConfig error", err)
}

b := []BankAccount{BankAccount{Enabled: false}}
b := []BankAccount{{Enabled: false}}
err = cfg.UpdateExchangeBankAccounts("Bitfinex", b)
if err != nil {
t.Error("Test failed. UpdateDepositBankAccounts error", err)
Expand Down
2 changes: 1 addition & 1 deletion currency/forexprovider/base/base_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (fxp IFXProviders) GetCurrencyData(baseCurrency, symbols string) (map[strin
return rates, nil
}
}
return nil, errors.New("ForexProvider error GetCurrencyData() failed to aquire data")
return nil, errors.New("ForexProvider error GetCurrencyData() failed to acquire data")
}
return rates, nil
}
Expand Down
4 changes: 2 additions & 2 deletions exchanges/alphapoint/alphapoint_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ func (a *Alphapoint) GetExchangeDepositAddress(cryptocurrency pair.CurrencyItem)
return "", errors.New("associated currency address not found")
}

// WithdrawExchangeCryptoFunds returns a withdrawal ID when a withdrawal is
// WithdrawCryptoExchangeFunds returns a withdrawal ID when a withdrawal is
// submitted
func (a *Alphapoint) WithdrawCryptoExchangeFunds(address string, cryptocurrency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFunds returns a withdrawal ID when a withdrawal is submitted
// WithdrawFiatExchangeFunds returns a withdrawal ID when a withdrawal is submitted
func (a *Alphapoint) WithdrawFiatExchangeFunds(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}
8 changes: 4 additions & 4 deletions exchanges/anx/anx_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,20 @@ func (a *ANX) GetExchangeDepositAddress(cryptocurrency pair.CurrencyItem) (strin
return "", errors.New("not yet implemented")
}

// WithdrawExchangeCryptoFunds returns a withdrawal ID when a withdrawal is
// WithdrawCryptoExchangeFunds returns a withdrawal ID when a withdrawal is
// submitted
func (a *ANX) WithdrawCryptoExchangeFunds(address string, cryptocurrency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToLocalBank returns a withdrawal ID when a withdrawal is
// WithdrawFiatExchangeFunds returns a withdrawal ID when a withdrawal is
// submitted
func (a *ANX) WithdrawFiatExchangeFunds(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToInternationalBank returns a withdrawal ID when a withdrawal is
// WithdrawFiatExchangeFundsToInternationalBank returns a withdrawal ID when a withdrawal is
// submitted
func (a *ANX) WithdrawExchangeFiatFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
func (a *ANX) WithdrawFiatExchangeFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}
4 changes: 2 additions & 2 deletions exchanges/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,12 @@ func (b *Binance) QueryOrder(symbol, origClientOrderID string, orderID int64) (Q

// GetAccount returns binance user accounts
func (b *Binance) GetAccount() (*Account, error) {
type respone struct {
type response struct {
Response
Account
}

var resp respone
var resp response

path := fmt.Sprintf("%s%s", apiURL, accountInfo)
params := url.Values{}
Expand Down
8 changes: 4 additions & 4 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,20 @@ func (b *Binance) GetExchangeDepositAddress(cryptocurrency pair.CurrencyItem) (s
return "", errors.New("not yet implemented")
}

// WithdrawExchangeCryptoFunds returns a withdrawal ID when a withdrawal is
// WithdrawCryptoExchangeFunds returns a withdrawal ID when a withdrawal is
// submitted
func (b *Binance) WithdrawCryptoExchangeFunds(address string, cryptocurrency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToLocalBank returns a withdrawal ID when a
// WithdrawFiatExchangeFunds returns a withdrawal ID when a
// withdrawal is submitted
func (b *Binance) WithdrawFiatExchangeFunds(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToInternationalBank returns a withdrawal ID when a
// WithdrawFiatExchangeFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (b *Binance) WithdrawExchangeFiatFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
func (b *Binance) WithdrawFiatExchangeFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}
8 changes: 4 additions & 4 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,19 +209,19 @@ func (b *Bitfinex) GetExchangeDepositAddress(cryptocurrency pair.CurrencyItem) (
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFunds returns a withdrawal ID when a withdrawal is submitted
// WithdrawCryptoExchangeFunds returns a withdrawal ID when a withdrawal is submitted
func (b *Bitfinex) WithdrawCryptoExchangeFunds(address string, cryptocurrency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToLocalBank returns a withdrawal ID when a
// WithdrawFiatExchangeFunds returns a withdrawal ID when a
// withdrawal is submitted
func (b *Bitfinex) WithdrawFiatExchangeFunds(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToInternationalBank returns a withdrawal ID when a
// WithdrawFiatExchangeFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (b *Bitfinex) WithdrawExchangeFiatFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
func (b *Bitfinex) WithdrawFiatExchangeFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}
8 changes: 4 additions & 4 deletions exchanges/bitflyer/bitflyer_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,20 @@ func (b *Bitflyer) GetExchangeDepositAddress(cryptocurrency pair.CurrencyItem) (
return "", errors.New("not yet implemented")
}

// WithdrawExchangeCryptoFunds returns a withdrawal ID when a withdrawal is
// WithdrawCryptoExchangeFunds returns a withdrawal ID when a withdrawal is
// submitted
func (b *Bitflyer) WithdrawCryptoExchangeFunds(address string, cryptocurrency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToLocalBank returns a withdrawal ID when a
// WithdrawFiatExchangeFunds returns a withdrawal ID when a
// withdrawal is submitted
func (b *Bitflyer) WithdrawFiatExchangeFunds(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}

// WithdrawExchangeFiatFundsToInternationalBank returns a withdrawal ID when a
// WithdrawFiatExchangeFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (b *Bitflyer) WithdrawExchangeFiatFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
func (b *Bitflyer) WithdrawFiatExchangeFundsToInternationalBank(currency pair.CurrencyItem, amount float64) (string, error) {
return "", errors.New("not yet implemented")
}
Loading

0 comments on commit 0f20916

Please sign in to comment.