Diff.java

package org.docascode.cli;

import org.docascode.api.DiffCommand;
import org.docascode.api.DocAsCode;
import org.docascode.api.core.errors.DocAsCodeException;
import picocli.CommandLine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@CommandLine.Command(name = "diff",
    description = "Compute and display a comparison of files between either Git revision or Maven versions.")
public class Diff extends Command implements Runnable {

    @CommandLine.Option(names = {"--files","-f"}, arity="1..*", description = "The files to compare")
    private List<File> files = new ArrayList<>();

    @CommandLine.Option(names = {"--base","-b"}, arity="1", description = "The base revision or version.")
    private String base;

    @CommandLine.Option(names = {"--revised","-r"}, arity="1", description = "the new revision or version. Default to current workspace version.")
    private String revised = null;

    @Override
    public void run() {
        try (DocAsCode docascode = DocAsCode.open()){
            DiffCommand diff = ((DiffCommand )docascode
                    .diff()
                    .addListener(this))
                    .setOldRevision(base)
                    .setNewRevision(revised);
            for (File f : this.files) {
                File toFile = new File(
                        docascode.getRepository().getTmpDir(),
                        docascode.getRepository().relativize(f));
                diff.setFile(f)
                        .toFile(toFile)
                        .openAtEnd(true)
                        .call();
            }
        } catch (DocAsCodeException e) {
            error(e);
        }
    }
}