mirror of
https://github.com/tgdrive/teldrive.git
synced 2026-01-16 14:07:18 +08:00
3.6 KiB
3.6 KiB
TelDrive Agent Guidelines
This file contains instructions for agentic coding agents working on the TelDrive codebase.
1. Build, Lint, and Test Commands
The project uses task (Taskfile.yml) for build automation and standard Go toolchain.
Build
- Build Server:
task server(builds binary tobin/teldrive) - Build UI:
task ui(downloads frontend assets) - Full Build:
task(runsuithenserver) - Install Dependencies:
task deps(runsgo mod download && go mod tidy) - Generate Code:
task gen(runsgo generate ./...)
Test
- Run All Tests:
go test ./... - Run Specific Package:
go test ./internal/config/... - Run Specific Test:
go test -v ./internal/config -run TestConfigLoader - Run with Race Detector:
go test -race ./...
Lint
- Run Linter:
task lint(runsgolangci-lint run) - Format Code:
go fmt ./...
2. Code Style & Guidelines
Formatting & Style
- Go Standard: Strictly follow
gofmtand standard Go idioms. - Imports: Group imports into standard library, 3rd party, and local packages.
- Line Length: Aim for readable line lengths (soft limit ~120 chars), but prioritize readability.
Project Structure
cmd/: Entry points for the application commands (server, check, etc.).internal/: Private application code.config/: Configuration loading (Koanf based).database/: Database interactions (GORM).tgc/: Telegram client wrappers.
pkg/: Library code that might be imported by other projects.services/: Core business logic and API handlers.models/: GORM database models.
Naming Conventions
- Interfaces: Named with
-ersuffix (e.g.,Reader,Writer) where appropriate. - Structs: PascalCase.
- Variables: camelCase.
- Constants: PascalCase or SCREAMING_SNAKE_CASE.
- Package Names: Short, lowercase, single word (e.g.,
config,auth).
Error Handling
- Wrapping: Use
%wto wrap errors to preserve context. - Check Errors: Never swallow errors. Handle them or return them.
- Panic: Avoid panic except during initialization/startup where recovery is impossible.
- Logging: Use
zaplogger vialogging.FromContext(ctx).- Levels: Use
Debugfor trace info,Infofor general ops,Errorfor failures. - Fields: Use structured logging (e.g.,
zap.String("key", "value")).
- Levels: Use
Configuration
- Library: Uses
knadh/koanf/v2. - Loading: Loads from Defaults -> Config File -> Env Vars -> Flags.
- Validation: Uses
go-playground/validator. Ensure struct tagsvalidate:"..."are present. - Mapping: Use explicit mapping in
populatemethod ininternal/config/config.go(avoid reflection where possible).
Database
- Library: GORM with PostgreSQL.
- Migrations: Use SQL migrations in
internal/database/migrationsor GORM auto-migration if configured. - Context: Always pass
ctxto database calls.
Telegram Client (TGC)
- Library:
gotd/td. - Session: Managed via
internal/tgcand database. - Concurrency: Be mindful of Telegram rate limits. Use
tgc.NewMiddlewarewith rate limiters.
Frontend
- Location:
ui/directory. - Assets: Embeds frontend in binary via
embedpackage or serves fromui/dist.
3. Development Workflow
- Dependencies: Ensure
taskis installed. - Generate: Run
task genif modifying API definitions or generated code. - Test: Write unit tests for new logic, especially in
internal/packages. - Lint: Run linter before committing.