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

import com.atlassian.bitbucket.mesh.git.builder.AbstractGitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;

import javax.annotation.Nonnull;
import java.nio.file.Path;

import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;

public class DefaultGitUploadArchiveBuilder
        extends AbstractGitProcessBuilder<GitUploadArchiveBuilder>
        implements GitUploadArchiveBuilder {

    private Path directory;

    public DefaultGitUploadArchiveBuilder(GitProcessBuilder builder, Path directory) {
        super(builder.command("upload-archive"));

        //Default to the repository's directory, if one was set, so callers don't need to redundantly
        //pass it to directory(Path)
        this.directory = directory;
    }

    @Nonnull
    @Override
    public GitUploadArchiveBuilder directory(@Nonnull Path value) {
        directory = requireNonNull(value, "directory");

        return self();
    }

    @Override
    protected void applyArguments() {
        checkState(directory != null, "git upload-archive requires a path");

        builder.rawArgument(directory.toString());
    }

    @Nonnull
    @Override
    protected GitUploadArchiveBuilder self() {
        return this;
    }
}
