From 9c4985368aee638d982f30ea7bc5c7ee71ae3ab3 Mon Sep 17 00:00:00 2001 From: Jeroen Wijenbergh Date: Tue, 6 May 2025 12:35:21 +0200 Subject: All: Fix staticcheck errors --- internal/api/cache.go | 2 +- internal/config/atomicfile/atomicfile.go | 4 ++-- internal/config/atomicfile/atomicfile_test.go | 4 ++-- internal/config/v1/v1.go | 4 ++++ internal/discovery/discovery.go | 7 ++++++- internal/discovery/manager.go | 8 ++++++++ internal/levenshtein/levenshtein.go | 1 + internal/test/server.go | 5 ++++- internal/verify/verify_test.go | 3 ++- internal/wireguard/wireguard.go | 4 ++-- 10 files changed, 32 insertions(+), 10 deletions(-) (limited to 'internal') diff --git a/internal/api/cache.go b/internal/api/cache.go index 9a478df..5c682f4 100644 --- a/internal/api/cache.go +++ b/internal/api/cache.go @@ -16,7 +16,7 @@ type EndpointCache struct { mu sync.Mutex } -// Get() returns a cached or fresh endpoint cache copy +// Get returns a cached or fresh endpoint cache copy func (ec *EndpointCache) Get(ctx context.Context, wk string, transport http.RoundTripper) (*endpoints.Endpoints, error) { ec.mu.Lock() defer ec.mu.Unlock() diff --git a/internal/config/atomicfile/atomicfile.go b/internal/config/atomicfile/atomicfile.go index 5c18e85..542f58a 100644 --- a/internal/config/atomicfile/atomicfile.go +++ b/internal/config/atomicfile/atomicfile.go @@ -29,8 +29,8 @@ func WriteFile(filename string, data []byte, perm os.FileMode) (err error) { tmpName := f.Name() defer func() { if err != nil { - f.Close() - os.Remove(tmpName) + f.Close() //nolint:errcheck + os.Remove(tmpName) //nolint:errcheck } }() if _, err := f.Write(data); err != nil { diff --git a/internal/config/atomicfile/atomicfile_test.go b/internal/config/atomicfile/atomicfile_test.go index 670d225..f14cb31 100644 --- a/internal/config/atomicfile/atomicfile_test.go +++ b/internal/config/atomicfile/atomicfile_test.go @@ -24,7 +24,7 @@ func TestDoesNotOverwriteIrregularFiles(t *testing.T) { // macOS private temp does not allow unix socket creation, but /tmp does. if runtime.GOOS == "darwin" { path = filepath.Join("/tmp", filename) - t.Cleanup(func() { os.Remove(path) }) + t.Cleanup(func() { os.Remove(path) }) //nolint:errcheck } else { path = filepath.Join(t.TempDir(), filename) } @@ -35,7 +35,7 @@ func TestDoesNotOverwriteIrregularFiles(t *testing.T) { if err != nil { t.Fatal(err) } - defer l.Close() + defer l.Close() //nolint:errcheck err = WriteFile(path, []byte("hello"), 0o644) if err == nil { diff --git a/internal/config/v1/v1.go b/internal/config/v1/v1.go index 973c3d3..5f5cd93 100644 --- a/internal/config/v1/v1.go +++ b/internal/config/v1/v1.go @@ -64,11 +64,15 @@ type SecureInternetHome struct { CurrentLocation string `json:"current_location"` } +// Type is the type of server, a server from discovery or one entered manually by typing it in the client type Type int8 const ( + // CustomServerType is the type of server that is manually added by typing a URL CustomServerType Type = iota + // InstituteAccessServerType is the type of server that is in the discovery server_list.json with type "institute_access" InstituteAccessServerType + // SecureInternetServerType is the type of server that is in the discovery server_list.json with type "secure_internet" SecureInternetServerType ) diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index b6fef6f..c7eae95 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -44,6 +44,7 @@ type Organization struct { KeywordList discotypes.MapOrString `json:"keyword_list,omitempty"` } +// Score returns the levenshstein score for a search query `search` on the organizations func (o *Organization) Score(search string) int { return levenshtein.DiscoveryScore(search, o.DisplayName, o.KeywordList) } @@ -75,7 +76,7 @@ type Server struct { SupportContact []string `json:"support_contact,omitempty"` } -// Matches returns if the search query `str` matches with this server +// Score returns the score of the search query `str` on this server func (s *Server) Score(search string) int { return levenshtein.DiscoveryScore(search, s.DisplayName, s.KeywordList) } @@ -404,12 +405,16 @@ func (discovery *Discovery) Servers(ctx context.Context) (*Servers, bool, error) return &discovery.ServerList, true, nil } +// UpdateServers updates the discovery servers to the new version +// It does this by checking versions func (discovery *Discovery) UpdateServers(other Discovery) { if other.ServerList.Version >= discovery.ServerList.Version { discovery.ServerList = other.ServerList } } +// Copy creates a deep-copy for the discovery struct +// It does this by marshalling and unmarshalling it as JSON func (discovery *Discovery) Copy() (Discovery, error) { var dest Discovery b, err := json.Marshal(discovery) diff --git a/internal/discovery/manager.go b/internal/discovery/manager.go index e71f13a..6a78486 100644 --- a/internal/discovery/manager.go +++ b/internal/discovery/manager.go @@ -7,6 +7,8 @@ import ( "codeberg.org/eduVPN/eduvpn-common/internal/log" ) +// Manager is the discovery struct that is cached +// with some bookkeeping to avoid race conditions type Manager struct { disco *Discovery @@ -15,6 +17,7 @@ type Manager struct { wait sync.WaitGroup } +// NewManager creates a new Discovery manager func NewManager(disco *Discovery) *Manager { return &Manager{disco: disco} } @@ -35,6 +38,8 @@ func (m *Manager) unlock(write bool) { m.mu.RUnlock() } +// Discovery gets the cached discovery +// `write` is true if discovery will be written to func (m *Manager) Discovery(write bool) (*Discovery, func()) { if write { m.wait.Wait() @@ -45,6 +50,7 @@ func (m *Manager) Discovery(write bool) (*Discovery, func()) { } } +// Cancel aborts the running discovery startup process func (m *Manager) Cancel() { if m.cancel != nil { m.cancel() @@ -52,6 +58,8 @@ func (m *Manager) Cancel() { m.wait.Wait() } +// Startup handles the discovery process in the background +// It's called Startup because it's called when the lib is initialised func (m *Manager) Startup(ctx context.Context, cb func()) { ctx, cancel := context.WithCancel(ctx) m.cancel = cancel diff --git a/internal/levenshtein/levenshtein.go b/internal/levenshtein/levenshtein.go index 68108bc..3be81c8 100644 --- a/internal/levenshtein/levenshtein.go +++ b/internal/levenshtein/levenshtein.go @@ -1,3 +1,4 @@ +// Package levenshtein implements the algorithm for calculating the "distance" between two strings package levenshtein import ( diff --git a/internal/test/server.go b/internal/test/server.go index 1abbd8c..f222abc 100644 --- a/internal/test/server.go +++ b/internal/test/server.go @@ -24,12 +24,14 @@ func NewServer(handler http.Handler, listener net.Listener) *Server { } s := httptest.NewUnstartedServer(handler) - s.Listener.Close() + // This is only for tests and if this fails, bad luck + s.Listener.Close() //nolint:errcheck s.Listener = listener s.StartTLS() return &Server{s} } +// HandlerPath handles a specific HTTP query type HandlerPath struct { Method string Path string @@ -38,6 +40,7 @@ type HandlerPath struct { ResponseCode int } +// HandlerFunc handles a HTTP request func (hp *HandlerPath) HandlerFunc() func(http.ResponseWriter, *http.Request) { if hp.ResponseHandler != nil { return hp.ResponseHandler diff --git a/internal/verify/verify_test.go b/internal/verify/verify_test.go index 2c499b1..aa373d1 100644 --- a/internal/verify/verify_test.go +++ b/internal/verify/verify_test.go @@ -15,7 +15,8 @@ func Test_verifyWithKeys(t *testing.T) { if err != nil { panic(err) } - defer file.Close() + // this is best effort + defer file.Close() //nolint:errcheck // Get last line (key string) from file scanner := bufio.NewScanner(file) diff --git a/internal/wireguard/wireguard.go b/internal/wireguard/wireguard.go index 8c2753b..b156430 100644 --- a/internal/wireguard/wireguard.go +++ b/internal/wireguard/wireguard.go @@ -19,7 +19,7 @@ func availableTCPPort() (int, error) { if err != nil { return -1, err } - defer ltcp.Close() + defer ltcp.Close() //nolint:errcheck return ltcp.Addr().(*net.TCPAddr).Port, nil } @@ -32,7 +32,7 @@ func availableUDPPort() (int, error) { if err != nil { return -1, err } - defer ludp.Close() + defer ludp.Close() //nolint:errcheck return ludp.LocalAddr().(*net.UDPAddr).Port, nil } -- cgit v1.2.3