A fast and reliable Go CLI tool for managing import aliases consistently across your entire codebase using the Language Server Protocol (LSP).
- π Fast: Uses persistent LSP connections for processing multiple files
- π Accurate: Leverages
goplsfor precise Go code analysis and refactoring - π¦ Comprehensive: Scans entire modules or specific package patterns
- π‘οΈ Safe: Skips generated files and provides clear feedback
- π― Consistent: Ensures uniform import aliases across your codebase
- π Informative: Shows detailed location information for all import usages
# Install goalias
go install github.com/jackchuka/goalias/cmd/goalias@latest
# Install gopls (required dependency)
go install golang.org/x/tools/gopls@latestBoth goalias and gopls must be available in your $PATH.
# List all current aliases for a package
goalias list -p github.com/example/mypackage
# Set a consistent alias across your entire module
goalias set -p github.com/example/mypackage -a mypkg
# Set alias for specific package patterns
goalias set -p github.com/example/mypackage -a mypkg ./cmd/... ./internal/...Update import aliases consistently across your codebase:
# Set alias for entire module
goalias set -p github.com/example/mypackage -a mypkg
# Set alias for specific package patterns
goalias set -p github.com/example/mypackage -a mypkg ./cmd/...
# Multiple patterns
goalias set -p github.com/example/mypackage -a mypkg ./cmd/... ./internal/...Display all current aliases and their locations:
# List aliases for entire module
goalias list -p github.com/example/mypackage
# List aliases for specific package patterns
goalias list -p github.com/example/mypackage ./cmd/...Example output:
LOCATION ALIAS
-------- -----
handler/foo.go:4 myutils
handler/bar.go:6 myutils
cmd/server/main.go:8 mypackage
Sets or updates import aliases across specified packages.
goalias set --package <importPath> --alias <name> [patterns...]Required Flags:
--package,-p: Full import path to manage--alias,-a: Desired alias identifier
Optional Arguments:
patterns: Go package patterns (defaults to./...)
Examples:
# Set alias for entire module
goalias set -p github.com/gin-gonic/gin -a gin
# Set alias for specific directories
goalias set -p github.com/stretchr/testify/assert -a tassert ./tests/...
# Multiple patterns
goalias set -p github.com/pkg/errors -a pkg_errros ./cmd/... ./internal/...Lists all import aliases and their locations.
goalias list --package <importPath> [patterns...]Required Flags:
--package,-p: Full import path to search for
Optional Arguments:
patterns: Go package patterns (defaults to./...)
Examples:
# List all usages in module
goalias list -p github.com/gin-gonic/gin
# List usages in specific directories
goalias list -p github.com/stretchr/testify/assert ./tests/...- Package Discovery: Uses
go listto find Go packages matching your patterns - AST Parsing: Parses Go source files to locate import declarations
- Smart Filtering: Automatically skips generated files (containing
Code generated ... DO NOT EDIT) - LSP Integration: Uses persistent
goplsconnections for accurate code analysis and refactoring - Consistent Updates: Applies import alias changes atomically across all matching files
goalias is designed for speed and efficiency:
- Persistent LSP Connections: Reuses
goplssessions instead of spawning new processes - Batch Operations: Processes multiple files in a single LSP session
- Smart Caching: Leverages
goplsinternal caching for faster analysis
- Go 1.22 or later
gopls(Go Language Server)- Git (for repository operations)
git clone https://github.com/jackchuka/goalias.git
cd goalias
go build -o goalias ./cmd/goalias# Run all tests
go test ./...
# Run tests with verbose output
go test -v ./...
# Run tests with coverage
go test -cover ./...Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Add tests for new functionality
- Run tests (
go test ./...) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow standard Go formatting (
golangci-lint) - Add tests for new functionality
- Update documentation as needed
- Follow existing code patterns and conventions
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Cobra for CLI functionality
- Uses gopls for Go language server capabilities
- Inspired by the need for consistent import management in large Go codebases
If you encounter any issues or have questions:
- Check the existing issues
- Create a new issue with detailed information
- Include your Go version, OS, and example code when reporting bugs