package com.atlassian.bitbucket.mesh.git.builder.checkrefformat;

import com.atlassian.bitbucket.mesh.git.GitProcessCompletionHandler;
import com.atlassian.bitbucket.mesh.process.ProcessCompletionHandler;
import com.atlassian.bitbucket.mesh.process.ProcessStdioHandler;
import com.atlassian.bitbucket.mesh.process.ProcessSummary;
import com.atlassian.bitbucket.mesh.process.handler.NullStdioHandler;

import javax.annotation.Nonnull;

/**
 * Provides a {@link ProcessCompletionHandler} and {@link ProcessStdioHandler} combination that can be used to build a
 * {@code git check-ref-format} command that will return a {@code boolean} indicating whether a given ref name is valid.
 *
 * @see GitCheckRefFormat#ref(String)
 * @see GitCheckRefFormatBuilder
 */
public class CheckRefFormatProcessHandler extends NullStdioHandler<Boolean> implements ProcessCompletionHandler {

    private boolean zeroExit = true;

    @Override
    public void onComplete(@Nonnull ProcessSummary summary) {
        if (summary.getExitCode() != 0) {
            zeroExit = false;
        } else {
            new GitProcessCompletionHandler().onComplete(summary);
        }
    }

    @Override
    public Boolean getResult() {
        return zeroExit;
    }
}
