Detailed Guide to go-doudou CLI Commands
Photo by Christopher Gower on Unsplash
go-doudou is a powerful Go language microservice development framework that provides rich command-line tools to help developers quickly build, deploy, and manage microservices. This article will detail the usage of various commands and subcommands of the go-doudou CLI tool, and explain them in conjunction with actual examples.
Installation and Upgrade
Installing go-doudou
For Go versions below 1.17:
go get -v github.com/unionj-cloud/go-doudou/v2@v2.5.8
For Go versions >= 1.17, it's recommended to use the following command to install the go-doudou
command-line tool globally:
go install -v github.com/unionj-cloud/go-doudou/v2@v2.5.8
It's recommended to use the following command to download go-doudou as a project dependency:
go get -v -d github.com/unionj-cloud/go-doudou/v2@v2.5.8
Tip
If you encounter a 410 Gone error
, please execute the following command first, and then execute the above installation command:
export GOSUMDB=off
Upgrading go-doudou
Execute the go-doudou version
command to upgrade the globally installed go-doudou command-line tool:
go-doudou version
If a new version is detected, you will be prompted whether to upgrade. After selecting "Yes", the latest version will be automatically installed.
Command Overview
The basic usage of the go-doudou command-line tool is as follows:
go-doudou [flags]
go-doudou [command]
2
Main commands include:
svc
: Generate or update servicesinit
: Initialize new projecthttp
: Generate HTTP routes and handlersclient
: Generate HTTP client codetest
: Generate integration test code
grpc
: Generate gRPC service codecrud
: Generate generic CRUD code from databaserun
: Run the servicepush
: Build Docker image and pushdeploy
: Deploy the service to Kubernetesshutdown
: Shut down the deployed service
completion
: Generate autocompletion script for specified shellenum
: Generate functions for constants to implement IEnum interfacename
: Bulk add or update JSON tags of struct fieldsversion
: Show the version number of go-doudouwork
: Build modular application
Note
Although the ddl
command may still be visible in the help information, this command has been deprecated and is not recommended for use in new projects. Please use the svc crud
command instead.
You can view the help information by running go-doudou -h
:
go-doudou -h
Example output:
go-doudou works like a scaffolding tool but more than that.
it lets api providers design their apis and help them code less.
it generates openapi 3.0 spec json document for frontend developers or other api consumers to understand what apis there,
consumers can import it into postman to debug and test, or upload it into some code generators to download client sdk.
it provides some useful components and middleware for constructing microservice cluster like service register and discovering,
load balancing and so on. it just begins, more features will come out soon.
Usage:
go-doudou [flags]
go-doudou [command]
Available Commands:
completion Generate the autocompletion script for the specified shell
enum Generate functions for constants to implement IEnum interface
help Help about any command
name bulk add or update json tag of struct fields
svc generate or update service
version Print the version number of go-doudou
work Build modular application
Flags:
-h, --help help for go-doudou
-v, --version version for go-doudou
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Detailed Explanation of the completion Command
The completion
command is used to generate an autocompletion script for the specified shell to improve the efficiency of using the go-doudou command line.
Basic Usage
go-doudou completion [command]
Subcommands
bash
: Generate an autocompletion script for bashfish
: Generate an autocompletion script for fishpowershell
: Generate an autocompletion script for powershellzsh
: Generate an autocompletion script for zsh
Examples
Generate bash autocompletion script:
go-doudou completion bash > ~/.bash_completion
Generate zsh autocompletion script:
go-doudou completion zsh > ~/.zsh_completion
Detailed Explanation of the svc Command
The svc
command is the most commonly used command in the go-doudou command-line tool, used to generate or update service-related code. It contains multiple subcommands:
svc init
The svc init
command is used to initialize a new go-doudou microservice project.
Basic Usage
go-doudou svc init [dir] [flags]
where [dir]
is the name of the project directory to initialize.
Common Parameters
-m, --mod
: Module name--module
: Initialize as a component of a modular application (boolean value). When set totrue
, go-doudou will automatically callgo work use
to add the component to the workspace.-f, --file
: Path or download link to an OpenAPI 3.0 or Swagger 2.0 specification JSON file--case
: Naming convention for protobuf message fields and JSON tags, supports "lowerCamel" and "snake" (default "lowerCamel")-t, --type
: Specify project type, value can be "grpc" or "rest" (default "grpc")--grpc_gen_cmd
: Command for generating gRPC service and message code (default uses protoc command)
Examples
Basic initialization:
go-doudou svc init myservice -m github.com/myorg/myservice
Initialize with MySQL database and generate gRPC code:
go-doudou svc init myservice --db_driver mysql --db_dsn "root:password@tcp(localhost:3306)/mydb?charset=utf8mb4&parseTime=True&loc=Local" --db_soft deleted_at --db_grpc
Initialize as a component of a modular application:
go-doudou svc init component-c -m my-workspace/component-c --module
When using the --module
flag, go-doudou will automatically perform the following operations:
- Create the necessary project structure
- Automatically call
go work use
to add the newly created component to the workspace - Generate plugins and entry code for the modular application
- Update the main application's import statements to automatically include the new component's plugins
svc http
The svc http
command is used to generate HTTP routes and handlers.
Basic Usage
go-doudou svc http [flags]
Common Parameters
--handler
: Whether to generate default handler implementations (boolean value)-c, --client
: Whether to generate default Go HTTP client code (boolean value)-o, --omitempty
: Whether to addomitempty
to JSON tags in generated anonymous structs (boolean value)--case
: JSON tag naming convention applied to fields in anonymous structs in generated handlers, options are "lowerCamel" or "snake" (default "lowerCamel")--doc
: Whether to generate OpenAPI 3.0 JSON documentation (boolean value)-e, --env
: Base URL environment variable name-r, --routePattern
: Route pattern generation strategy, 0 means splitting each method of the service interface by slash / (after converting to snake_case), 1 means not splitting, just converting to lowercase--allowGetWithReqBody
: Whether to allow GET requests with request bodies (boolean value)
Examples
Generate HTTP routes and client code:
go-doudou svc http -c
Generate HTTP routes, handlers, and OpenAPI documentation:
go-doudou svc http --handler --doc
svc http client
svc http client
is a subcommand of svc http
, used to generate HTTP client code from an OpenAPI 3.0 specification JSON file.
Basic Usage
go-doudou svc http client [flags]
Common Parameters
-f, --file
: Path or download link to an OpenAPI 3.0 or Swagger 2.0 specification JSON file-e, --env
: Base URL environment variable name-p, --pkg
: Client package name (default "client")-o, --omit
: Whether to addomitempty
to JSON tags (boolean value)
Examples
Generate client code from an OpenAPI document:
go-doudou svc http client -f ./api-docs.json -e BASE_URL -p client
svc http test
svc http test
is a subcommand of svc http
, used to generate integration test code from a Postman Collection file.
Basic Usage
go-doudou svc http test [flags]
Common Parameters
--collection
: Path to a Postman Collection v2.1 compatible file--dotenv
: Path to a dotenv format configuration file for integration tests only
Examples
Generate test code from a Postman Collection:
go-doudou svc http test --collection ./postman_collection.json --dotenv ./.env.test
svc grpc
The svc grpc
command is used to generate gRPC service code.
Basic Usage
go-doudou svc grpc [flags]
Common Parameters
-o, --omitempty
: Whether to addomitempty
to JSON tags in generated anonymous structs (boolean value)--case
: protobuf message field naming strategy, supports "lowerCamel" and "snake" (default "lowerCamel")--grpc_gen_cmd
: Command for generating gRPC service and message code (default uses protoc command)--http2grpc
: Whether to generate RESTful API for gRPC service (boolean value)--allow_get_body
: Whether to allow GET requests with request bodies (boolean value)--annotated_only
: Whether to only generate gRPC API for methods with @grpc annotation (boolean value)
Examples
Generate basic gRPC service code:
go-doudou svc grpc
Generate gRPC service code and provide RESTful API proxy:
go-doudou svc grpc --http2grpc
Generate gRPC service code that only includes methods with @grpc annotation:
go-doudou svc grpc --annotated_only
svc crud
The svc crud
command is used to generate generic CRUD code from a database. This command is the recommended choice to replace the deprecated ddl
command.
Basic Usage
go-doudou svc crud [flags]
Common Parameters
--db_orm
: Specify ORM, currently only supports gorm (default "gorm")--db_driver
: Database driver type, options are "mysql", "postgres", "sqlite", "sqlserver", "tidb"--db_dsn
: Database connection URL--db_soft
: Database soft delete column name (default "deleted_at")--db_service
: Generate gRPC or REST service, accepts values: grpc or rest--db_gen_gen
: Whether to generate gen.go file (boolean value)--db_table_prefix
: Table prefix or PostgreSQL schema name--db_table_glob
: For filtering tables with glob matching--db_table_exclude_glob
: For excluding tables with glob matching--case
: protobuf message field and JSON tag naming convention, supports "lowerCamel" and "snake" (default "lowerCamel")--db_type_mapping
: Specify custom column type to Go type mappings--db_omitempty
: Whether to addomitempty
to JSON tags in generated model fields (boolean value)--grpc_gen_cmd
: Command for generating gRPC service and message code (default uses protoc command)
Examples
Generate CRUD code from a MySQL database:
go-doudou svc crud --db_driver mysql --db_dsn "root:password@tcp(localhost:3306)/mydb?charset=utf8mb4&parseTime=True&loc=Local" --db_soft deleted_at --db_service rest
Generate CRUD code from a PostgreSQL database, and specify schema:
go-doudou svc crud --db_driver postgres --db_dsn "host=localhost user=postgres password=postgres dbname=mydb port=5432 sslmode=disable" --db_table_prefix public --db_service grpc
Generate CRUD code for specific tables only:
go-doudou svc crud --db_driver mysql --db_dsn "root:password@tcp(localhost:3306)/mydb" --db_table_glob "user_*" --db_service rest
svc run
The svc run
command is used to run a go-doudou service.
Basic Usage
go-doudou svc run [flags]
Common Parameters
-w, --watch
: Enable watch mode, automatically restart the service when files change (boolean value)
Examples
Start the service:
go-doudou svc run
Start the service with watch mode enabled:
go-doudou svc run -w
svc push
The svc push
command is used to build Docker images and push them to an image repository, while generating or updating K8s deployment YAML files.
Basic Usage
go-doudou svc push [flags]
Common Parameters
-r, --repo
: Private Docker image repository--pre
: Image name prefix for building and pushing Docker images--ver
: Docker image version
Examples
Build an image and push it to a private repository:
go-doudou svc push -r myregistry.com/myuser
Build an image with a version tag:
go-doudou svc push -r myregistry.com/myuser --ver v1.0.0
svc deploy
The svc deploy
command wraps the kubectl apply command, used to deploy a service to a Kubernetes cluster.
Basic Usage
go-doudou svc deploy [flags]
Common Parameters
-k, --k8sfile
: Kubernetes YAML file for deploying the service
Examples
Deploy a service with default configuration:
go-doudou svc deploy
Deploy a service with a specified Kubernetes configuration file:
go-doudou svc deploy -k myservice_deployment.yaml
svc shutdown
The svc shutdown
command wraps the kubectl delete command, used to shut down a deployed service.
Basic Usage
go-doudou svc shutdown [flags]
Common Parameters
-k, --k8sfile
: Kubernetes YAML file for deploying the service
Examples
Shut down a service deployed with default configuration:
go-doudou svc shutdown
Shut down a service with a specified Kubernetes configuration file:
go-doudou svc shutdown -k myservice_deployment.yaml
Detailed Explanation of the name Command
The name
command is used to bulk add or update JSON tags for struct fields.
Basic Usage
go-doudou name [flags]
Common Parameters
-f, --file
: Go source file path-c, --case
: JSON tag naming convention, supports "lowerCamel", "snake", etc. (default "lowerCamel")-s, --strategy
: Naming strategy name, currently only supports "lowerCamel" and "snake" (default "lowerCamel")-o, --omitempty
: Whether to addomitempty
marker (boolean value)--form
: Whether to add form tags for github.com/go-playground/form
Examples
Add snake_case JSON tags to fields in the User struct:
go-doudou name -f ./model/user.go -c snake -o
Generate both JSON and form tags:
go-doudou name -f ./model/user.go -c lowerCamel -o --form
Detailed Explanation of the enum Command
The enum
command is used to generate functions implementing the IEnum
interface for constants. This is useful for using enum types in Go.
Basic Usage
go-doudou enum [flags]
Common Parameters
-f, --file
: Absolute path to a Go source file
Examples
Generate enum interface implementations for a file containing constant definitions:
go-doudou enum -f ./enum/status.go
Example of generated code (assuming status.go defines constants of type Status):
// Original file
type Status int
const (
StatusPending Status = iota
StatusActive
StatusInactive
)
// Generated functions
func (s *Status) StringSetter(value string) {
switch value {
case "StatusPending":
*s = StatusPending
case "StatusActive":
*s = StatusActive
case "StatusInactive":
*s = StatusInactive
default:
*s = StatusPending
}
}
func (s *Status) StringGetter() string {
switch *s {
case StatusPending:
return "StatusPending"
case StatusActive:
return "StatusActive"
case StatusInactive:
return "StatusInactive"
default:
return "StatusPending"
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Detailed Explanation of the version Command
The version
command is used to display the version number of go-doudou and check if a new version is available.
Basic Usage
go-doudou version
Examples
go-doudou version
Example output:
go-doudou version v2.5.8
If an updated version is detected, you will be prompted whether to upgrade:
A new version is available: v2.5.9
Do you want to upgrade? [Y/n]
2
Detailed Explanation of the work Command
The work
command is used to build modular applications, it creates a project structure with a workspace and a main entry module.
Basic Usage
go-doudou work [flags]
go-doudou work [command]
2
Subcommands
init
: Initialize a workspace folder
work init
The work init
command is used to initialize a workspace folder for developing modular applications.
Basic Usage
go-doudou work init [dir]
where [dir]
is the path to the workspace directory to initialize. If not specified, the current directory is used.
Workspace Structure
After executing the work init
command, go-doudou will create the following workspace structure:
workspace/ # Workspace root directory
├── go.work # Go workspace file, automatically includes main module and other components
└── main/ # Main entry module directory
├── go.mod # go.mod file for the main module
├── .env # Environment variable configuration file
└── cmd/ # Command directory
└── main.go # Main entry file, responsible for loading and running all components
2
3
4
5
6
7
When adding components using svc init --module
, go-doudou will automatically call the go work use
command to add the new component to the workspace, and automatically update the main/cmd/main.go
file to import the new component's plugins.
Examples
Initialize the current directory as a workspace:
go-doudou work init
Specify a directory as a workspace:
go-doudou work init ./my-workspace
Practical Application Examples
1. Microservice Initialization and Development Process
Here is a complete microservice development process:
# Step 1: Initialize the project
go-doudou svc init myservice -m github.com/myorg/myservice
# Step 2: Edit the svc.go file to define the service interface
# Define the service interface in myservice/svc.go
# Step 3: Generate HTTP and gRPC service code
cd myservice
go-doudou svc http -c --doc
go-doudou svc grpc
# Step 4: Implement business logic
# Edit the svcimpl.go file
# Step 5: Run the service
go-doudou svc run
# Step 6: Build the image and deploy
go-doudou svc push -r myregistry.com/myuser
go-doudou svc deploy
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2. Database Table-Based Microservice Generation
# Step 1: Initialize a project with database support
go-doudou svc init dbservice -m github.com/myorg/dbservice --db_driver mysql --db_dsn "root:password@tcp(localhost:3306)/mydb?charset=utf8mb4&parseTime=True&loc=Local" --db_soft deleted_at --db_grpc --db_rest
# Step 2: Run the service
cd dbservice
go-doudou svc run
2
3
4
5
6
3. Using the crud Command to Generate a CRUD Service from an Existing Database
# Step 1: Run the command in an existing project directory
cd myproject
go-doudou svc crud --db_driver postgres --db_dsn "host=localhost user=postgres password=postgres dbname=mydb" --db_service rest --db_soft deleted_at
# Step 2: Run the generated service
go-doudou svc run
2
3
4
5
6
4. Modular Application Development in a Workspace
# Step 1: Initialize the workspace
go-doudou work init my-workspace
cd my-workspace
# Step 2: Initialize modular components
# go-doudou will automatically execute "go work use" to add the component to the workspace
go-doudou svc init component-a -m my-workspace/component-a --module
go-doudou svc init component-b -m my-workspace/component-b --module
# Step 3: Define the service interface and generate code in each component
cd component-a
go-doudou svc http -c
go-doudou svc grpc
cd ../component-b
go-doudou svc http -c
go-doudou svc grpc
# Step 4: Start the main application - the main module will automatically import all components
cd ../main
go run cmd/main.go
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
How modular applications work:
- Each component generates a
plugin
package during initialization, used to register itself to the main application - The main application (
main
module) automatically imports all component plugins and initializes them at runtime - When calling
svc init --module
, go-doudou automatically executesgo work use
to add the new component to the workspace - At the same time, it updates the
main/cmd/main.go
file, adding import statements for the new component's plugins
5. Implementing Enum Types
# Step 1: Define enum constants
# Define the following in a status.go file
type Status int
const (
StatusPending Status = iota
StatusActive
StatusInactive
)
# Step 2: Generate enum interface implementation
go-doudou enum -f ./model/status.go
2
3
4
5
6
7
8
9
10
11
12
6. Generate Autocompletion Scripts
# Generate bash autocompletion script
go-doudou completion bash > ~/.bash_completion
# Generate zsh autocompletion script
go-doudou completion zsh > ~/.zsh_completion
2
3
4
5
Advanced Usage and Tips
1. Using Annotations to Control Interface Permissions
In the service interface, you can add annotations using special comments, such as:
// @role(ADMIN)
GetAdminData(ctx context.Context) (data string, err error)
2
Then check these annotations in middleware:
annotations := httpsrv.RouteAnnotationStore.GetParams(routeName, "@role")
if !sliceutils.StringContains(annotations, userRole) {
// Deny access
}
2
3
4
2. Custom protoc Command
For complex gRPC services, you can customize the protoc command:
go-doudou svc grpc --grpc_gen_cmd "protoc --proto_path=. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --validate_out=lang=go,paths=source_relative:. transport/grpc/myservice.proto"
3. Environment Variables Affecting Service Behavior
go-doudou supports various environment variables to configure service behavior:
GDD_SERVICE_NAME
: Service nameGDD_SERVICE_GROUP
: Service group nameGDD_SERVICE_VERSION
: Service versionGDD_WEIGHT
: Service instance weightGDD_REGISTER_HOST
: Service registration hostGDD_HTTP_PORT
: HTTP service portGDD_GRPC_PORT
: gRPC service portGDD_LOG_LEVEL
: Log level, optional values: "debug", "info", "warn", "error"GDD_PROMETHEUS
: Whether to enable Prometheus metrics collection
Example:
export GDD_SERVICE_NAME=myservice
export GDD_HTTP_PORT=8080
export GDD_LOG_LEVEL=debug
go-doudou svc run
2
3
4
4. Integration Testing Tips
Using the test code generated by the svc http test
command, you can easily implement integration tests:
# Step 1: Generate test code from a Postman Collection
go-doudou svc http test --collection ./collection.json --dotenv ./.env.test
# Step 2: Run the tests
go test -v ./test/...
2
3
4
5
5. Modular Application Development Tips
For large projects, you can easily manage modular applications using the work
command and the --module
flag:
# Initialize workspace
go-doudou work init my-workspace
cd my-workspace
# Add multiple modules - go-doudou will automatically call go work use
go-doudou svc init api-gateway -m my-workspace/api-gateway --module
go-doudou svc init user-service -m my-workspace/user-service --module
go-doudou svc init product-service -m my-workspace/product-service --module
# Start the application (main module automatically imports and initializes all components)
cd main
go run cmd/main.go
2
3
4
5
6
7
8
9
10
11
12
Advantages of modular applications:
- Clearer code organization, each component is maintained independently
- Can develop and test each component independently
- Shared dependencies are resolved through go.work, avoiding dependency conflicts
- The main application automatically integrates all components, no need to manually write integration code
- Suitable for the development and management of large microservice applications
Summary
The go-doudou command-line tool provides rich functionality to help developers quickly build, deploy, and manage microservices. Through the various commands and subcommands introduced in this article, you can easily complete the entire process from service initialization and code generation to deployment.
The biggest feature of go-doudou is simplifying the microservice development process, eliminating the need to write a lot of boilerplate code, allowing you to focus on implementing business logic. It supports the generation of RESTful API and gRPC services, as well as integration with databases, making it an ideal choice for building modern Go microservices.
In addition, go-doudou also provides powerful modular application development support. Through the work
command and the --module
flag, you can easily manage multi-module projects. go-doudou will automatically execute go work use
to add components to the workspace, and automatically import and initialize all components in the main application, greatly simplifying the development and maintenance work of modular applications.
Important Note
Please note that the ddl
command has been deprecated and is no longer recommended for use. If you need to generate code from a database or synchronize Go structs to a database, please use the svc crud
command instead.
I hope this article helps you understand and use the go-doudou CLI tool. For more details, please refer to the official documentation and example code repositories.