You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+
## Project Overview
6
+
7
+
flanksource/commons is a Go utility library providing common functionality for the Flanksource ecosystem. It's a collection of reusable modules organized by concern: core utilities, infrastructure helpers, development tools, data processing, and system integration.
8
+
9
+
## Key Commands
10
+
11
+
```bash
12
+
# Testing
13
+
make test# Run all tests with verbose output
14
+
go test ./... # Run all tests (standard Go)
15
+
go test ./logger/... # Run tests for a specific package
16
+
17
+
# Linting and Code Quality
18
+
make lint # Run golangci-lint with project-specific rules
19
+
20
+
# Dependency Management
21
+
make tidy # Clean up go.mod dependencies
22
+
go mod tidy # Standard Go dependency cleanup
23
+
24
+
# Build
25
+
go build # Build the dependency installer binary
26
+
go run main.go <dep># Download and install a dependency (e.g., go run main.go jq)
27
+
```
28
+
29
+
## Architecture and Code Organization
30
+
31
+
### Module Structure
32
+
The codebase follows a flat package structure where each directory represents a focused utility module:
33
+
34
+
-**HTTP Client (`http/`)**: Enhanced HTTP client with authentication middleware pattern. Supports Digest, NTLM, OAuth2, and custom auth. Uses functional options for configuration.
35
+
-**Logger (`logger/`)**: Dual logging system supporting both logrus and slog backends. Global logger instance with interface-based design for flexibility.
36
+
-**Collections (`collections/`)**: Generic utilities for maps, slices, sets, and priority queues. Heavy use of Go generics.
37
+
-**Dependency Management (`deps/`)**: Template-based system for downloading and installing external binaries. Supports cross-platform paths and verification.
38
+
-**Properties (`properties/`)**: Global configuration management with environment variable support and structured properties.
39
+
40
+
### Key Patterns
41
+
42
+
1.**Global State Pattern**: Logger and Properties packages use global instances initialized via `Init()` functions. Always check if initialization is needed before using these packages.
43
+
44
+
2.**Middleware Pattern**: HTTP client uses layered middleware for auth, tracing, and logging:
45
+
```go
46
+
client:= http.NewClient().
47
+
WithLogger(logger).
48
+
WithAuth(username, password).
49
+
WithTrace(true)
50
+
```
51
+
52
+
3.**Interface-First Design**: Most packages expose interfaces (e.g., `Logger` interface) allowing for mock implementations in tests.
53
+
54
+
4.**Builder Pattern**: Used extensively in HTTP client and dependency configurations for fluent API design.
55
+
56
+
### Testing Approach
57
+
58
+
- Uses testify for assertions in most tests
59
+
- Some modules (like `diff/`) use Ginkgo/Gomega for BDD-style tests
60
+
- Test files are colocated with source files following `*_test.go` convention
61
+
- No global test setup/teardown - each test is self-contained
62
+
63
+
### Important Development Notes
64
+
65
+
1.**Go Version**: Requires Go 1.23.0+ (uses newer generic features)
66
+
2.**Linting**: The project has custom golangci-lint rules that disable certain checks for HTTP-related naming
67
+
3.**Imports**: When adding new functionality, prefer using existing utilities from within the project (e.g., use `collections` package for slice operations)
68
+
4.**Error Handling**: The codebase uses explicit error returns rather than panics - maintain this pattern
69
+
5.**Logging**: Use the `logger` package for all logging needs, avoid fmt.Print statements
70
+
71
+
### Common Tasks
72
+
73
+
To add a new utility module:
74
+
1. Create a new directory at the root level
75
+
2. Follow the existing pattern of having a main file with the package name
76
+
3. Include comprehensive tests in `*_test.go` files
77
+
4. Update imports in other packages if the utility is widely useful
78
+
79
+
To modify the HTTP client:
80
+
1. Changes go in `http/client.go` or `http/middleware.go`
81
+
2. Authentication providers are in `http/auth_*.go` files
82
+
3. Always maintain backward compatibility with the builder pattern
83
+
84
+
To work with the dependency installer:
85
+
1. Dependencies are defined in `deps/binary.go`
86
+
2. Binary definitions include version, download URLs, and install paths
0 commit comments