mirror of
https://github.com/lukaszraczylo/kportal.git
synced 2026-06-05 23:03:40 +00:00
chore: add golangci-lint v2 config and fix linter warnings (#46)
- [x] Add golangci-lint v2 configuration with formatters section - [x] Reorganize linters-settings under linters section - [x] Replace if-else chains with switch statements for clarity - [x] Wrap all ignored error returns with `_ = ` pattern - [x] Add OSC 8 hyperlink helper function for clickable ports - [x] Add blank line in table styling function - [x] Remove unnecessary type assertion in test
This commit is contained in:
+6
-4
@@ -1,23 +1,25 @@
|
|||||||
# golangci-lint configuration
|
# golangci-lint configuration
|
||||||
# https://golangci-lint.run/usage/configuration/
|
# https://golangci-lint.run/usage/configuration/
|
||||||
|
version: "2"
|
||||||
|
|
||||||
run:
|
run:
|
||||||
timeout: 5m
|
timeout: 5m
|
||||||
tests: true
|
tests: true
|
||||||
|
|
||||||
|
formatters:
|
||||||
|
enable:
|
||||||
|
- gofmt
|
||||||
|
|
||||||
linters:
|
linters:
|
||||||
enable:
|
enable:
|
||||||
- errcheck
|
- errcheck
|
||||||
- gosimple
|
|
||||||
- govet
|
- govet
|
||||||
- ineffassign
|
- ineffassign
|
||||||
- staticcheck
|
- staticcheck
|
||||||
- unused
|
- unused
|
||||||
- gosec
|
- gosec
|
||||||
- gocritic
|
- gocritic
|
||||||
- gofmt
|
settings:
|
||||||
|
|
||||||
linters-settings:
|
|
||||||
govet:
|
govet:
|
||||||
enable:
|
enable:
|
||||||
- fieldalignment
|
- fieldalignment
|
||||||
|
|||||||
+3
-2
@@ -347,10 +347,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Populate headers based on direction
|
// Populate headers based on direction
|
||||||
if entry.Direction == "request" {
|
switch entry.Direction {
|
||||||
|
case "request":
|
||||||
uiEntry.RequestHeaders = entry.Headers
|
uiEntry.RequestHeaders = entry.Headers
|
||||||
uiEntry.RequestBody = entry.Body
|
uiEntry.RequestBody = entry.Body
|
||||||
} else if entry.Direction == "response" {
|
case "response":
|
||||||
uiEntry.ResponseHeaders = entry.Headers
|
uiEntry.ResponseHeaders = entry.Headers
|
||||||
uiEntry.ResponseBody = entry.Body
|
uiEntry.ResponseBody = entry.Body
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ func (r *Runner) makeRequest(ctx context.Context, cfg Config) (statusCode int, b
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, bytesWritten, err
|
return 0, 0, bytesWritten, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
|
||||||
// Read response body to measure bytes
|
// Read response body to measure bytes
|
||||||
respBody, err := io.ReadAll(resp.Body)
|
respBody, err := io.ReadAll(resp.Body)
|
||||||
|
|||||||
@@ -423,7 +423,7 @@ func TestWatcher_HandleReload_LoadError(t *testing.T) {
|
|||||||
defer watcher.Stop()
|
defer watcher.Stop()
|
||||||
|
|
||||||
// Delete the config file to cause load error
|
// Delete the config file to cause load error
|
||||||
os.Remove(configPath)
|
_ = os.Remove(configPath)
|
||||||
|
|
||||||
// Call handleReload directly
|
// Call handleReload directly
|
||||||
watcher.handleReload()
|
watcher.handleReload()
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ func TestPortChecker_CheckAvailability_ExcludeMap(t *testing.T) {
|
|||||||
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
||||||
listener, err := net.Listen("tcp", ":0")
|
listener, err := net.Listen("tcp", ":0")
|
||||||
assert.NoError(t, err, "should create listener")
|
assert.NoError(t, err, "should create listener")
|
||||||
defer listener.Close()
|
defer func() { _ = listener.Close() }()
|
||||||
|
|
||||||
// Get the port that's now occupied
|
// Get the port that's now occupied
|
||||||
addr := listener.Addr().(*net.TCPAddr)
|
addr := listener.Addr().(*net.TCPAddr)
|
||||||
@@ -236,12 +236,12 @@ func TestPortChecker_CheckAvailability_MultipleSkipPorts(t *testing.T) {
|
|||||||
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
||||||
listener1, err := net.Listen("tcp", ":0")
|
listener1, err := net.Listen("tcp", ":0")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
defer listener1.Close()
|
defer func() { _ = listener1.Close() }()
|
||||||
|
|
||||||
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
||||||
listener2, err := net.Listen("tcp", ":0")
|
listener2, err := net.Listen("tcp", ":0")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
defer listener2.Close()
|
defer func() { _ = listener2.Close() }()
|
||||||
|
|
||||||
port1 := listener1.Addr().(*net.TCPAddr).Port
|
port1 := listener1.Addr().(*net.TCPAddr).Port
|
||||||
port2 := listener2.Addr().(*net.TCPAddr).Port
|
port2 := listener2.Addr().(*net.TCPAddr).Port
|
||||||
@@ -360,7 +360,7 @@ func TestPortChecker_PortAvailability_Integration(t *testing.T) {
|
|||||||
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
// #nosec G102 -- test intentionally binds to all interfaces to match production port checking
|
||||||
listener, err := net.Listen("tcp", ":0")
|
listener, err := net.Listen("tcp", ":0")
|
||||||
assert.NoError(t, err, "should create listener")
|
assert.NoError(t, err, "should create listener")
|
||||||
defer listener.Close()
|
defer func() { _ = listener.Close() }()
|
||||||
|
|
||||||
// Get the occupied port
|
// Get the occupied port
|
||||||
occupiedPort := listener.Addr().(*net.TCPAddr).Port
|
occupiedPort := listener.Addr().(*net.TCPAddr).Port
|
||||||
@@ -370,7 +370,7 @@ func TestPortChecker_PortAvailability_Integration(t *testing.T) {
|
|||||||
assert.False(t, available, "occupied port should not be available")
|
assert.False(t, available, "occupied port should not be available")
|
||||||
|
|
||||||
// Close the listener
|
// Close the listener
|
||||||
listener.Close()
|
_ = listener.Close()
|
||||||
|
|
||||||
// The port should now be available (though there might be a brief delay)
|
// The port should now be available (though there might be a brief delay)
|
||||||
// We don't assert this to avoid flakiness in CI environments
|
// We don't assert this to avoid flakiness in CI environments
|
||||||
|
|||||||
@@ -439,7 +439,7 @@ func (c *Checker) checkDataTransfer(port int) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer func() { _ = conn.Close() }()
|
||||||
|
|
||||||
// Set a short read deadline to detect hung connections
|
// Set a short read deadline to detect hung connections
|
||||||
// We don't expect to receive data, but we want to verify the connection isn't hung
|
// We don't expect to receive data, but we want to verify the connection isn't hung
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func (s *HealthCheckTestSuite) TearDownTest() {
|
|||||||
s.checker.Stop()
|
s.checker.Stop()
|
||||||
}
|
}
|
||||||
if s.listener != nil {
|
if s.listener != nil {
|
||||||
s.listener.Close()
|
_ = s.listener.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,17 +198,17 @@ func (s *HealthCheckTestSuite) TestDataTransferMethod() {
|
|||||||
case "banner":
|
case "banner":
|
||||||
_, _ = conn.Write([]byte("220 Welcome\r\n"))
|
_, _ = conn.Write([]byte("220 Welcome\r\n"))
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
case "close":
|
case "close":
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
case "silent":
|
case "silent":
|
||||||
// Just keep connection open
|
// Just keep connection open
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
conn.Close()
|
_ = conn.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
defer testListener.Close()
|
defer func() { _ = testListener.Close() }()
|
||||||
} else {
|
} else {
|
||||||
testPort = 54322 // Unused port
|
testPort = 54322 // Unused port
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func TestNewLogger_OutputModes(t *testing.T) {
|
|||||||
t.Run("empty logFile uses io.Discard", func(t *testing.T) {
|
t.Run("empty logFile uses io.Discard", func(t *testing.T) {
|
||||||
l, err := NewLogger("test-forward", "", 1024)
|
l, err := NewLogger("test-forward", "", 1024)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer l.Close()
|
defer func() { _ = l.Close() }()
|
||||||
|
|
||||||
assert.Nil(t, l.file)
|
assert.Nil(t, l.file)
|
||||||
assert.Equal(t, io.Discard, l.output)
|
assert.Equal(t, io.Discard, l.output)
|
||||||
@@ -34,7 +34,7 @@ func TestNewLogger_OutputModes(t *testing.T) {
|
|||||||
|
|
||||||
l, err := NewLogger("test-forward", logFile, 2048)
|
l, err := NewLogger("test-forward", logFile, 2048)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer l.Close()
|
defer func() { _ = l.Close() }()
|
||||||
|
|
||||||
assert.NotNil(t, l.file)
|
assert.NotNil(t, l.file)
|
||||||
assert.NotEqual(t, io.Discard, l.output)
|
assert.NotEqual(t, io.Discard, l.output)
|
||||||
@@ -58,7 +58,7 @@ func TestNewLogger_OutputModes(t *testing.T) {
|
|||||||
|
|
||||||
err = l.Log(Entry{Direction: "request"})
|
err = l.Log(Entry{Direction: "request"})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
l.Close()
|
_ = l.Close()
|
||||||
|
|
||||||
// File should have both contents
|
// File should have both contents
|
||||||
data, _ := os.ReadFile(logFile)
|
data, _ := os.ReadFile(logFile)
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ func TestNewLogger(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotNil(t, l)
|
require.NotNil(t, l)
|
||||||
assert.Nil(t, l.file) // No file when using stdout
|
assert.Nil(t, l.file) // No file when using stdout
|
||||||
l.Close()
|
_ = l.Close()
|
||||||
|
|
||||||
// Test file logger (using temp file)
|
// Test file logger (using temp file)
|
||||||
tmpFile := t.TempDir() + "/test.log"
|
tmpFile := t.TempDir() + "/test.log"
|
||||||
@@ -173,7 +173,7 @@ func TestNewLogger(t *testing.T) {
|
|||||||
err = l.Log(Entry{Direction: "request", Method: "GET"})
|
err = l.Log(Entry{Direction: "request", Method: "GET"})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
l.Close()
|
_ = l.Close()
|
||||||
|
|
||||||
// Verify file has content
|
// Verify file has content
|
||||||
data, err := os.ReadFile(tmpFile)
|
data, err := os.ReadFile(tmpFile)
|
||||||
|
|||||||
@@ -98,13 +98,13 @@ func (l *Logger) log(level Level, msg string, fields map[string]interface{}) {
|
|||||||
Fields: fields,
|
Fields: fields,
|
||||||
}
|
}
|
||||||
data, _ := json.Marshal(entry)
|
data, _ := json.Marshal(entry)
|
||||||
fmt.Fprintln(l.output, string(data))
|
_, _ = fmt.Fprintln(l.output, string(data))
|
||||||
} else {
|
} else {
|
||||||
// Text format
|
// Text format
|
||||||
if len(fields) > 0 {
|
if len(fields) > 0 {
|
||||||
fmt.Fprintf(l.output, "[%s] %s %v\n", levelStr, msg, fields)
|
_, _ = fmt.Fprintf(l.output, "[%s] %s %v\n", levelStr, msg, fields)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(l.output, "[%s] %s\n", levelStr, msg)
|
_, _ = fmt.Fprintf(l.output, "[%s] %s\n", levelStr, msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -564,6 +564,11 @@ func (m model) buildTableRows() [][]string {
|
|||||||
|
|
||||||
statusIcon, statusText := m.getStatusIconAndText(id, fwd)
|
statusIcon, statusText := m.getStatusIconAndText(id, fwd)
|
||||||
|
|
||||||
|
localPortText := fmt.Sprintf("%d", fwd.LocalPort)
|
||||||
|
if fwd.Status == "Active" && !m.ui.isForwardDisabled(id) {
|
||||||
|
localPortText = hyperlink(fmt.Sprintf("http://127.0.0.1:%d", fwd.LocalPort), fmt.Sprintf("%d→", fwd.LocalPort))
|
||||||
|
}
|
||||||
|
|
||||||
rows = append(rows, []string{
|
rows = append(rows, []string{
|
||||||
truncate(fwd.Context, ColumnWidthContext),
|
truncate(fwd.Context, ColumnWidthContext),
|
||||||
truncate(fwd.Namespace, ColumnWidthNamespace),
|
truncate(fwd.Namespace, ColumnWidthNamespace),
|
||||||
@@ -571,7 +576,7 @@ func (m model) buildTableRows() [][]string {
|
|||||||
truncate(fwd.Type, ColumnWidthType),
|
truncate(fwd.Type, ColumnWidthType),
|
||||||
truncate(fwd.Resource, ColumnWidthResource),
|
truncate(fwd.Resource, ColumnWidthResource),
|
||||||
fmt.Sprintf("%d", fwd.RemotePort),
|
fmt.Sprintf("%d", fwd.RemotePort),
|
||||||
fmt.Sprintf("%d", fwd.LocalPort),
|
localPortText,
|
||||||
statusIcon + " " + statusText,
|
statusIcon + " " + statusText,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -642,6 +647,7 @@ func (m model) createTableStyleFunc(colors mainViewColors) func(row, col int) li
|
|||||||
return baseStyle.Foreground(colors.errorColor)
|
return baseStyle.Foreground(colors.errorColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return baseStyle
|
return baseStyle
|
||||||
|
|||||||
@@ -368,7 +368,7 @@ func TestHTTPLogEntry(t *testing.T) {
|
|||||||
func TestHTTPLogSubscriberType(t *testing.T) {
|
func TestHTTPLogSubscriberType(t *testing.T) {
|
||||||
// Test that our mock matches the type
|
// Test that our mock matches the type
|
||||||
mock := NewMockHTTPLogSubscriber()
|
mock := NewMockHTTPLogSubscriber()
|
||||||
var subscriber HTTPLogSubscriber = mock.GetSubscriberFunc()
|
subscriber := mock.GetSubscriberFunc()
|
||||||
|
|
||||||
// Test subscription
|
// Test subscription
|
||||||
callCount := 0
|
callCount := 0
|
||||||
|
|||||||
@@ -856,11 +856,12 @@ func TestModel_Update_ViewModeRouting(t *testing.T) {
|
|||||||
ui := NewBubbleTeaUI(nil, "1.0.0")
|
ui := NewBubbleTeaUI(nil, "1.0.0")
|
||||||
ui.mu.Lock()
|
ui.mu.Lock()
|
||||||
ui.viewMode = tt.viewMode
|
ui.viewMode = tt.viewMode
|
||||||
if tt.viewMode == ViewModeAddWizard {
|
switch tt.viewMode {
|
||||||
|
case ViewModeAddWizard:
|
||||||
ui.addWizard = newAddWizardState()
|
ui.addWizard = newAddWizardState()
|
||||||
} else if tt.viewMode == ViewModeBenchmark {
|
case ViewModeBenchmark:
|
||||||
ui.benchmarkState = newBenchmarkState("id", "alias", 8080)
|
ui.benchmarkState = newBenchmarkState("id", "alias", 8080)
|
||||||
} else if tt.viewMode == ViewModeHTTPLog {
|
case ViewModeHTTPLog:
|
||||||
ui.httpLogState = newHTTPLogState("id", "alias")
|
ui.httpLogState = newHTTPLogState("id", "alias")
|
||||||
}
|
}
|
||||||
ui.mu.Unlock()
|
ui.mu.Unlock()
|
||||||
|
|||||||
@@ -187,6 +187,13 @@ func (t *TableUI) Remove(id string) {
|
|||||||
delete(t.forwards, id)
|
delete(t.forwards, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hyperlink wraps text in an OSC 8 terminal hyperlink escape sequence.
|
||||||
|
// Clicking the text opens the URL in terminals that support it (Ghostty, iTerm2,
|
||||||
|
// Windows Terminal, Kitty, WezTerm, etc.). Unsupported terminals show plain text.
|
||||||
|
func hyperlink(url, text string) string {
|
||||||
|
return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\", url, text)
|
||||||
|
}
|
||||||
|
|
||||||
// truncate truncates a string to maxLen, adding "..." if needed
|
// truncate truncates a string to maxLen, adding "..." if needed
|
||||||
func truncate(s string, maxLen int) string {
|
func truncate(s string, maxLen int) string {
|
||||||
if len(s) <= maxLen {
|
if len(s) <= maxLen {
|
||||||
|
|||||||
@@ -661,12 +661,13 @@ func (m model) handleAddWizardEnter() (tea.Model, tea.Cmd) {
|
|||||||
Alias: wizard.alias,
|
Alias: wizard.alias,
|
||||||
}
|
}
|
||||||
|
|
||||||
if wizard.selectedResourceType == ResourceTypePodPrefix {
|
switch wizard.selectedResourceType {
|
||||||
|
case ResourceTypePodPrefix:
|
||||||
fwd.Resource = "pod/" + wizard.resourceValue
|
fwd.Resource = "pod/" + wizard.resourceValue
|
||||||
} else if wizard.selectedResourceType == ResourceTypePodSelector {
|
case ResourceTypePodSelector:
|
||||||
fwd.Resource = wizard.resourceValue
|
fwd.Resource = wizard.resourceValue
|
||||||
fwd.Selector = wizard.selector
|
fwd.Selector = wizard.selector
|
||||||
} else if wizard.selectedResourceType == ResourceTypeService {
|
case ResourceTypeService:
|
||||||
fwd.Resource = "service/" + wizard.resourceValue
|
fwd.Resource = "service/" + wizard.resourceValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1304,10 +1304,10 @@ func decompressContent(content string, headers map[string]string) string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return content // Return original on error
|
return content // Return original on error
|
||||||
}
|
}
|
||||||
defer reader.Close()
|
defer func() { _ = reader.Close() }()
|
||||||
case "deflate":
|
case "deflate":
|
||||||
reader = flate.NewReader(bytes.NewReader(data))
|
reader = flate.NewReader(bytes.NewReader(data))
|
||||||
defer reader.Close()
|
defer func() { _ = reader.Close() }()
|
||||||
default:
|
default:
|
||||||
// br (brotli), compress, zstd - not in stdlib, return original
|
// br (brotli), compress, zstd - not in stdlib, return original
|
||||||
return content
|
return content
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ func (c *Checker) fetchLatestRelease(ctx context.Context) (*ReleaseInfo, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer func() { _ = resp.Body.Close() }()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
|
return nil, fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
|
||||||
|
|||||||
Reference in New Issue
Block a user