Skip to content

Commit

Permalink
Made CI happy
Browse files Browse the repository at this point in the history
  • Loading branch information
ZerGo0 committed Sep 25, 2024
1 parent e8b4e56 commit 8b29c76
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
8 changes: 4 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ linters:
# Enable specific linter
# https://golangci-lint.run/usage/linters/
enable:
- cyclop # Go linter that checks if the cyclic complexity of a function is acceptable
- copyloopvar # checks for pointers to enclosing loop variables
# - cyclop # Go linter that checks if the cyclic complexity of a function is acceptable
- dogsled # Checks assignments with too many blank identifiers (e.g. x, , , _, := f())
- dupl # Tool for code clone detection
- err113 # Golang linter to check the errors handling expressions
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
- exhaustive # check exhaustiveness of enum switch statements
- exportloopref # checks for pointers to enclosing loop variables
- funlen # Tool for detection of long functions
- gochecknoglobals # A global variable is a variable declared in package scope and that can be read and written to by any function within the package.
- gocritic # Provides diagnostics that check for bugs, performance and style issues.
- goconst # Inspects source code for security problems
- gocyclo # Computes and checks the cyclomatic complexity of functions
- goerr113 # Golang linter to check the errors handling expressions
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
- goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt.
- gomnd # An analyzer to detect magic numbers.
- goprintffuncname # Checks that printf-like functions are named with f at the end
- gosec # Inspects source code for security problems
- misspell # Finds commonly misspelled English words in comments
- mnd # An analyzer to detect magic numbers.
- nakedret # Finds naked returns in functions greater than a specified function length
- nestif # Reports deeply nested if statements
- nilerr # Finds the code that returns nil even if it checks that the error is not nil.
Expand Down
3 changes: 3 additions & 0 deletions internal/log/access_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"bytes"
"context"
"log/slog"
"os"
"testing"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/stretchr/testify/require"
)

func TestInterceptorLogger(t *testing.T) {
os.Setenv("ENV", "prod")

testcases := []struct {
Name string

Expand Down
3 changes: 3 additions & 0 deletions internal/log/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"encoding/json"
"io"
"log/slog"
"os"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
)

func TestNewSpanContextHandler(t *testing.T) {
os.Setenv("ENV", "prod")

jsonHandler := slog.NewJSONHandler(io.Discard, nil)

for _, testcase := range []struct {
Expand Down
2 changes: 2 additions & 0 deletions internal/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestNew(t *testing.T) {
os.Setenv("ENV", "prod")

for _, testcase := range []struct {
name string
level string
Expand Down
68 changes: 47 additions & 21 deletions pkg/services/fontsource/fontsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fontsource
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
Expand All @@ -14,6 +15,30 @@ import (
"time"
)

const (
useragent = "Fontsource Downloader (https://github.com/ZerGo0/fontsourcedownloader)"
filePerms = 0o600
timeoutSecs = 10
)

var (
errUnexpectedStatusCode = errors.New("unexpected status code")
)

type Font struct {
ID string `json:"id"`
Family string `json:"family"`
Subsets []string `json:"subsets"`
Weights []int `json:"weights"`
Styles []string `json:"styles"`
DefSubset string `json:"defSubset"`
Variable bool `json:"variable"`
LastModified string `json:"lastModified"`
Category string `json:"category"`
License string `json:"license"`
Type string `json:"type"`
}

func DownloadFonts(ctx context.Context, logger *slog.Logger, outputDir, formats, weights, styles, subsets string) error {
logger.InfoContext(ctx, "starting font source downloader")

Expand Down Expand Up @@ -56,28 +81,22 @@ func DownloadFonts(ctx context.Context, logger *slog.Logger, outputDir, formats,
return nil
}

type Font struct {
ID string `json:"id"`
Family string `json:"family"`
Subsets []string `json:"subsets"`
Weights []int `json:"weights"`
Styles []string `json:"styles"`
DefSubset string `json:"defSubset"`
Variable bool `json:"variable"`
LastModified string `json:"lastModified"`
Category string `json:"category"`
License string `json:"license"`
Type string `json:"type"`
}

func fetchFonts(ctx context.Context, logger *slog.Logger) ([]Font, error) {
logger.InfoContext(ctx, "fetching fonts")

httpClient := &http.Client{
Timeout: 10 * time.Second,
Timeout: timeoutSecs * time.Second,
}

resp, err := httpClient.Get("https://api.fontsource.org/v1/fonts")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.fontsource.org/v1/fonts", nil)
if err != nil {
logger.ErrorContext(ctx, "an error occurred", slog.String("error", err.Error()))
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", useragent)

resp, err := httpClient.Do(req)
if err != nil {
logger.ErrorContext(ctx, "an error occurred", slog.String("error", err.Error()))
return nil, err
Expand All @@ -86,7 +105,7 @@ func fetchFonts(ctx context.Context, logger *slog.Logger) ([]Font, error) {

if resp.StatusCode != http.StatusOK {
logger.ErrorContext(ctx, "an error occurred", slog.String("error", resp.Status))
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
return nil, fmt.Errorf("%w: %d", errUnexpectedStatusCode, resp.StatusCode)
}

var fonts []Font
Expand All @@ -103,14 +122,14 @@ func downloadFont(ctx context.Context, logger *slog.Logger, outputDir, formats,
logger.InfoContext(ctx, "downloading font", slog.String("id", font.ID))

httpClient := &http.Client{
Timeout: 10 * time.Second,
Timeout: timeoutSecs * time.Second,
}

splitFormats := strings.Split(formats, ",")
splitStyles := strings.Split(styles, ",")
splitSubsets := strings.Split(subsets, ",")

var splitWeights []int
splitWeights := make([]int, 0)
for _, weight := range strings.Split(weights, ",") {
convertedWeight, err := strconv.Atoi(weight)
if err != nil {
Expand All @@ -129,7 +148,14 @@ func downloadFont(ctx context.Context, logger *slog.Logger, outputDir, formats,
for _, subset := range splitSubsets {
url := fmt.Sprintf("https://cdn.jsdelivr.net/fontsource/fonts/%s@latest/%s-%d-%s.%s", font.ID, subset, weight, style, format)

resp, err := httpClient.Get(url)
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
logger.ErrorContext(ctx, "an error occurred", slog.String("error", err.Error()))
continue
}
req.Header.Set("User-Agent", useragent)

resp, err := httpClient.Do(req)
if err != nil {
logger.ErrorContext(ctx, "an error occurred", slog.String("error", err.Error()))
continue
Expand All @@ -150,7 +176,7 @@ func downloadFont(ctx context.Context, logger *slog.Logger, outputDir, formats,
continue
}

if err := os.WriteFile(outputPath, body, 0o644); err != nil {
if err := os.WriteFile(outputPath, body, filePerms); err != nil {
logger.ErrorContext(ctx, "an error occurred", slog.String("error", err.Error()))
continue
}
Expand Down

0 comments on commit 8b29c76

Please sign in to comment.