How to create and apply DIFF programmatically?

The task is as follows: obtain a diff between the snapshot recorded on disk (an XML file) and the current database, and then store this diff to XML file
everything should work from @Configuration SpringBoot application:

 @Bean
 @Order(4)
 public CommandLineRunner initEverithing(ResourceLoader resourceLoader) { ... and so on

not from CLI but from SpringBoot application.

To perform diff with a file (“offline” in Liquibase), you need to provide a snapshot of the database, not the changelog. You create this with the snapshot command, formatted as JSON.

Is there only way the CLI? Is there any programmatic API?

You just take a snapshot in JSON format, and then when you run diff you specify that as the referenceURL argument, like offline:postgresql?snapshot=mysnapshot.json. The URL argument is for the database.

You can take snapshot by using the Liquibase API.

How to run diff and take snapshot using the Liquibase API? ChatGPT don’t know how

This method should work. You would need to write the contents of the OutputStream to disk as a temp file, and then use that in your offline URL that you pass to the diff command.

public static DatabaseSnapshot takeSnapshot(String url, Database database, String schemas, OutputStream stream) throws DatabaseException {
    DatabaseSnapshot snapshot = null;
    try {
        CommandScope snapshotCommand = new CommandScope("snapshot");
        snapshotCommand.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, database);
        snapshotCommand.addArgumentValue(SnapshotCommandStep.SCHEMAS_ARG, schemas);
        snapshotCommand.setOutput(stream);
        CommandResults results = snapshotCommand.execute();
        snapshot = (DatabaseSnapshot) results.getResult("snapshot");
    } catch (Exception e) {
        String message = "";
        if (database.getConnection() instanceof OfflineConnection) {
            message =
                "The snapshot specified in the URL property cannot be accessed. Database-scoped checks cannot be completed.\n" +
                "Please check all database properties and credentials and try again.";
        } else {
            message =
                "The database target specified in the URL property cannot be accessed. Database-scoped checks cannot be completed.\n" +
                "Please check all database properties and credentials and try again.";
        }
        throw new DatabaseException(message, e);
    }
    return snapshot;
}