Sword CLI
The Sword CLI creates projects and generates code inside them. It prepares the project layout, the module layers, controllers for each transport, providers and components.
Installation
The CLI is not published on crates.io yet. Install it from the repository:
cargo install --git https://github.com/sword-web/sword sword-cliThe binary is installed under the name sword.
Basic workflow
Create a web project, enter the directory and run it:
sword new my-app
cd my-app
cargo runThe command asks for the application type and the layout. To add a full module after creating an application:
sword generate module usersThe module generator asks for the controller kind and, for gRPC, for the proto path.
Create a project
The new command takes the directory to create. The syntax is:
sword new <name> [options]The application types are web, socketio, grpc, events and jobs. A project can start with a flat layout or a full module. The flat layout places a controller in the entry point. The module layout creates a directory with the module files and registers it.
sword new my-app --kind grpc
sword new my-app --module users
sword new my-app --module users --controller grpcOptions
| Option | Description |
|---|---|
--kind <kind> | Application type: web, socketio, grpc, events or jobs. |
--flat | Start with a single controller in the entry point. |
--module <name> | Start with a full module under the source directory. |
--controller <kind> | Controller kind of the initial module. Requires the module option. |
--no-git | Do not initialize a git repository. |
--yes | Accept the defaults and skip the prompts. |
Generate code
The generate command adds code to an existing project:
sword generate module <name> [options]
sword generate controller <module> <name> [options]
sword generate provider <module> <name>
sword generate component <module> <name>The module generator creates the entity, repository, service and controller, plus the module error in web, Socket.IO and gRPC projects. It registers the module in the entry point.
The controller generator adds a controller to an existing module and registers it. It supports the five controller kinds.
The provider generator adds a provider with its configuration. Providers live in the shared module, so the common case is to add them there.
The component generator adds an injectable component.
Module options
| Option | Description |
|---|---|
--controller <kind> | Controller kind of the module. |
--proto <path> | Proto path for a gRPC controller. |
--yes | Accept the defaults and skip the prompts. |
Controller options
| Option | Description |
|---|---|
--kind <kind> | Controller kind. |
--proto <path> | Proto path for a gRPC controller. |
--yes | Accept the defaults and skip the prompts. |
gRPC controllers
A gRPC controller creates a proto service with CRUD methods. The command writes the proto file, adds the controller that implements the service, and updates the build script to compile every proto in the proto directory. If the project already has a build script that only compiles the example proto, the command replaces it with the directory-based version.
sword generate controller users admin --kind grpc --proto config/proto/admin.proto
