Skip to content

Commit

Permalink
Minor linter and typo fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAndereAndi committed Apr 9, 2024
1 parent 94ea245 commit d5c47f3
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 19 deletions.
58 changes: 58 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 5m

# include test files or not, default is true
tests: true

# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
# to go.mod are needed. This setting is most useful to check that go.mod does
# not need updates, such as in a continuous integration and testing system.
# If invoked with -mod=vendor, the go command assumes that the vendor
# directory holds the correct copies of dependencies and ignores
# the dependency descriptions in go.mod.
modules-download-mode: readonly

# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
formats:
- format: colored-line-number

linters:
enable:
- bodyclose
- errcheck
- errorlint
- gocheckcompilerdirectives
- gochecknoinits
- gochecksumtype
- goconst
- gofmt
- gosimple
- gosec
- govet
- nilerr
- nilnil
- staticcheck
- typecheck
- unused
- whitespace

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- errcheck
- goconst
- gosec

# checking for errors in defers seldom makes sense...
- source: "^\\s*defer\\s"
linters:
- errcheck
- staticcheck
15 changes: 9 additions & 6 deletions hub/hub_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,16 @@ func (h *Hub) connectFoundService(remoteService *api.ServiceDetails, host, port,
}

address := fmt.Sprintf("wss://%s:%s%s", host, port, path)
conn, _, err := dialer.Dial(address, nil)
if err != nil {
conn, resp, err := dialer.Dial(address, nil)
if err == nil {
defer resp.Body.Close()
} else {
address = fmt.Sprintf("wss://%s:%s", host, port)
conn, _, err = dialer.Dial(address, nil)
}
if err != nil {
return err
conn, resp, err = dialer.Dial(address, nil)
if err != nil {
return err
}
defer resp.Body.Close()
}

tlsConn := conn.UnderlyingConn().(*tls.Conn)
Expand Down
1 change: 0 additions & 1 deletion hub/hub_pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (h *Hub) RegisterRemoteSKI(ski string, enable bool) {
// Disconnect a connection to an SKI, used by a service implementation
// e.g. if heartbeats go wrong
func (h *Hub) DisconnectSKI(ski string, reason string) {

con := h.connectionForSKI(ski)
if con == nil {
return
Expand Down
5 changes: 3 additions & 2 deletions hub/hub_shipconnection.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hub

import (
"errors"
"time"

"github.com/enbility/ship-go/api"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (h *Hub) HandleShipHandshakeStateUpdate(ski string, state model.ShipState)
}

pairingState := h.mapShipMessageExchangeState(state.State, ski)
if state.Error != nil && state.Error != api.ErrConnectionNotFound {
if state.Error != nil && !errors.Is(state.Error, api.ErrConnectionNotFound) {
pairingState = api.ConnectionStateError
}

Expand All @@ -88,7 +89,7 @@ func (h *Hub) HandleShipHandshakeStateUpdate(ski string, state model.ShipState)

existingDetails := service.ConnectionStateDetail()
existingState := existingDetails.State()
if existingState != pairingState || existingDetails.Error() != state.Error {
if existingState != pairingState || !errors.Is(existingDetails.Error(), state.Error) {
service.SetConnectionStateDetail(pairingDetail)

// always send a delayed update, as the processing of the new state has to be done
Expand Down
15 changes: 10 additions & 5 deletions hub/hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (s *HubSuite) Test_SendWSCloseMessage() {
wsURL := strings.Replace(server.URL, "http://", "ws://", -1)

// Connect to the server
con, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
con, resp, err := websocket.DefaultDialer.Dial(wsURL, nil)
assert.Nil(s.T(), err)

ski := "12af9e"
Expand All @@ -163,6 +163,7 @@ func (s *HubSuite) Test_SendWSCloseMessage() {

hub.sendWSCloseMessage(con)

resp.Body.Close()
_ = con.Close()
server.CloseClientConnections()
server.Close()
Expand Down Expand Up @@ -334,16 +335,18 @@ func (s *HubSuite) Test_ServeHTTP_01() {
wsURL := strings.Replace(server.URL, "http://", "ws://", -1)

// Connect to the server
con, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
con, resp, err := websocket.DefaultDialer.Dial(wsURL, nil)
assert.Nil(s.T(), err)
resp.Body.Close()
_ = con.Close()

dialer := &websocket.Dialer{
Subprotocols: []string{api.ShipWebsocketSubProtocol},
}
con, _, err = dialer.Dial(wsURL, nil)
con, resp, err = dialer.Dial(wsURL, nil)
assert.Nil(s.T(), err)

resp.Body.Close()
_ = con.Close()
server.CloseClientConnections()
server.Close()
Expand Down Expand Up @@ -373,9 +376,10 @@ func (s *HubSuite) Test_ServeHTTP_02() {
},
Subprotocols: []string{api.ShipWebsocketSubProtocol},
}
con, _, err := dialer.Dial(wsURL, nil)
con, resp, err := dialer.Dial(wsURL, nil)
assert.Nil(s.T(), err)

resp.Body.Close()
_ = con.Close()

validCert, _ := cert.CreateCertificate("unit", "org", "DE", "CN")
Expand All @@ -389,9 +393,10 @@ func (s *HubSuite) Test_ServeHTTP_02() {
},
Subprotocols: []string{api.ShipWebsocketSubProtocol},
}
con, _, err = dialer.Dial(wsURL, nil)
con, resp, err = dialer.Dial(wsURL, nil)
assert.Nil(s.T(), err)

resp.Body.Close()
_ = con.Close()
server.CloseClientConnections()
server.Close()
Expand Down
5 changes: 1 addition & 4 deletions mdns/avahi.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func (a *AvahiProvider) ResolveEntries(callback api.MdnsResolveCB) {
}
}
}

}

// process an avahi mDNS service
Expand Down Expand Up @@ -211,14 +210,13 @@ func (a *AvahiProvider) processService(service avahi.Service, remove bool, cb ap
// resolve the new service
resolved, err := a.avServer.ResolveService(service.Interface, service.Protocol, service.Name, service.Type, service.Domain, avahi.ProtoUnspec, 0)
if err != nil {
return fmt.Errorf("error resolving service: %s error: %s", service.Name, err)
return fmt.Errorf("error resolving service: %s error: %w", service.Name, err)
}

return a.processAddedService(resolved, cb)
}

func (a *AvahiProvider) processRemovedService(service avahi.Service, cb api.MdnsResolveCB) error {

// get the elements for the service
elements := a.serviceElements[getServiceUniqueKey(service)]

Expand All @@ -228,7 +226,6 @@ func (a *AvahiProvider) processRemovedService(service avahi.Service, cb api.Mdns
}

func (a *AvahiProvider) processAddedService(service avahi.Service, cb api.MdnsResolveCB) error {

// convert [][]byte to []string manually
var txt []string
for _, element := range service.Txt {
Expand Down
1 change: 0 additions & 1 deletion mdns/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ func (m *MdnsManager) processMdnsEntry(elements map[string]string, name, host st
if updated {
m.setMdnsEntry(ski, entry)
}

} else if !exists && !remove {
updated = true
// new
Expand Down

0 comments on commit d5c47f3

Please sign in to comment.