From 9180d729157382506711a9dfed924524831044b6 Mon Sep 17 00:00:00 2001 From: Naji Obeid Date: Mon, 26 Feb 2024 17:06:39 +0000 Subject: [PATCH] fix linting: --- pkg/geoipupdate/download/download.go | 7 +++++-- pkg/geoipupdate/download/metadata.go | 6 +++--- pkg/geoipupdate/download/metadata_test.go | 10 ++++++---- pkg/geoipupdate/download/output_test.go | 1 + pkg/geoipupdate/download/writer.go | 5 ++--- pkg/geoipupdate/download/writer_test.go | 22 +++++++++++++--------- pkg/geoipupdate/geoip_updater.go | 8 +++----- pkg/geoipupdate/geoip_updater_test.go | 14 +++++++++----- pkg/geoipupdate/internal/errors.go | 7 ++++++- 9 files changed, 48 insertions(+), 32 deletions(-) diff --git a/pkg/geoipupdate/download/download.go b/pkg/geoipupdate/download/download.go index ac110f97..2e692714 100644 --- a/pkg/geoipupdate/download/download.go +++ b/pkg/geoipupdate/download/download.go @@ -17,6 +17,7 @@ import ( ) const ( + // Extension is the typical extension used for database files. Extension = ".mmdb" // zeroMD5 is the default value provided as an MD5 hash for a non-existent @@ -24,6 +25,8 @@ const ( zeroMD5 = "00000000000000000000000000000000" ) +// Downloader represents common methods required to implement the functionality +// required to download/update mmdb files. type Downloader interface { GetOutdatedEditions(ctx context.Context) ([]Metadata, error) DownloadEdition(ctx context.Context, edition Metadata) error @@ -59,7 +62,7 @@ type Download struct { func New( accountID int, licenseKey string, - url string, + serverURL string, proxy *url.URL, databaseDir string, preserveFileTimes bool, @@ -80,7 +83,7 @@ func New( licenseKey: licenseKey, oldEditionsHash: map[string]string{}, preserveFileTimes: preserveFileTimes, - url: url, + url: serverURL, now: time.Now, verbose: verbose, } diff --git a/pkg/geoipupdate/download/metadata.go b/pkg/geoipupdate/download/metadata.go index cf24f40b..4f27ebfd 100644 --- a/pkg/geoipupdate/download/metadata.go +++ b/pkg/geoipupdate/download/metadata.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "log" "net/http" "net/url" @@ -24,7 +24,7 @@ type metadataResponse struct { Databases []Metadata `json:"databases"` } -// databaseResponse represents the metadata content for a certain database returned by the +// Metadata represents the metadata content for a certain database returned by the // metadata endpoint. type Metadata struct { Date string `json:"date"` @@ -57,7 +57,7 @@ func (d *Download) GetOutdatedEditions(ctx context.Context) ([]Metadata, error) } defer response.Body.Close() - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { return nil, fmt.Errorf("reading response body: %w", err) } diff --git a/pkg/geoipupdate/download/metadata_test.go b/pkg/geoipupdate/download/metadata_test.go index 82e297e9..941ec187 100644 --- a/pkg/geoipupdate/download/metadata_test.go +++ b/pkg/geoipupdate/download/metadata_test.go @@ -28,7 +28,7 @@ func TestGetOutdatedEditions(t *testing.T) { err = os.WriteFile(dbFile, []byte("edition-2 content"), os.ModePerm) require.NoError(t, err) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { jsonData := ` { "databases": [ @@ -52,7 +52,8 @@ func TestGetOutdatedEditions(t *testing.T) { ` w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte(jsonData)) + _, err := w.Write([]byte(jsonData)) + require.NoError(t, err) })) defer server.Close() @@ -87,8 +88,9 @@ func TestGetOutdatedEditions(t *testing.T) { require.NoError(t, err) require.ElementsMatch(t, expectedOutdatedEditions, outdatedEditions) - expectedDatabases := append( - expectedOutdatedEditions, + expectedDatabases := expectedOutdatedEditions + expectedDatabases = append( + expectedDatabases, Metadata{ EditionID: "edition-1", MD5: "618dd27a10de24809ec160d6807f363f", diff --git a/pkg/geoipupdate/download/output_test.go b/pkg/geoipupdate/download/output_test.go index 96f786e3..fad8a025 100644 --- a/pkg/geoipupdate/download/output_test.go +++ b/pkg/geoipupdate/download/output_test.go @@ -31,6 +31,7 @@ func TestOutputFormat(t *testing.T) { now: func() time.Time { return now }, } + //nolint:lll expectedOutput := `[{"edition_id":"edition-1","old_hash":"1","new_hash":"1","modified_at":1704067200,"checked_at":1708646400},{"edition_id":"edition-2","old_hash":"10","new_hash":"11","modified_at":1706745600,"checked_at":1708646400}]` output, err := d.MakeOutput() diff --git a/pkg/geoipupdate/download/writer.go b/pkg/geoipupdate/download/writer.go index d634ab8b..274749d9 100644 --- a/pkg/geoipupdate/download/writer.go +++ b/pkg/geoipupdate/download/writer.go @@ -10,7 +10,6 @@ import ( "fmt" "hash" "io" - "io/ioutil" "log" "net/http" "os" @@ -48,7 +47,7 @@ func (d *Download) DownloadEdition(ctx context.Context, edition Metadata) error defer response.Body.Close() if response.StatusCode != http.StatusOK { - responseBody, err := ioutil.ReadAll(response.Body) + responseBody, err := io.ReadAll(response.Body) if err != nil { return fmt.Errorf("reading response body: %w", err) } @@ -232,7 +231,7 @@ func syncDir(path string) error { } // setModifiedAtTime sets the times for a database file to a certain value. -func setModifiedAtTime(path string, dateString string) error { +func setModifiedAtTime(path, dateString string) error { releaseDate, err := ParseTime(dateString) if err != nil { return err diff --git a/pkg/geoipupdate/download/writer_test.go b/pkg/geoipupdate/download/writer_test.go index ffd12d76..f4f51888 100644 --- a/pkg/geoipupdate/download/writer_test.go +++ b/pkg/geoipupdate/download/writer_test.go @@ -61,7 +61,7 @@ func TestDownloadEdition(t *testing.T) { require.NoError(t, tw.Close()) require.NoError(t, gw.Close()) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/gzip") w.Header().Set("Content-Disposition", "attachment; filename=test.tar.gz") _, err := io.Copy(w, &buf) @@ -75,6 +75,7 @@ func TestDownloadEdition(t *testing.T) { dbFile := filepath.Join(tempDir, edition.EditionID+Extension) + //nolint:gosec // we need to read the content of the file in this test. fileContent, err := os.ReadFile(dbFile) require.NoError(t, err) require.Equal(t, dbContent, string(fileContent)) @@ -105,7 +106,7 @@ func TestDownloadEdition(t *testing.T) { require.NoError(t, tw.Close()) require.NoError(t, gw.Close()) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/gzip") w.Header().Set("Content-Disposition", "attachment; filename=test.tar.gz") _, err := io.Copy(w, &buf) @@ -119,6 +120,7 @@ func TestDownloadEdition(t *testing.T) { dbFile := filepath.Join(tempDir, edition.EditionID+Extension) + //nolint:gosec // we need to read the content of the file in this test. fileContent, err := os.ReadFile(dbFile) require.NoError(t, err) require.Equal(t, dbContent, string(fileContent)) @@ -134,8 +136,8 @@ func TestDownloadEdition(t *testing.T) { { description: "server error", preserveFileTime: false, - server: func(t *testing.T) *httptest.Server { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server: func(_ *testing.T) *httptest.Server { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusInternalServerError) })) return server @@ -149,11 +151,12 @@ func TestDownloadEdition(t *testing.T) { description: "wrong file format", preserveFileTime: false, server: func(t *testing.T) *httptest.Server { - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { jsonData := `{"message": "Hello, world!", "status": "ok"}` w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte(jsonData)) + _, err := w.Write([]byte(jsonData)) + require.NoError(t, err) })) return server }, @@ -172,7 +175,7 @@ func TestDownloadEdition(t *testing.T) { require.NoError(t, tw.Close()) require.NoError(t, gw.Close()) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/gzip") w.Header().Set("Content-Disposition", "attachment; filename=test.tar.gz") _, err := io.Copy(w, &buf) @@ -207,7 +210,7 @@ func TestDownloadEdition(t *testing.T) { require.NoError(t, tw.Close()) require.NoError(t, gw.Close()) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/gzip") w.Header().Set("Content-Disposition", "attachment; filename=test.tar.gz") _, err := io.Copy(w, &buf) @@ -242,7 +245,7 @@ func TestDownloadEdition(t *testing.T) { require.NoError(t, tw.Close()) require.NoError(t, gw.Close()) - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/gzip") w.Header().Set("Content-Disposition", "attachment; filename=test.tar.gz") _, err := io.Copy(w, &buf) @@ -253,6 +256,7 @@ func TestDownloadEdition(t *testing.T) { }, checkResult: func(t *testing.T, err error) { require.Error(t, err) + //nolint:lll require.Regexp(t, "^validating hash for edition-1: md5 of new database .* does not match expected md5", err.Error()) }, }, diff --git a/pkg/geoipupdate/geoip_updater.go b/pkg/geoipupdate/geoip_updater.go index fe962c86..55be212e 100644 --- a/pkg/geoipupdate/geoip_updater.go +++ b/pkg/geoipupdate/geoip_updater.go @@ -70,7 +70,7 @@ func (c *Client) Run(ctx context.Context) error { return nil } - if err := c.retry(ctx, getOutdatedEditions, "Couldn't get download metadata"); err != nil { + if err := c.retry(getOutdatedEditions, "Couldn't get download metadata"); err != nil { return fmt.Errorf("getting download metadata: %w", err) } @@ -84,11 +84,10 @@ func (c *Client) Run(ctx context.Context) error { jobProcessor := internal.NewJobProcessor(ctx, c.config.Parallelism) for _, edition := range outdatedEditions { edition := edition - processFunc := func(ctx context.Context) error { + processFunc := func(_ context.Context) error { err := c.retry( - ctx, func() error { return downloadEdition(edition) }, - fmt.Sprintf("Couldn't download %s", edition.EditionID), + "Couldn't download "+edition.EditionID, ) if err != nil { return err @@ -117,7 +116,6 @@ func (c *Client) Run(ctx context.Context) error { // retry implements a retry functionality for downloads for non permanent errors. func (c *Client) retry( - ctx context.Context, f func() error, logMsg string, ) error { diff --git a/pkg/geoipupdate/geoip_updater_test.go b/pkg/geoipupdate/geoip_updater_test.go index e7b20512..2c4d4565 100644 --- a/pkg/geoipupdate/geoip_updater_test.go +++ b/pkg/geoipupdate/geoip_updater_test.go @@ -41,7 +41,7 @@ func TestFullDownload(t *testing.T) { require.NoError(t, err) // mock metadata handler. - metadataHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + metadataHandler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { jsonData := ` { "databases": [ @@ -65,20 +65,21 @@ func TestFullDownload(t *testing.T) { ` w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - w.Write([]byte(jsonData)) + _, err := w.Write([]byte(jsonData)) + require.NoError(t, err) }) // mock download handler. downloadHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - edition := strings.Split(r.URL.Path, "/")[3] // extract the edition-id. + name := strings.Split(r.URL.Path, "/")[3] // extract the edition-id. var buf bytes.Buffer gw := gzip.NewWriter(&buf) tw := tar.NewWriter(gw) - content := "new " + edition + " content" + content := "new " + name + " content" header := &tar.Header{ - Name: "edition-1" + download.Extension, + Name: name + download.Extension, Size: int64(len(content)), } @@ -139,6 +140,7 @@ func TestFullDownload(t *testing.T) { // edition-1 file hasn't been modified. dbFile = filepath.Join(tempDir, "edition-1"+download.Extension) + //nolint:gosec // we need to read the content of the file in this test. fileContent, err := os.ReadFile(dbFile) require.NoError(t, err) require.Equal(t, "edition-1 content", string(fileContent)) @@ -148,6 +150,7 @@ func TestFullDownload(t *testing.T) { // edition-2 file has been updated. dbFile = filepath.Join(tempDir, "edition-2"+download.Extension) + //nolint:gosec // we need to read the content of the file in this test. fileContent, err = os.ReadFile(dbFile) require.NoError(t, err) require.Equal(t, "new edition-2 content", string(fileContent)) @@ -159,6 +162,7 @@ func TestFullDownload(t *testing.T) { // edition-3 file has been downloaded. dbFile = filepath.Join(tempDir, "edition-3"+download.Extension) + //nolint:gosec // we need to read the content of the file in this test. fileContent, err = os.ReadFile(dbFile) require.NoError(t, err) require.Equal(t, "new edition-3 content", string(fileContent)) diff --git a/pkg/geoipupdate/internal/errors.go b/pkg/geoipupdate/internal/errors.go index 0fa5cae6..e2381148 100644 --- a/pkg/geoipupdate/internal/errors.go +++ b/pkg/geoipupdate/internal/errors.go @@ -14,7 +14,12 @@ type ResponseError struct { } func (e ResponseError) Error() string { - return fmt.Sprintf("received: HTTP status code '%d' - Error code '%s' - Message '%s'", e.StatusCode, e.Code, e.Message) + return fmt.Sprintf("received: "+ + "HTTP status code '%d' - "+ + "Error code '%s' - "+ + "Message '%s'", + e.StatusCode, e.Code, e.Message, + ) } // IsPermanentError returns true if the error is non-retriable.