Skip to content

Build a CLI with Mateu

miguelperezcolom edited this page Aug 5, 2024 · 1 revision

You can use Mateu to build a web browser based CLI.

This means that when you run a command a browser will open and, at the end of the user flow, the process running the CLI will end.

The only thing you need in order to build your web browser based CLI with Mateu is:

  • open the browser when your application starts
  • finish the java application by calling exit in the last call
  • create a bash script or an alias to run your app
  • optionally, deploy it to Homebrew

Open the browser

You just need to add this class to your project:

package com.example.demomateualpha;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

import static io.mateu.core.infra.Utils.browse;

@Service
@Slf4j
public class ServerStartListener implements ApplicationListener<WebServerInitializedEvent> {

    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        // This is to exclude management port
        if (!"management".equals(event.getApplicationContext().getServerNamespace())) {
            browse("http://localhost:" + event.getWebServer().getPort() + "/");
        }
    }

}

Exit

In your last action method, just call exit:

package com.example.demomateualpha;


import io.mateu.core.domain.uidefinition.shared.annotations.MainAction;
import io.mateu.core.domain.uidefinition.shared.annotations.MateuUI;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;

import java.net.URL;

import static io.mateu.core.infra.Utils.exit;

@MateuUI("")
@Getter@Setter
public class HelloWorld {

    @NotBlank
    String name = "Mateu";

    @SneakyThrows
    @MainAction
    URL runAndClose() {
        exit(0); // <-- this will be executed in a new tread with a 100ms delay
        return new URL("https://www.google.es"); // <-- this can be the route to an html file in your laptop
    }

}

As the server will be stopped after calling the end method please notice it is a good practice to redirect the user to a report file or, at least, give a message saying something like You are done. You can now close this browser window.. We cannot close the browser window from javascript (unless we opened it from the client side) so this is the best way to handle this behavior.

Create a bash script to run your app

You will probably want to create a simple bash script (or windows shell script) to run your appp. It can be something like this:

java -jar target/demo-mateu-alpha-0.0.1-SNAPSHOT.jar

Remember you can create an alias to easily call it or an alias to your script, instead of providing the full path :). E.g.:

alias mycli='java -jar /an-absolute-path/demo-mateu-alpha-0.0.1-SNAPSHOT.jar'

Optionally, deploy it to Homebrew

You have very good documentation for doing that:

That will allow you to easily make your CLI available to anyone, managing also the java installation.

Conclusion

You do not need to write your CLI tools in bash or python. You just need to use Mateu to easily build a nice web browser based UI and provide a better user experience while using our loved and well known java for writing our logic.

Clone this wiki locally