Skip to content

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:

bash
cargo install --git https://github.com/sword-web/sword sword-cli

The binary is installed under the name sword.

Basic workflow ​

Create a web project, enter the directory and run it:

bash
sword new my-app
cd my-app
cargo run

The command asks for the application type and the layout. To add a full module after creating an application:

bash
sword generate module users

The 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:

text
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.

bash
sword new my-app --kind grpc
sword new my-app --module users
sword new my-app --module users --controller grpc

Options ​

OptionDescription
--kind <kind>Application type: web, socketio, grpc, events or jobs.
--flatStart 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-gitDo not initialize a git repository.
--yesAccept the defaults and skip the prompts.

Generate code ​

The generate command adds code to an existing project:

text
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 ​

OptionDescription
--controller <kind>Controller kind of the module.
--proto <path>Proto path for a gRPC controller.
--yesAccept the defaults and skip the prompts.

Controller options ​

OptionDescription
--kind <kind>Controller kind.
--proto <path>Proto path for a gRPC controller.
--yesAccept 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.

bash
sword generate controller users admin --kind grpc --proto config/proto/admin.proto