Skip to content

Commit

Permalink
Migrate from gometalinter.v2 to golangci-lint (thrasher-corp#249)
Browse files Browse the repository at this point in the history
* Migrate from gometalinter.v2 to golangci-lint
  • Loading branch information
thrasher- authored Mar 1, 2019
1 parent 81852f2 commit 7dcb1ab
Show file tree
Hide file tree
Showing 133 changed files with 2,177 additions and 2,202 deletions.
83 changes: 83 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
run:
deadline: 30s
issues-exit-code: 1
tests: true
skip-dirs:
- vendor
- web
- testdata

linters:
disable-all: true
enable:
# defaults
- govet
# - errcheck
- staticcheck
# - unused
- gosimple
- structcheck
# - varcheck
- ineffassign
# - deadcode
- typecheck
- goimports
- govet

# disabled by default linters
- golint
- stylecheck
- gosec
# - interfacer
- unconvert
# - dupl
- goconst
# - gocyclo
- gofmt
- goimports
# - maligned
- depguard
- misspell
# - lll
- unparam
- nakedret
# - prealloc
- scopelint
- gocritic
# - gochecknoinits
# - gochecknoglobals

linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
goconst:
min-occurrences: 6
depguard:
list-type: blacklist
# lll:
# line-length: 80 # NOTE: we'll enforce this at a later point
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
- hugeParam
- importShadow
- rangeValCopy
- methodExprCall

issues:
max-issues-per-linter: 0
max-same-issues: 0

exclude-rules:
- text: "weak cryptographic primitive"
linters:
- gosec

service:
golangci-lint-version: 1.15.x
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ matrix:

- language: go
go:
- 1.11.x
- 1.12.x
env:
- GO111MODULE=on
install: true
Expand Down
39 changes: 11 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
LDFLAGS = -ldflags "-w -s"
GCTPKG = github.com/thrasher-/gocryptotrader
LINTPKG = gopkg.in/alecthomas/gometalinter.v2
LINTBIN = $(GOPATH)/bin/gometalinter.v2
ENABLELLL = false
LINTOPTS = \
--disable-all \
--enable=gofmt \
--enable=vet \
--enable=vetshadow \
--enable=misspell \
--enable=golint \
--enable=ineffassign \
--enable=goconst \
--enable=structcheck \
--enable=unparam \
--enable=gosimple \
--enable=unconvert
ifeq ($(ENABLELLL), true)
LINTOPTS += \
--enable=lll \
--line-length=80
endif
LINTOPTS += \
--deadline=5m ./... | \
tee /dev/stderr
LINTPKG = github.com/golangci/golangci-lint/cmd/[email protected]
LINTBIN = $(GOPATH)/bin/golangci-lint

get:
GO111MODULE=on go get $(GCTPKG)

linter:
GO111MODULE=on go get $(GCTPKG)
GO111MODULE=off go get -u $(LINTPKG)
$(LINTBIN) --install
test -z "$$($(LINTBIN) $(LINTOPTS))"
GO111MODULE=on go get $(LINTPKG)
test -z "$$($(LINTBIN) run --verbose | tee /dev/stderr)"

check: linter test

Expand All @@ -46,4 +23,10 @@ install:
GO111MODULE=on go install $(LDFLAGS)

fmt:
gofmt -l -w -s $(shell find . -type f -name '*.go')
gofmt -l -w -s $(shell find . -type f -name '*.go')

update_deps:
GO111MODULE=on go mod verify
GO111MODULE=on go mod tidy
rm -rf vendor
GO111MODULE=on go mod vendor
86 changes: 38 additions & 48 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package common

import (
"crypto/hmac"
"crypto/md5"
"crypto/md5" // nolint:gosec
"crypto/rand"
"crypto/sha1"
"crypto/sha1" // nolint:gosec
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
Expand Down Expand Up @@ -38,11 +38,11 @@ var (

// ErrNotYetImplemented defines a common error across the code base that
// alerts of a function that has not been completed or tied into main code
ErrNotYetImplemented = errors.New("Not Yet Implemented")
ErrNotYetImplemented = errors.New("not yet implemented")

// ErrFunctionNotSupported defines a standardised error for an unsupported
// wrapper function by an API
ErrFunctionNotSupported = errors.New("Unsupported Wrapper Function")
ErrFunctionNotSupported = errors.New("unsupported wrapper function")
)

// Const declarations for common.go operations
Expand Down Expand Up @@ -91,9 +91,9 @@ func GetRandomSalt(input []byte, saltLen int) ([]byte, error) {

// GetMD5 returns a MD5 hash of a byte array
func GetMD5(input []byte) []byte {
hash := md5.New()
hash.Write(input)
return hash.Sum(nil)
m := md5.New() // nolint:gosec
m.Write(input)
return m.Sum(nil)
}

// GetSHA512 returns a SHA512 hash of a byte array
Expand All @@ -113,40 +113,30 @@ func GetSHA256(input []byte) []byte {
// GetHMAC returns a keyed-hash message authentication code using the desired
// hashtype
func GetHMAC(hashType int, input, key []byte) []byte {
var hash func() hash.Hash
var hasher func() hash.Hash

switch hashType {
case HashSHA1:
{
hash = sha1.New
}
hasher = sha1.New
case HashSHA256:
{
hash = sha256.New
}
hasher = sha256.New
case HashSHA512:
{
hash = sha512.New
}
hasher = sha512.New
case HashSHA512_384:
{
hash = sha512.New384
}
hasher = sha512.New384
case HashMD5:
{
hash = md5.New
}
hasher = md5.New
}

hmac := hmac.New(hash, key)
hmac.Write(input)
return hmac.Sum(nil)
h := hmac.New(hasher, key)
h.Write(input)
return h.Sum(nil)
}

// Sha1ToHex takes a string, sha1 hashes it and return a hex string of the
// result
func Sha1ToHex(data string) string {
h := sha1.New()
h := sha1.New() // nolint:gosec
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
Expand All @@ -172,7 +162,7 @@ func Base64Encode(input []byte) string {

// StringSliceDifference concatenates slices together based on its index and
// returns an individual string array
func StringSliceDifference(slice1 []string, slice2 []string) []string {
func StringSliceDifference(slice1, slice2 []string) []string {
var diff []string
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
Expand Down Expand Up @@ -256,8 +246,8 @@ func TrimString(input, cutset string) string {
}

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

// StringToUpper changes strings to uppercase
Expand Down Expand Up @@ -312,7 +302,7 @@ func IsValidCryptoAddress(address, crypto string) (bool, error) {
case "eth":
return regexp.MatchString("^0x[a-km-z0-9]{40}$", address)
default:
return false, errors.New("Invalid crypto currency")
return false, errors.New("invalid crypto currency")
}
}

Expand Down Expand Up @@ -353,7 +343,7 @@ func CalculateNetProfit(amount, priceThen, priceNow, costs float64) float64 {

// SendHTTPRequest sends a request using the http package and returns a response
// as a string and an error
func SendHTTPRequest(method, path string, headers map[string]string, body io.Reader) (string, error) {
func SendHTTPRequest(method, urlPath string, headers map[string]string, body io.Reader) (string, error) {
result := strings.ToUpper(method)

if result != http.MethodPost && result != http.MethodGet && result != http.MethodDelete {
Expand All @@ -362,7 +352,7 @@ func SendHTTPRequest(method, path string, headers map[string]string, body io.Rea

initialiseHTTPClient()

req, err := http.NewRequest(method, path, body)
req, err := http.NewRequest(method, urlPath, body)
if err != nil {
return "", err
}
Expand All @@ -389,14 +379,14 @@ func SendHTTPRequest(method, path string, headers map[string]string, body io.Rea
// SendHTTPGetRequest sends a simple get request using a url string & JSON
// decodes the response into a struct pointer you have supplied. Returns an error
// on failure.
func SendHTTPGetRequest(url string, jsonDecode, isVerbose bool, result interface{}) error {
func SendHTTPGetRequest(urlPath string, jsonDecode, isVerbose bool, result interface{}) error {
if isVerbose {
log.Debugf("Raw URL: %s", url)
log.Debugf("Raw URL: %s", urlPath)
}

initialiseHTTPClient()

res, err := HTTPClient.Get(url)
res, err := HTTPClient.Get(urlPath)
if err != nil {
return err
}
Expand All @@ -411,7 +401,7 @@ func SendHTTPGetRequest(url string, jsonDecode, isVerbose bool, result interface
}

if isVerbose {
log.Debugf("Raw Resp: %s", string(contents[:]))
log.Debugf("Raw Resp: %s", string(contents))
}

defer res.Body.Close()
Expand Down Expand Up @@ -441,12 +431,12 @@ func JSONDecode(data []byte, to interface{}) error {

// EncodeURLValues concatenates url values onto a url string and returns a
// string
func EncodeURLValues(url string, values url.Values) string {
path := url
func EncodeURLValues(urlPath string, values url.Values) string {
u := urlPath
if len(values) > 0 {
path += "?" + values.Encode()
u += "?" + values.Encode()
}
return path
return u
}

// ExtractHost returns the hostname out of a string
Expand All @@ -466,16 +456,16 @@ func ExtractPort(host string) int {
}

// OutputCSV dumps data into a file as comma-separated values
func OutputCSV(path string, data [][]string) error {
_, err := ReadFile(path)
func OutputCSV(filePath string, data [][]string) error {
_, err := ReadFile(filePath)
if err != nil {
errTwo := WriteFile(path, nil)
errTwo := WriteFile(filePath, nil)
if errTwo != nil {
return errTwo
}
}

file, err := os.Create(path)
file, err := os.Create(filePath)
if err != nil {
return err
}
Expand Down Expand Up @@ -508,8 +498,8 @@ func UnixTimestampStrToTime(timeStr string) (time.Time, error) {
}

// ReadFile reads a file and returns read data as byte array.
func ReadFile(path string) ([]byte, error) {
return ioutil.ReadFile(path)
func ReadFile(file string) ([]byte, error) {
return ioutil.ReadFile(file)
}

// WriteFile writes selected data to a file and returns an error
Expand Down Expand Up @@ -570,7 +560,7 @@ func FloatFromString(raw interface{}) (float64, error) {
}
flt, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, fmt.Errorf("Could not convert value: %s Error: %s", str, err)
return 0, fmt.Errorf("could not convert value: %s Error: %s", str, err)
}
return flt, nil
}
Expand Down
Loading

0 comments on commit 7dcb1ab

Please sign in to comment.