Skip to content

Commit

Permalink
Change console render library (#29)
Browse files Browse the repository at this point in the history
* Change console render libery

* Updated prefixes

* Fixed tests

* Refactored default styles

* Replace PrintResponse method

* Changed error handler

* Remove dead code

* Fixed logger

* Cleanup packages

* Replaced logger

* Added tests

* Updated colours

* Refactored logger styles

* Fixed double empty lines

* Fixed prefix in logs

* Fixed lint issues

* Fixed message box

* Fixed tests

* Added tests for print response

* Added tests for box message printer

* Fixed tests

* Refactor tests

* Added logger tests

* Fixed printing width
  • Loading branch information
evg4b committed Sep 22, 2024
1 parent 4041077 commit a7a6433
Show file tree
Hide file tree
Showing 60 changed files with 1,639 additions and 1,445 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ dist/
node_modules
.uncors.yaml
coverage.out
.DS_Store
uncors
!uncors/
41 changes: 20 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,53 @@ go 1.22.2
require (
github.com/PuerkitoBio/purell v1.2.1
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/charmbracelet/lipgloss v0.10.0
github.com/charmbracelet/log v0.4.0
github.com/deckarep/golang-set/v2 v2.6.0
github.com/dustin/go-humanize v1.0.1
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a
github.com/gobuffalo/validate v2.0.4+incompatible
github.com/gojuno/minimock/v3 v3.4.0
github.com/gojuno/minimock/v3 v3.3.7
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/go-version v1.6.0
github.com/mitchellh/mapstructure v1.5.0
github.com/muesli/termenv v0.15.2
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/pterm/pterm v0.12.79
github.com/samber/lo v1.47.0
github.com/samber/lo v1.39.0
github.com/spf13/afero v1.11.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.29.0
golang.org/x/net v0.24.0
)

require (
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
)

require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/containerd/console v1.0.4 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0
github.com/gookit/color v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
174 changes: 51 additions & 123 deletions go.sum

Large diffs are not rendered by default.

16 changes: 7 additions & 9 deletions internal/config/mappings.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
package config

import (
"fmt"
"strings"

"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/pkg/urlx"
"github.com/samber/lo"
)

type Mappings []Mapping

func (m Mappings) String() string {
builder := &strings.Builder{}
var lines []string

for _, group := range lo.GroupBy(m, extractHost) {
for _, mapping := range group {
helpers.FPrintf(builder, "%s => %s\n", mapping.From, mapping.To)
lines = append(lines, fmt.Sprintf("%s => %s", mapping.From, mapping.To))
}

mapping := group[0]
for _, mock := range mapping.Mocks {
helpers.FPrintf(builder, " mock: %s\n", mock.String())
lines = append(lines, fmt.Sprintf(" mock: %s", mock.String()))
}
for _, static := range mapping.Statics {
helpers.FPrintf(builder, " static: %s\n", static.String())
lines = append(lines, fmt.Sprintf(" static: %s", static.String()))
}
for _, cacheGlob := range mapping.Cache {
helpers.FPrintf(builder, " cache: %s\n", cacheGlob)
lines = append(lines, fmt.Sprintf(" cache: %s", cacheGlob))
}
}

builder.WriteString("\n")

return builder.String()
return strings.Join(lines, "\n")
}

func extractHost(item Mapping) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,6 @@ func TestMappings(t *testing.T) {

actual := mappings.String()

assert.Equal(t, "\n", actual)
assert.Equal(t, "", actual)
})
}
13 changes: 7 additions & 6 deletions internal/contracts/logger.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package contracts

type Logger interface {
Error(a ...any)
Error(msg any, keyvals ...any)
Errorf(template string, a ...any)
Warning(a ...any)
Warningf(template string, a ...any)
Info(a ...any)
Warn(msg any, keyvals ...any)
Warnf(template string, a ...any)
Info(msg any, keyvals ...any)
Infof(template string, a ...any)
Debug(a ...any)
Debug(msg any, keyvals ...any)
Debugf(template string, a ...any)
PrintResponse(request *Request, code int)
Print(msg any, keyvals ...any)
Printf(format string, args ...any)
}
3 changes: 2 additions & 1 deletion internal/handler/cache/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/internal/tui"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (m *Middleware) cacheRequest(writer contracts.ResponseWriter, request *cont
m.logger.Debugf("extracted %s from request", cacheKey)

m.writeCachedResponse(writer, cachedResponse)
m.logger.PrintResponse(request, writer.StatusCode())
tui.PrintResponse(m.logger, request, writer.StatusCode())

return
}
Expand Down
10 changes: 6 additions & 4 deletions internal/handler/cache/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package cache_test

import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/charmbracelet/log"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/handler/cache"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testutils"
"github.com/go-http-utils/headers"
goCache "github.com/patrickmn/go-cache"
Expand All @@ -30,7 +32,7 @@ func TestCacheMiddleware(t *testing.T) {

middleware := cache.NewMiddleware(
cache.WithCacheStorage(goCache.New(time.Minute, time.Minute)),
cache.WithLogger(mocks.NewNoopLogger(t)),
cache.WithLogger(log.New(io.Discard)),
cache.WithMethods([]string{http.MethodGet}),
cache.WithGlobs(config.CacheGlobs{
"/translations",
Expand Down Expand Up @@ -184,7 +186,7 @@ func TestCacheMiddleware(t *testing.T) {

middleware := cache.NewMiddleware(
cache.WithCacheStorage(goCache.New(time.Minute, time.Minute)),
cache.WithLogger(mocks.NewNoopLogger(t)),
cache.WithLogger(log.New(io.Discard)),
cache.WithMethods([]string{http.MethodGet}),
cache.WithGlobs(config.CacheGlobs{cacheGlob}),
)
Expand Down Expand Up @@ -212,7 +214,7 @@ func TestCacheMiddleware(t *testing.T) {

middleware := cache.NewMiddleware(
cache.WithCacheStorage(goCache.New(time.Minute, time.Minute)),
cache.WithLogger(mocks.NewNoopLogger(t)),
cache.WithLogger(log.New(io.Discard)),
cache.WithMethods(methods),
cache.WithGlobs(config.CacheGlobs{cacheGlob}),
)
Expand Down
3 changes: 2 additions & 1 deletion internal/handler/mock/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/internal/infra"
"github.com/evg4b/uncors/internal/tui"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func (h *Handler) ServeHTTP(writer contracts.ResponseWriter, request *contracts.
h.serveRawContent(writer)
}

h.logger.PrintResponse(request, writer.StatusCode())
tui.PrintResponse(h.logger, request, writer.StatusCode())
}

func normaliseCode(code int) int {
Expand Down
16 changes: 9 additions & 7 deletions internal/handler/mock/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package mock_test

import (
"context"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"

"github.com/charmbracelet/log"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/handler/mock"
"github.com/evg4b/uncors/testing/hosts"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testconstants"
"github.com/evg4b/uncors/testing/testutils"
"github.com/go-http-utils/headers"
Expand Down Expand Up @@ -66,7 +68,7 @@ func TestHandler(t *testing.T) {
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
handler := mock.NewMockHandler(
mock.WithLogger(mocks.NewNoopLogger(t)),
mock.WithLogger(log.New(io.Discard)),
mock.WithResponse(testCase.response),
mock.WithFileSystem(fileSystem),
mock.WithAfter(func(_ time.Duration) <-chan time.Time {
Expand Down Expand Up @@ -134,7 +136,7 @@ func TestHandler(t *testing.T) {
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
handler := mock.NewMockHandler(
mock.WithLogger(mocks.NewNoopLogger(t)),
mock.WithLogger(log.New(io.Discard)),
mock.WithResponse(testCase.response),
mock.WithFileSystem(fileSystem),
mock.WithAfter(func(_ time.Duration) <-chan time.Time {
Expand Down Expand Up @@ -223,7 +225,7 @@ func TestHandler(t *testing.T) {
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
handler := mock.NewMockHandler(
mock.WithLogger(mocks.NewNoopLogger(t)),
mock.WithLogger(log.New(io.Discard)),
mock.WithResponse(testCase.response),
mock.WithFileSystem(fileSystem),
mock.WithAfter(func(_ time.Duration) <-chan time.Time {
Expand Down Expand Up @@ -271,7 +273,7 @@ func TestHandler(t *testing.T) {
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
handler := mock.NewMockHandler(
mock.WithLogger(mocks.NewNoopLogger(t)),
mock.WithLogger(log.New(io.Discard)),
mock.WithResponse(testCase.response),
mock.WithFileSystem(fileSystem),
mock.WithAfter(func(_ time.Duration) <-chan time.Time {
Expand Down Expand Up @@ -343,7 +345,7 @@ func TestHandler(t *testing.T) {
t.Run(testCase.name, func(t *testing.T) {
called := false
handler := mock.NewMockHandler(
mock.WithLogger(mocks.NewNoopLogger(t)),
mock.WithLogger(log.New(io.Discard)),
mock.WithResponse(testCase.response),
mock.WithFileSystem(fileSystem),
mock.WithAfter(func(duration time.Duration) <-chan time.Time {
Expand All @@ -366,7 +368,7 @@ func TestHandler(t *testing.T) {

t.Run("correctly cancel delay", func(t *testing.T) {
handler := mock.NewMockHandler(
mock.WithLogger(mocks.NewNoopLogger(t)),
mock.WithLogger(log.New(io.Discard)),
mock.WithResponse(config.Response{
Code: http.StatusOK,
Delay: 1 * time.Hour,
Expand Down
3 changes: 2 additions & 1 deletion internal/handler/proxy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/internal/infra"
"github.com/evg4b/uncors/internal/tui"
"github.com/evg4b/uncors/internal/urlreplacer"
)

Expand Down Expand Up @@ -70,7 +71,7 @@ func (h *Handler) executeQuery(request *http.Request) (*http.Response, error) {
if err != nil {
return nil, fmt.Errorf("failed to do reuest: %w", err)
}
h.logger.PrintResponse(originalResponse.Request, originalResponse.StatusCode)
tui.PrintResponse(h.logger, originalResponse.Request, originalResponse.StatusCode)

return originalResponse, nil
}
Expand Down
11 changes: 6 additions & 5 deletions internal/handler/proxy/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"strings"
"testing"

"github.com/charmbracelet/log"

"github.com/evg4b/uncors/internal/config"
"github.com/evg4b/uncors/internal/contracts"
"github.com/evg4b/uncors/internal/handler/proxy"
"github.com/evg4b/uncors/internal/helpers"
"github.com/evg4b/uncors/internal/urlreplacer"
"github.com/evg4b/uncors/pkg/urlx"
"github.com/evg4b/uncors/testing/hosts"
"github.com/evg4b/uncors/testing/mocks"
"github.com/evg4b/uncors/testing/testconstants"
"github.com/evg4b/uncors/testing/testutils"
"github.com/go-http-utils/headers"
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestProxyHandler(t *testing.T) {
handler := proxy.NewProxyHandler(
proxy.WithHTTPClient(httpClient),
proxy.WithURLReplacerFactory(replacerFactory),
proxy.WithLogger(mocks.NewNoopLogger(t)),
proxy.WithLogger(log.New(io.Discard)),
)

req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, targetURL.Path, nil)
Expand Down Expand Up @@ -122,7 +123,7 @@ func TestProxyHandler(t *testing.T) {
handler := proxy.NewProxyHandler(
proxy.WithHTTPClient(httpClient),
proxy.WithURLReplacerFactory(replacerFactory),
proxy.WithLogger(mocks.NewNoopLogger(t)),
proxy.WithLogger(log.New(io.Discard)),
)

req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, expectedURL.Path, nil)
Expand Down Expand Up @@ -156,7 +157,7 @@ func TestProxyHandler(t *testing.T) {
handler := proxy.NewProxyHandler(
proxy.WithHTTPClient(httpClient),
proxy.WithURLReplacerFactory(replacerFactory),
proxy.WithLogger(mocks.NewNoopLogger(t)),
proxy.WithLogger(log.New(io.Discard)),
)

req, err := http.NewRequestWithContext(context.TODO(), http.MethodPost, "/", nil)
Expand All @@ -183,7 +184,7 @@ func TestProxyHandler(t *testing.T) {
handler := proxy.NewProxyHandler(
proxy.WithHTTPClient(http.DefaultClient),
proxy.WithURLReplacerFactory(replacerFactory),
proxy.WithLogger(mocks.NewNoopLogger(t)),
proxy.WithLogger(log.New(io.Discard)),
)

t.Run("should correctly create response", func(t *testing.T) {
Expand Down
8 changes: 6 additions & 2 deletions internal/handler/proxy/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package proxy
import (
"net/http"

"github.com/evg4b/uncors/internal/tui"

"github.com/evg4b/uncors/internal/infra"
)

func (h *Handler) makeOptionsResponse(writer http.ResponseWriter, req *http.Request) {
func (h *Handler) makeOptionsResponse(writer http.ResponseWriter, req *http.Request) error {
infra.WriteCorsHeaders(writer.Header())
h.logger.PrintResponse(req, http.StatusOK)
tui.PrintResponse(h.logger, req, http.StatusOK)

return nil
}
Loading

0 comments on commit a7a6433

Please sign in to comment.