Skip to content

Commit

Permalink
Add golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
gnvk committed Sep 19, 2024
1 parent db57756 commit 0e6767d
Show file tree
Hide file tree
Showing 25 changed files with 966 additions and 667 deletions.
42 changes: 28 additions & 14 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,38 @@ on:
branches: [ master ]

jobs:

build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.19.x, 1.20.x, 1.21.x, 1.22.x, 1.23.x]
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '>=1.19'

- name: Check format
run: |
gofmt -s -l .
if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi
go-version: ${{ matrix.go-version }}
- name: Build
run: go build -v ./...

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.22
- uses: golangci/golangci-lint-action@v6
with:
version: v1.61
- name: Build
run: go build -v ./...
- name: Test
run: go test -cover -timeout 6s ./...
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 1.23
- name: Test
run: go test -timeout 6s ./...
run: go test -cover -timeout 6s ./...
367 changes: 367 additions & 0 deletions .golangci.yml

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
generate:
# The default "help" goal nicely prints all the available goals based on the funny looking ## comments.
# Source: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.DEFAULT_GOAL := help
.PHONY: help
help: ## Display this help
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

GOLANGCI_LINT_VERSION := v1.61.0

.PHONY: generate
generate: ## Generate easyjson
go generate ./...

test:
go test ./...
.PHONY: golangci-lint
golangci-lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)

.PHONY: lint
lint: golangci-lint ## Run the linters
golangci-lint run

.PHONY: test
test: ## Run the unit tests
go test -cover ./...
3 changes: 1 addition & 2 deletions alpaca/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"time"

"cloud.google.com/go/civil"
"github.com/shopspring/decimal"

// Required for easyjson generation
_ "github.com/mailru/easyjson/gen"
"github.com/shopspring/decimal"
)

//go:generate go install github.com/mailru/easyjson/[email protected]
Expand Down
54 changes: 44 additions & 10 deletions alpaca/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (c *Client) GetAccount() (*Account, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var account Account
if err = unmarshal(resp, &account); err != nil {
Expand All @@ -145,6 +146,7 @@ func (c *Client) GetAccountConfigurations() (*AccountConfigurations, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var configs AccountConfigurations
if err = unmarshal(resp, &configs); err != nil {
Expand Down Expand Up @@ -172,6 +174,7 @@ func (c *Client) UpdateAccountConfigurations(req UpdateAccountConfigurationsRequ
if err != nil {
return nil, err
}
defer closeResp(resp)

var configs AccountConfigurations
if err = unmarshal(resp, &configs); err != nil {
Expand Down Expand Up @@ -229,6 +232,7 @@ func (c *Client) GetAccountActivities(req GetAccountActivitiesRequest) ([]Accoun
if err != nil {
return nil, err
}
defer closeResp(resp)

var activities accountSlice
if err = unmarshal(resp, &activities); err != nil {
Expand Down Expand Up @@ -268,6 +272,7 @@ func (c *Client) GetPortfolioHistory(req GetPortfolioHistoryRequest) (*Portfolio
if err != nil {
return nil, err
}
defer closeResp(resp)

var history PortfolioHistory
if err = unmarshal(resp, &history); err != nil {
Expand All @@ -287,6 +292,7 @@ func (c *Client) GetPositions() ([]Position, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var positions positionSlice
if err = unmarshal(resp, &positions); err != nil {
Expand All @@ -310,6 +316,7 @@ func (c *Client) GetPosition(symbol string) (*Position, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var position Position
if err = unmarshal(resp, &position); err != nil {
Expand Down Expand Up @@ -339,6 +346,7 @@ func (c *Client) CloseAllPositions(req CloseAllPositionsRequest) ([]Order, error
if err != nil {
return nil, err
}
defer closeResp(resp)

var closeAllPositions closeAllPositionsSlice
if err = unmarshal(resp, &closeAllPositions); err != nil {
Expand Down Expand Up @@ -399,6 +407,7 @@ func (c *Client) ClosePosition(symbol string, req ClosePositionRequest) (*Order,
if err != nil {
return nil, err
}
defer closeResp(resp)

var order Order
if err = unmarshal(resp, &order); err != nil {
Expand All @@ -418,6 +427,7 @@ func (c *Client) GetClock() (*Clock, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var clock Clock
if err = unmarshal(resp, &clock); err != nil {
Expand Down Expand Up @@ -451,6 +461,7 @@ func (c *Client) GetCalendar(req GetCalendarRequest) ([]CalendarDay, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var calendar calendarDaySlice
if err = unmarshal(resp, &calendar); err != nil {
Expand Down Expand Up @@ -509,6 +520,7 @@ func (c *Client) GetOrders(req GetOrdersRequest) ([]Order, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var orders orderSlice
if err = unmarshal(resp, &orders); err != nil {
Expand Down Expand Up @@ -556,6 +568,7 @@ func (c *Client) PlaceOrder(req PlaceOrderRequest) (*Order, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var order Order
if err = unmarshal(resp, &order); err != nil {
Expand All @@ -575,6 +588,7 @@ func (c *Client) GetOrder(orderID string) (*Order, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var order Order
if err = unmarshal(resp, &order); err != nil {
Expand All @@ -598,6 +612,7 @@ func (c *Client) GetOrderByClientOrderID(clientOrderID string) (*Order, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var order Order
if err = unmarshal(resp, &order); err != nil {
Expand Down Expand Up @@ -626,6 +641,7 @@ func (c *Client) ReplaceOrder(orderID string, req ReplaceOrderRequest) (*Order,
if err != nil {
return nil, err
}
defer closeResp(resp)

var order Order
if err = unmarshal(resp, &order); err != nil {
Expand Down Expand Up @@ -692,6 +708,7 @@ func (c *Client) GetAssets(req GetAssetsRequest) ([]Asset, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var assets assetSlice
if err = unmarshal(resp, &assets); err != nil {
Expand All @@ -711,6 +728,7 @@ func (c *Client) GetAsset(symbol string) (*Asset, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var asset Asset
if err = unmarshal(resp, &asset); err != nil {
Expand Down Expand Up @@ -759,6 +777,7 @@ func (c *Client) GetAnnouncements(req GetAnnouncementsRequest) ([]Announcement,
if err != nil {
return nil, err
}
defer closeResp(resp)

var announcements announcementSlice
if err = unmarshal(resp, &announcements); err != nil {
Expand All @@ -779,6 +798,7 @@ func (c *Client) GetAnnouncement(announcementID string) (*Announcement, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var announcement Announcement
if err = unmarshal(resp, &announcement); err != nil {
Expand All @@ -798,6 +818,7 @@ func (c *Client) GetWatchlists() ([]Watchlist, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

var watchlists watchlistSlice
if err = unmarshal(resp, &watchlists); err != nil {
Expand All @@ -816,6 +837,7 @@ func (c *Client) CreateWatchlist(req CreateWatchlistRequest) (*Watchlist, error)
if err != nil {
return nil, err
}
defer closeResp(resp)

watchlist := &Watchlist{}
if err = unmarshal(resp, watchlist); err != nil {
Expand All @@ -834,6 +856,7 @@ func (c *Client) GetWatchlist(watchlistID string) (*Watchlist, error) {
if err != nil {
return nil, err
}
defer closeResp(resp)

watchlist := &Watchlist{}
if err = unmarshal(resp, watchlist); err != nil {
Expand All @@ -852,6 +875,7 @@ func (c *Client) UpdateWatchlist(watchlistID string, req UpdateWatchlistRequest)
if err != nil {
return nil, err
}
defer closeResp(resp)

watchlist := &Watchlist{}
if err = unmarshal(resp, watchlist); err != nil {
Expand All @@ -860,7 +884,7 @@ func (c *Client) UpdateWatchlist(watchlistID string, req UpdateWatchlistRequest)
return watchlist, nil
}

var ErrSymbolMissing = fmt.Errorf("symbol missing from request")
var ErrSymbolMissing = errors.New("symbol missing from request")

func (c *Client) AddSymbolToWatchlist(watchlistID string, req AddSymbolToWatchlistRequest) (*Watchlist, error) {
if req.Symbol == "" {
Expand All @@ -876,6 +900,7 @@ func (c *Client) AddSymbolToWatchlist(watchlistID string, req AddSymbolToWatchli
if err != nil {
return nil, err
}
defer closeResp(resp)

watchlist := &Watchlist{}
if err = unmarshal(resp, watchlist); err != nil {
Expand All @@ -894,8 +919,12 @@ func (c *Client) RemoveSymbolFromWatchlist(watchlistID string, req RemoveSymbolF
return err
}

_, err = c.delete(u)
return err
resp, err := c.delete(u)
if err != nil {
return err
}
closeResp(resp)
return nil
}

func (c *Client) DeleteWatchlist(watchlistID string) error {
Expand All @@ -904,8 +933,12 @@ func (c *Client) DeleteWatchlist(watchlistID string) error {
return err
}

_, err = c.delete(u)
return err
resp, err := c.delete(u)
if err != nil {
return err
}
closeResp(resp)
return nil
}

// GetAccount returns the user's account information
Expand Down Expand Up @@ -1132,10 +1165,11 @@ func verify(resp *http.Response) error {
}

func unmarshal(resp *http.Response, v easyjson.Unmarshaler) error {
defer func() {
// The underlying TCP connection can not be reused if the body is not fully read
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
return easyjson.UnmarshalFromReader(resp.Body, v)
}

func closeResp(resp *http.Response) {
// The underlying TCP connection can not be reused if the body is not fully read
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
Loading

0 comments on commit 0e6767d

Please sign in to comment.