Using Bingo CLI
Bingo CLI is the official CLI tool for the Bingo framework, used for quickly creating projects and generating code, significantly improving development efficiency.
Installation
go install github.com/bingo-project/bingoctl/cmd/bingo@latestTo install an older version (v1.4.x with built-in templates), build from source:
git clone https://github.com/bingo-project/bingoctl && cd bingoctl && git checkout v1.4.7 && go build -o bingo ./cmd/bingoctlSee CHANGELOG for version history
Verify installation:
bingo versionShell Completion
Bingo supports command-line auto-completion for multiple shells.
Zsh
# Temporary (current session)
source <(bingo completion zsh)
# Permanent
## Linux
bingo completion zsh > "${fpath[1]}/_bingo"
## macOS (Homebrew)
bingo completion zsh > $(brew --prefix)/share/zsh/site-functions/_bingoIf completion doesn't work, ensure
.zshrchas:autoload -U compinit; compinit
Bash
# Temporary (current session)
source <(bingo completion bash)
# Permanent
## Linux
bingo completion bash > /etc/bash_completion.d/bingo
## macOS (Homebrew)
bingo completion bash > $(brew --prefix)/etc/bash_completion.d/bingoRequires the
bash-completionpackage
Fish
bingo completion fish > ~/.config/fish/completions/bingo.fishPowerShell
bingo completion powershell > bingo.ps1
# Add the generated script to your PowerShell profileCore Features
1. Create Project
Create a complete Bingo project scaffold with one command:
bingo create github.com/myorg/myappGenerated project includes:
- Complete directory structure
- Configuration file templates
- Docker Compose configuration
- Makefile
- Basic example code
Create Command Options
Project Name and Module Name
# Use default module name (same as project name)
bingo create github.com/myorg/myapp
# Customize module name (-m option)
bingo create myapp -m github.com/mycompany/myappTemplate Version Control
# Use recommended version (default)
bingo create myapp
# Use specific version
bingo create myapp -r v1.2.3
# Use branch (development version)
bingo create myapp -r main
# Force re-download template
bingo create myapp -r main --no-cacheService Selection
Control which services are included in the project (apiserver, admserver, scheduler, bot, etc.):
# Only include apiserver (default)
bingo create myapp
# Create all available services
bingo create myapp --all
# or use shorthand
bingo create myapp -a
# Explicitly specify services (comma-separated)
bingo create myapp --services apiserver,admserver,scheduler
# Add service to default apiserver
bingo create myapp --add-service scheduler
# Exclude specific service
bingo create myapp --no-service bot
# Include only skeleton, no services
bingo create myapp --services noneGit Initialization
# Create project and initialize git repository (default)
bingo create myapp
# Create project without git initialization
bingo create myapp --init-git=falseBuild Options
# Create project without building (default)
bingo create myapp
# Create project and run make build
bingo create myapp --buildCache Management and Mirror Configuration
# Cache location: ~/.bingo/templates/
# For regions with GitHub access difficulties, configure mirror
export BINGO_TEMPLATE_MIRROR=https://ghproxy.com/
bingo create myapp
# Or temporarily set
BINGO_TEMPLATE_MIRROR=https://ghproxy.com/ bingo create myapp2. Code Generation
Quickly generate code for each layer following Bingo's best practices.
Global Options
-d, --directory string Specify directory for generated files
-p, --package string Specify package name
-t, --table string Read fields from database table
-s, --service string Target service name, for auto-inferring pathMulti-Service Support
When project contains multiple services, use --service parameter to auto-infer generation path:
# Generate code for default service (usually apiserver)
bingo make model user
# Generate for specific service, auto-inferring path
bingo make model user --service admserver
# Generate complete CRUD (for specified service)
bingo make crud order --service admserver
# Explicitly specify directory (highest priority, overrides --service)
bingo make model user -d custom/pathPath Inference Rules:
- Scan
cmd/directory to identify existing services - If configured path contains service name, intelligently replace (e.g.,
internal/apiserver/model→internal/admserver/model) - Otherwise use default pattern:
internal/{service}/{suffix}
Complete CRUD Code Generation
Generate all layers at once:
bingo make crud userAuto-generates:
internal/pkg/model/user.go- Data modelinternal/apiserver/store/user.go- Data access layerinternal/apiserver/biz/user/user.go- Business logic layerinternal/apiserver/controller/v1/user/user.go- Controller layerpkg/api/v1/user.go- Request/response definitions
And auto-registers to:
- Store interface
- Biz interface
- Routes
Generate Individual Layers
# Generate Model
bingo make model article
# Generate Store layer
bingo make store article
# Generate Biz layer
bingo make biz article
# Generate Controller layer
bingo make controller article
# Generate Request validation
bingo make request article3. Generate Code from Database
Auto-generate Model code from existing database tables:
# Generate single table
bingo gen -t users
# Generate multiple tables
bingo gen -t users,posts,commentsPrerequisites: Need to configure database connection in .bingo.yaml.
4. Database Migration
Generate database migration files:
bingo make migration create_users_tableGenerated migration files are located in internal/pkg/database/migration/.
Optional: Generate migration from database table
bingo make migration create_posts_table -t postsRun Migrations
bingo migrate <command> [options]
# Options
-v, --verbose Show detailed compilation output
--rebuild Force recompile migration program
-f, --force Force execution in production environment
# Subcommands
bingo migrate up # Run all pending migrations
bingo migrate rollback # Rollback the last batch of migrations
bingo migrate reset # Rollback all migrations
bingo migrate refresh # Rollback all and re-run migrations
bingo migrate fresh # Drop all tables and re-run migrationsConfigure Migration Table Name (optional, in .bingo.yaml):
migrate:
table: bingo_migration # Default value5. Generate Service Modules
Generate complete service modules:
# Generate API service with HTTP server
bingo make service api --http --with-store --with-controller
# Generate service with gRPC server
bingo make service rpc --grpc
# Generate service supporting both HTTP and gRPC
bingo make service gateway --http --grpc
# Generate pure worker service for business processing
bingo make service worker --no-bizService options:
--http: Enable HTTP server--grpc: Enable gRPC server--with-biz: Generate business layer (default true)--no-biz: Don't generate business layer--with-store: Generate storage layer--with-controller: Generate controller layer--with-middleware: Generate middleware directory--with-router: Generate router directory
6. Other Generators
# Generate middleware
bingo make middleware auth
# Generate scheduled task
bingo make job cleanup
# Generate data seeder
bingo make seeder users
# Generate CLI command
bingo make cmd serve7. Run Database Seeders
Run user-defined seeders to populate the database:
bingo db seed [options]
# Options
-v, --verbose Show detailed compilation output
--rebuild Force recompile seeder program
--seeder Specify seeder class name to run
# Examples
bingo db seed # Run all seeders
bingo db seed --seeder=User # Run only UserSeeder
bingo db seed -v # Show detailed outputConfiguration File
Copy example file to create .bingo.yaml in project root:
cp .bingo.example.yaml .bingo.yamlConfiguration file content:
version: v1
# Project package name
rootPackage: github.com/myorg/myapp
# Directory configuration
directory:
cmd: internal/bingoctl/cmd
model: internal/pkg/model
store: internal/apiserver/store
request: pkg/api/v1
biz: internal/apiserver/biz
controller: internal/apiserver/controller/v1
middleware: internal/pkg/middleware
job: internal/watcher/watcher
migration: internal/pkg/database/migration
seeder: internal/pkg/database/seeder
# Registry configuration
registries:
router: internal/apiserver/router/api.go
store:
filePath: internal/apiserver/store/store.go
interface: "IStore"
biz:
filePath: internal/apiserver/biz/biz.go
interface: "IBiz"
# Database configuration (for gen command)
mysql:
host: 127.0.0.1:3306
username: root
password: your-password
database: myappPractical Examples
Example 1: Create Blog System from Scratch
# 1. Create project
bingo create github.com/myorg/blog
cd blog
# 2. Configure database
vim .bingo.yaml # Modify mysql configuration
# 3. Start dependency services
docker-compose -f deployments/docker/docker-compose.yaml up -d
# 4. Generate user module
bingo make crud user
# 5. Generate post module
bingo make crud post
# 6. Generate comment module
bingo make crud comment
# 7. Run service
make build
./_output/platforms/<os>/<arch>/blog-apiserverExample 2: Generate Code from Existing Database
If you have an existing database:
# 1. Configure database connection
vim .bingo.yaml
# 2. Generate Model from database
bingo gen -t users,posts,comments,tags
# 3. Generate complete CRUD code for each table
bingo make crud user
bingo make crud post
bingo make crud comment
bingo make crud tagExample 3: Generate Scheduled Tasks
# 1. Generate task code
bingo make job daily_report
# 2. Edit task logic
vim internal/watcher/watcher/daily_report.go
# 3. Register task in schedulerExample 4: Generate New Microservice
# Generate independent notification service
bingo make service notification \
--http \
--with-store \
--with-controller \
--with-router
# Generate pure gRPC service
bingo make service user-grpc --grpcCommand Reference
Global Options
-c, --config string Configuration file path (default .bingo.yaml)create Command
bingo create <package-name>make Command
bingo make <type> <name> [options]
Options:
-d, --directory string Specify generation directory
-p, --package string Specify package name
-t, --table string Read fields from database tableSupported types:
crud- Complete CRUD codemodel- Data modelstore- Storage layerbiz- Business logic layercontroller- Controller layerrequest- Request validationmiddleware- Middlewarejob- Scheduled taskmigration- Database migrationseeder- Data seedingservice- Service modulecmd- CLI command
gen Command
bingo gen -t <table1,table2,...>Development Workflow
Recommended development workflow:
1. Create project
↓
bingo create github.com/myorg/app
2. Configure database
↓
Edit .bingo.yaml
3. Generate business modules
↓
bingo make crud user
bingo make crud post
4. Customize business logic
↓
Edit Biz layer code
5. Add middleware/tasks
↓
bingo make middleware auth
bingo make job cleanup
6. Run and test
↓
make build && ./_output/platforms/<os>/<arch>/app-apiserverBest Practices
1. Use CRUD Generator for Quick Start
For standard CRUD operations, use make crud directly:
bingo make crud product2. Generate from Database to Reduce Manual Work
If you already have database design, use gen command:
bingo gen -t products,categories,orders3. Test Immediately After Generation
Verify generated code works by running service:
bingo make crud user
make build
./_output/platforms/<os>/<arch>/app-apiserver
curl http://localhost:8080/v1/users4. Customize Generated Code
Generated code is a starting point. Customize based on actual needs:
- Add business rules in Biz layer
- Add parameter validation in Controller layer
- Optimize queries in Store layer
5. Version Control Configuration Files
Commit .bingo.yaml to version control:
git add .bingo.yaml
git commit -m "feat: add bingo config"FAQ
Q: Will generated code overwrite my changes?
A: bingo doesn't overwrite existing files by default. If a file exists, it prompts whether to overwrite.
Q: How do I customize generation templates?
A: bingo uses built-in templates. To customize, fork the bingoctl repository and modify templates.
Q: Can generated code be used in production directly?
A: Generated code follows Bingo best practices but still needs adjustment based on actual business:
- Add business validation
- Optimize query performance
- Add error handling
- Write unit tests
Q: What's the difference between Bingo CLI and bingoctl in the project?
A: They are two different tools:
- bingo (CLI tool): Independent project scaffold and code generation tool
- cmd/bingoctl (project component): Built-in CLI tool in Bingo projects, can be extended with custom commands
Next Step
- Overall Architecture - Deep dive into microservices architecture design