Create a plugin to register a command
You can use a plugin to register a command for use with the Content Hub CLI. In this example, you'll create a command called echo and then create a plugin to register it. The echo command outputs the string values that are passed in as arguments.
This walkthrough describes how to:
The following examples use the .NET CLI, but you can achieve the same results in your preferred integrated development environment (IDE).
Create a project
To create a project:
-
To create a class library, run the following commands:
-
To add a reference to the core CLI functionality, run the following command :
Sitecore.CH.Cli.Core provides access to the plugin infrastructure, useful utilities around file system access, configuration management, and console output. It also adds a dependency to the Web Client SDK.
Create the plugin
While starting up the application, the CLI scans the plugin search paths for any types that inherit from Sitecore.CH.Cli.Core.Abstractions.Infrastructure.IPlugin.
To create the plugin:
-
Rename the file
Class1.cstoPlugin.csand add the following code
This class contains the following methods:
-
ConfigureServices- lets you register custom services in the shared service collection for dependency injection. Command handlers can resolve these services in their constructor. See Dependency injection - .NET for more information. -
RegisterCommands- registers new commands that are being delivered as part of this plugin, and makes them available through the CLI.
Create and register a command
In this example, you create a command called echo. There are two methods you can use to do this:
-
Use a separate command declaration and command handling. This is the preferred approach as it allows for better and easier unit testing of the command handler implementation.
-
Specify options and command handling in the same class
To create and register a command:
The value echo that is passed to the base constructor is the name under which the command will be made available later.
Arguments are passed into the function. IHost contains a reference to the service provider that resolves previously registered services. The value echo that is passed to the base constructor is the name under which the command will be made available later.
New commands can be added to existing groups, or to new groups created specifically for them. Several command groups are available as standard, including:
-
delivery -
serialization -
tenant
-
Create a file called
EchoCommand.csand add a separate command declaration and command handling as shown in the following code: -
Or, use a separate class for arguments and options, as shown in the following code:
-
Specify options and command handling in the same class, as shown in the following code:
-
To connect the plugin class and the command, modify the
Plugin.csfile updating theRegisterCommandsmethod as follows:
After they are compiled and copied to a discoverable plugin search path, the walkthrough command group and echo command are included in the CLI. You can now run several commands to verify that everything has been set up correctly.
The following command starts from the walkthrough command group and verifies that the new echo command has been added:
You can run the echo command as follows: