Today’s codebases are big. Developers might be responsible for pieces of code in a handful of applications—or they might be responsible for many microservices that make up one app. Either way, it’s likely they are working in multiple repos, which can require coordination across different codebases and apps.
Do you know how coding issues proliferate across your repositories? Do you understand the impact of the API changes you’re going to make? Do you avoid code updates or security patches because they are so time consuming and tedious?
The Moderne Platform was created to automate code maintenance and security at scale. We’re now excited to offer the Moderne CLI, which puts the power of multi-repo code insights and auto-remediation into developer hands to improve productivity.
With Moderne CLI, developers are able to work efficiently across many repositories to automate code updates, migrations, security, and quality—enabling more time for delivering business value.
What is the Moderne CLI?
The Moderne CLI (command line interface) enables developers to work locally to examine and auto-remediate code on multiple repositories. The number of repos in play is only limited by the computing power of a developer’s own machine. Essentially, the Moderne CLI was built to amplify the refactoring automation capacity and control developers can have at their fingertips.
When you use the Moderne CLI with the Moderne Platform, organizations can accelerate developer work plus align with other teams and business units to operate on the codebase at unprecedented scale.
Through the Moderne CLI, developers have local access to the robust Moderne code search and transformation capabilities such as:
- Build Lossless Semantic Tree (LST) artifacts (mod build)
- Run recipes—now from your local machine (mod run)
- Apply changes from a recipe run (mod apply)
- Commit the changes to the repos (mod commit)
- Extract data tables to examine your code (mod study)
You’ll notice that the Moderne CLI employs ‘Git-like’ commands to assist with working efficiently across multiple repositories. Review the Moderne CLI documentation to learn how to set up the CLI and get to work.
Walking through the Moderne CLI workflow
Developers can have any number of automated coding tasks on their ‘to do’ list on any given day. For example, they might want to coordinate API changes across multiple repositories or manage security alerts quickly. They might also want to run static code analysis and updates on their code regularly or they might need to update code to support a newer framework or library version.
All of these types of tasks can be automated on many repos at once with the Moderne CLI.
Developers no longer need to change into individual repo directories before working on their code. The Moderne CLI operates on a directory path, which can be an individual repository or a path to a number of repositories on disk. The directory is traversed recursively, looking for Git repositories, and the mod command is applied to each repository before moving to the next. Any nested directory structure can be supported.
Let’s walk through a general workflow for how you would use Moderne CLI:
- Clone repositories and build LSTs.
- Run recipes, review changes, and commit changes.
- Analyze code via a data table after a recipe run.
Clone repositories and get them ready for auto-remediation
Moderne auto-remediation works on an LST artifact, which is a full-fidelity representation of the code in your repos. The LST retains type attribution, metadata, and white space in your code, which enables it to be searched efficiently and precisely—and then very accurately transformed.
Building or downloading LSTs to your environment is the first key step. You can do this by cloning the repositories you want to work on and then either building the LSTs locally as in Figure 1 or downloading them from the Moderne Platform (if they’re available).
The Moderne CLI is very efficient in how it builds and operates on LSTs in a local environment. It builds an LST in parts and then serializes and writes it to the disk (as part of the mod build command). Likewise, the mod run command can read an LST from the disk in pieces as it runs recipes. This provides the ability to run many different recipes across multiple repositories and on larger repos all on a local machine.
Note that through a Moderne Platform connection, LSTs can download automatically if you’ve set up a pipeline to publish LST artifacts to your artifact repository and connected it to the SaaS.
Run a recipe and review changes to multiple repositories
Once you have cloned your repositories and readied the LSTs, you can get to work running search and transformation recipes against one or more repos, viewing results, and committing changes.
Recipes are rules-based programs that specify the operations that can be applied to the LST. They are maintained and curated by the open-source OpenRewrite community. Note that the recipes you want to use must be installed locally first, pulling from the source identified in mod config. See the documentation for more information about this setup.
Running recipes (shown in Figure 2) involves selecting your target repositories and identifying the recipe you want to run as in this example:
mod run <PATH> --recipe <RECIPE_NAME>
This will generate patch files that contain the changes the recipe will make, which you can examine with your favorite viewer. If it all looks good, you can apply changes with mod apply or commit them all with mod commit.
Clearly, the bonus with Moderne CLI is that many lines of code across repos can be updated automatically all at the same time. No longer are you trekking through code to find the issues and then fixing them one by one.
Extract a data table to understand your code better
In addition to modifying code directly, recipes can collect rich tabular data pulled from your code that enables you to extract some specific attributes or inferences about your codebase and perform impact analysis.
For example, a recipe called Find Spring API endpoints will produce a data table of all HTTP API endpoints exposed by Spring applications, including data such as Repository Origin, Repository Path, Repository Branch, SCM Type, Repository Link, Source Path, Method Name, Method, and Path. You can use this data to understand the impact of your changes, prioritize changes, and define automated updates.
Dig into data tables using this command construct:
mod study /path/to/project --last-recipe-run --data-table <DATA-TABLE-NAME>
Once you build the data table, you can open the file as in Figure 3.
An example of report data (such as the Spring API endpoints report) is shown in Figure 4.
Recipe authorship and testing support with Moderne IDE plugin
The Moderne CLI can also work with the Moderne IDE integration, which can be an accelerant for authoring and testing OpenRewrite recipes in particular. You just have to pick up the Moderne plugin from the IDE marketplace.
When working in an IDE, with the Moderne plugin installed, you’ll be able to write a recipe and get fast feedback on its function and performance by running it against your actual code in the Moderne CLI.
In your IDE, you can set the recipe you're developing as an active recipe through the plugin, which makes it available to run immediately in the Moderne CLI. Basically, in the IDE, you:
- Open a file containing a recipe.
- Right click on the class name.
- Select Set Active Recipe (as shown in Figure 5).
Once you run the recipe in the Moderne CLI, you can see if the results are what you expect. If not, jump back to the recipe code for fine tuning.
Getting started with the Moderne CLI
With the Moderne CLI, you can transform your approach to API management, code upgrades, and application security—accelerating your time to value like never before.
To get started, download the Moderne CLI from the Moderne Platform and use it on your own code for free. You must simply authenticate the Moderne CLI with the Moderne Platform. Learn more about set-up and configuration in the documentation.
Contact Moderne for more information about how to get started with the Moderne Platform.