package com.atlassian.bitbucket.mesh.git.exception;

import com.atlassian.bitbucket.mesh.rpc.v1.RpcFailedCommand;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcUsage;
import com.google.protobuf.ByteString;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class UsageException extends AbstractValidationException {

    private final String commandLine;
    private final String usage;

    public UsageException(@Nonnull String commandLine, @Nonnull String usage) {
        super("[" + commandLine + "] is not a valid command");

        this.commandLine = requireNonNull(commandLine, "commandLine");
        this.usage = requireNonNull(usage, "usage");
    }

    @Nonnull
    public String getCommandLine() {
        return commandLine;
    }

    @Nonnull
    public String getUsage() {
        return usage;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setFailedCommand(RpcFailedCommand.newBuilder()
                        .setCommandLine(ByteString.copyFromUtf8(commandLine))
                        .setExitCode(-1)
                        .setStderr(usage))
                .setUsage(RpcUsage.getDefaultInstance())
                .build();
    }
}
