Portgen is a CLI tool designed to generate random unused ports within a specified range. It was born out of the frustration of having to think of unused ports for new projects, especially when dealing with multiple running containers.
To install Portgen, you need to have Go installed on your system. Then, you can use the following command:
go install github.com/lutefd/portgen/cmd/portgen@latest
Alternatively, you can clone the repository and build it manually:
git clone https://github.com/lutefd/portgen.git
cd portgen
make build
Portgen can be used in both interactive and non-interactive modes.
portgen [flags]
Flags:
-m, --min int
: Minimum port number (inclusive) (default 10000)-M, --max int
: Maximum port number (inclusive) (default 65535)-c, --copy
: Copy the generated port to clipboard-s, --short
: Print only the generated port number-h, --help
: Display help for portgen
To enter interactive mode, simply run portgen
without the -s
flag. In this mode, you can use the following commands:
generate
: Generate a new portcopy
: Copy the current port to clipboardtoggle
: Toggle clipboard mode on/off - useful for copying multiple ports in a rowhelp
: Show the help message
The project is organized as follows:
portgen/
├── cmd/
│ └── portgen/
│ └── main.go
├── internal/
│ ├── app/
│ │ ├── app.go
│ │ └── app_test.go
│ ├── cli/
│ │ ├── cli.go
│ │ └── cli_test.go
│ ├── port/
│ │ ├── port.go
│ │ └── port_test.go
│ └── ui/
│ ├── model.go
│ ├── model_test.go
│ ├── styles.go
│ └── styles_test.go
├── .gitignore
├── go.mod
├── go.sum
├── LICENSE
├── Makefile
└── README.md
cmd/portgen
: Contains the main entry point of the application.internal/app
: Implements the core functionality of generating ports and clipboard operations.internal/cli
: Handles the command-line interface using Cobra.internal/port
: Manages port generation and checking if a port is in use.internal/ui
: Implements the interactive UI using Bubble Tea.
To set up the development environment, follow these steps:
-
Clone the repository:
git clone https://github.com/Lutefd/portgen.git cd portgen
-
Install dependencies:
make deps
-
Build the project:
make build
-
Run tests:
make test
The project includes a Makefile with the following commands:
make all
: Run tests and build the projectmake build
: Build the projectmake test
: Run all testsmake clean
: Clean up build artifactsmake run
: Build and run the projectmake deps
: Install dependenciesmake build-linux
: Build for Linux (cross-compilation)
Contributions to Portgen are welcome! Here are some ways you can contribute:
- Report bugs or request features by opening an issue.
- Improve documentation.
- Submit pull requests with bug fixes or new features.
Please ensure that your code adheres to the existing style and that all tests pass before submitting a pull request.
Portgen is open-source software licensed under the MIT license. See the LICENSE file for more details.