3.3. Developing Crash commands

In this tutorial, you write a simple Groovy command, deploy it into Crash add-on, and test the command in the shell. This helps you start developing commands quickly and directly against Crash add-on, then you should learn necessary techniques by Crash references:

The command is to add a user. Let's start:

  1. Write user.groovy:

    import org.crsh.cli.*;
    @Usage("user management")
    class user {
      private static final String DEFAULT_DOMAIN = "example.com";
      private static final String DEFAULT_PASSWORD = "exo";
      private rootContainer;
      private portalContainer;
      private orgService;
      private userHandler;
      user() {
        rootContainer = org.exoplatform.container.RootContainer.instance;
        portalContainer = rootContainer.getPortalContainer("portal");
        orgService = portalContainer.getComponentInstanceOfType(org.exoplatform.services.organization.OrganizationService.class);
        userHandler = orgService.userHandler;
      @Usage("add new user") @Command public void add(
                                          @Usage("username") @Required @Argument String name,
                                          @Usage("first name") @Option(names=["f", "firstname"]) String first,
                                          @Usage("last name") @Option(names=["l", "lastname"]) String last,
                                          @Usage("mail") @Option(names=["m", "mail"]) String mail,
                                          @Usage("password") @Option(names=["p", "password"]) String password
                                          ) {
        def user = userHandler.createUserInstance(name);
        user.password = (password != null) ? password : DEFAULT_PASSWORD;
        user.setFirstName((first != null) ? first : name);
        user.setLastName((last != null) ? last : name);
        user.setEmail((mail != null) ? mail : name + "@" + DEFAULT_DOMAIN);
        userHandler.createUser(user, true);
  2. Add user.groovy into the crash.war!/WEB-INF/crash/commands. You can also create a sub folder of this to deploy your command.


    The groovy can be hot-deployed, so you do not need to restart server.

  3. Test your command with:

    • % user add user1 - this should create a user with name user1 and password exo.

    • Or, with more options: % user add -f Feo -l Leo -m fleo@gmail.com -p exo fleo.

Feel free to add more codes to validate the input, add more sub-commands or implement an injector to add a bunch of users that can be good for benchmarking.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus