This document outlines improvements to make LogBasset follow Go CLI application best practices, organized by priority.
Current Issues: Monolithic 408-line client.go, mixed responsibilities in commands, missing standard directories, tight coupling, no interfaces, global state variables.
Target Structure:
logbasset/
├── cmd/logbasset/main.go # Main application (moved from root)
├── internal/
│ ├── app/ # Application logic & version info
│ ├── cli/ # CLI command implementations
│ ├── client/ # API client (split: query.go, tail.go, auth.go)
│ ├── config/ # Configuration management
│ ├── output/ # Output formatting (JSON, CSV, table)
│ └── errors/ # Centralized error handling
├── pkg/ # Public APIs (if needed)
├── configs/ # Config templates
├── scripts/ # Build scripts
├── docs/ # Documentation
└── examples/ # Usage examples
Tasks:
- Add standard Go directories - Create directory structure above
- Move main.go to cmd/logbasset/ - Follow standard Go project layout
- Split monolithic client package - Break down 408-line client.go into focused files
- Create dedicated packages - Implement internal packages as shown above
- Add structured configuration management - Replace scattered environment variable reads with a config package using libraries like Viper
- Configuration validation - Add validation for config values (e.g., server URLs, token format)
- Eliminate global variables - Remove package-level vars (token, server, verbose) from cmd package
- Structured error types - Define custom error types for different failure modes (network, auth, validation)
- Better error messages - More descriptive errors with suggestions for resolution
- Exit codes - Use standard exit codes (0=success, 1=general error, 2=misuse, etc.)
- Input validation - Validate all user inputs (time formats, counts, etc.) before API calls
- Flag consistency - Standardize flag naming conventions across commands
- Global flags - Move common flags (server, token) to persistent flags
- Increase test coverage - Add comprehensive unit tests for all packages
- Interface abstractions - Add interfaces for the client to improve testability
- Mock HTTP responses - Use httptest for testing client functionality
- Dependency injection - Make HTTP client configurable/injectable for better testing
- Context propagation - Ensure context is properly passed through all operations
- Extract output logic - Move formatting logic from individual commands to shared formatters
- Separate business logic - Move formatting/output logic out of command handlers
- Consistent output - Standardize output formats across all commands
- Reduce tight coupling - Commands shouldn't directly instantiate client
- Config file support - Support for config files (YAML/JSON) in addition to environment variables
- Progress indicators - Add spinners/progress bars for long-running operations
- Graceful shutdown - Handle SIGINT/SIGTERM properly in tail command
- Auto-completion - Add shell completion support (bash, zsh, fish)
- Help improvements - Better examples in help text and man page generation
- Structured logging - Use a logging library (logrus/zap) instead of fmt.Fprintf to stderr
- Log levels - Support different verbosity levels (debug, info, warn, error)
- Token masking - Mask tokens in verbose output and logs
- Secure defaults - Use secure HTTP client settings (timeouts, TLS verification)
- Input sanitization - Sanitize user inputs before sending to API
- Integration tests - Add tests that verify CLI behavior end-to-end
- Table-driven tests - Convert existing tests to table-driven format where applicable
- Connection pooling - Configure HTTP client with appropriate connection limits
- Retry logic - Add exponential backoff for transient failures
- Rate limiting - Implement client-side rate limiting to respect API limits
- Memory optimization - Stream large responses instead of loading everything into memory
- Color support - Add colored output with ability to disable
- Paging support - Add built-in paging for long output
- Template support - Allow custom output templates
- Pre-commit hooks - Add git hooks for formatting, linting, and testing
- Development tooling - Add air/realize for hot reloading during development
- Linting improvements - Add more linters (golangci-lint config) and fix all issues
- Debugging support - Add pprof endpoints for performance debugging
- Add godoc comments - Document all exported functions, types, and packages
- Version information - Embed build info (commit, date) in binary
- License headers - Add license headers to source files
- Changelog automation - Use conventional commits and automated changelog generation
- Goreleaser optimization - Improve release configuration for better artifacts
- Docker support - Add Dockerfile and multi-stage builds
- Package managers - Add support for more package managers (Scoop for Windows, etc.)
- Binary size optimization - Use build flags to reduce binary size
- XDG Base Directory compliance - Store config in standard locations (
~/.config/logbasset/) - Request ID tracking - Add correlation IDs for API requests when verbose mode is enabled
- LogBasset has a solid foundation but will benefit significantly from implementing Go CLI best practices
- The priority ordering ensures foundational changes are made first, enabling cleaner implementation of features later
- Many improvements can be implemented incrementally without breaking existing functionality