sphere-cli
Sphere CLI (sphere-cli) is a command-line tool designed to streamline the development of Sphere projects. It helps you create new projects, generate service code, manage Protobuf definitions, and perform other common development tasks.
Installation
To install sphere-cli, ensure you have Go installed and run the following command:
go install github.com/go-sphere/sphere-cli@latestUsage
The general syntax for sphere-cli is:
sphere-cli [command] [flags]For detailed information on any command, you can use the --help flag:
sphere-cli [command] --helpCommands
Here is an overview of the available commands.
create
Initializes a new Sphere project with a default template.
Usage:
sphere-cli create --name <project-name> [--module <go-module-name>] [--layout <template-uri>]Flags:
--name string: (Required) The name for the new Sphere project.--module string: (Optional) The Go module path for the project.--layout string: (Optional) Custom template layout URI.
Example:
sphere-cli create --name myproject --module github.com/myorg/myprojectThis command creates a new project directory with the sphere-layout template, including:
- Makefile for build automation
- buf configuration for protobuf management
- Standard directory structure
- Example configurations
service
Generates service code, including both Protobuf definitions and Go service implementations.
This command has two subcommands: proto and golang.
service proto
Generates a .proto file for a new service.
Usage:
sphere-cli service proto --name <service-name> [--package <package-name>]Flags:
--name string: (Required) The name of the service.--package string: The package name for the generated.protofile (default:dash.v1).
Example:
sphere-cli service proto --name UserService --package api.v1service golang
Generates the Go implementation for a service from its definition.
Usage:
sphere-cli service golang --name <service-name> [--package <package-name>] [--mod <go-module-path>]Flags:
--name string: (Required) The name of the service.--package string: The package name for the generated Go code (default:dash.v1).--mod string: The Go module path for the generated code (default:github.com/go-sphere/sphere-layout).
Example:
sphere-cli service golang --name UserService --package api.v1 --mod github.com/myorg/myprojectrename
Renames the Go module path across the entire repository. This is useful when you need to change the module path after creating a project.
Usage:
sphere-cli rename --old <old-module-path> --new <new-module-path>Common Workflows
- Quick project setup and generation: ../../getting-started/quickstart
- Day-to-day development loop: ../../getting-started/workflow
Best Practices
- Use consistent naming: Follow Go naming conventions for services and packages
- Organize proto files: Keep related messages and services in appropriate packages
- Version your APIs: Use versioned packages (e.g.,
api.v1,api.v2) for backward compatibility - Document your protos: Add comments to your proto files for better generated documentation
- Run generation regularly: Use
make gen/allfrequently to keep generated code up to date
Common Issues and Solutions
Module Path Mismatches
If you need to change the module path after project creation:
sphere-cli rename --old github.com/old/path --new github.com/new/pathMissing Dependencies
If you encounter import errors, ensure all dependencies are properly installed:
make install # Install required tools
make init # Initialize dependenciesStale Generated Code
If generated code seems out of sync:
make clean # Clean generated files
make gen/all # Regenerate everything