Skip to content

Commit

Permalink
fix linting:
Browse files Browse the repository at this point in the history
  • Loading branch information
NajiObeid committed Feb 26, 2024
1 parent 2d903c1 commit 9180d72
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 32 deletions.
7 changes: 5 additions & 2 deletions pkg/geoipupdate/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ 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
// database.
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
Expand Down Expand Up @@ -59,7 +62,7 @@ type Download struct {
func New(
accountID int,
licenseKey string,
url string,
serverURL string,
proxy *url.URL,
databaseDir string,
preserveFileTimes bool,
Expand All @@ -80,7 +83,7 @@ func New(
licenseKey: licenseKey,
oldEditionsHash: map[string]string{},
preserveFileTimes: preserveFileTimes,
url: url,
url: serverURL,
now: time.Now,
verbose: verbose,
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/geoipupdate/download/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand All @@ -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"`
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/geoipupdate/download/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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()

Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions pkg/geoipupdate/download/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions pkg/geoipupdate/download/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
22 changes: 13 additions & 9 deletions pkg/geoipupdate/download/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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
},
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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())
},
},
Expand Down
8 changes: 3 additions & 5 deletions pkg/geoipupdate/geoip_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 9 additions & 5 deletions pkg/geoipupdate/geoip_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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)),
}

Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down
7 changes: 6 additions & 1 deletion pkg/geoipupdate/internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9180d72

Please sign in to comment.