// Path: yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/client/ClientBuilderForConnector.java
// public static Credentials lookupSystemCredentials(String credentialsId) {
//     return firstOrNull(
//             lookupCredentials(
//                     Credentials.class,
//                     Jenkins.getInstance(),
//                     ACL.SYSTEM,
//                     emptyList()
//             ),
//             withId(credentialsId)
//     );
// }


import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.github.kostyasha.yad.NoStapler;
import com.github.kostyasha.yad.connector.YADockerConnector;
import com.github.kostyasha.yad.credentials.DockerRegistryAuthCredentials;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.DockerClient;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.command.PullImageCmd;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.exception.DockerClientException;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.exception.NotFoundException;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.model.Image;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.core.NameParser;
import com.github.kostyasha.yad_docker_java.org.apache.commons.lang.StringUtils;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.model.TaskListener;
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.github.kostyasha.yad.client.ClientBuilderForConnector.lookupSystemCredentials;
import static java.util.Collections.emptyList;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang.builder.ToStringStyle.SHORT_PREFIX_STYLE;
        if (nonNull(connector)) {
            try (DockerClient altClient = connector.getClient()) {
                if (nonNull(altClient)) {
                    LOG.debug("Using alternative client {} for {}", client, imageName);
                    execInternal(altClient, imageName, listener);
                    return;
                }
            }
        }

        execInternal(client, imageName, listener);
    }

    /**
     * Action around image with defined configuration
     */
    public void execInternal(@Nonnull final DockerClient client, @Nonnull final String imageName, TaskListener listener)
            throws IOException {
        PrintStream llog = listener.getLogger();

        if (shouldPullImage(client, imageName)) {
            LOG.info("Pulling image '{}'. This may take awhile...", imageName);
            llog.println(String.format("Pulling image '%s'. This may take awhile...", imageName));

            long startTime = System.currentTimeMillis();
            final PullImageCmd pullImageCmd = client.pullImageCmd(imageName);

//            final AuthConfig authConfig = pullImageCmd.getAuthConfig();
            for (DockerRegistryCredential cred : getRegistriesCreds()) {
                // hostname requirements?
// Path: yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/client/ClientBuilderForConnector.java
// public static Credentials lookupSystemCredentials(String credentialsId) {
//     return firstOrNull(
//             lookupCredentials(
//                     Credentials.class,
//                     Jenkins.getInstance(),
//                     ACL.SYSTEM,
//                     emptyList()
//             ),
//             withId(credentialsId)
//     );
// }


// Path: yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerPullImage.java
import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.github.kostyasha.yad.NoStapler;
import com.github.kostyasha.yad.connector.YADockerConnector;
import com.github.kostyasha.yad.credentials.DockerRegistryAuthCredentials;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.DockerClient;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.command.PullImageCmd;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.exception.DockerClientException;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.exception.NotFoundException;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.api.model.Image;
import com.github.kostyasha.yad_docker_java.com.github.dockerjava.core.NameParser;
import com.github.kostyasha.yad_docker_java.org.apache.commons.lang.StringUtils;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.model.TaskListener;
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.github.kostyasha.yad.client.ClientBuilderForConnector.lookupSystemCredentials;
import static java.util.Collections.emptyList;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.apache.commons.lang.builder.ToStringStyle.SHORT_PREFIX_STYLE;

        if (nonNull(connector)) {
            try (DockerClient altClient = connector.getClient()) {
                if (nonNull(altClient)) {
                    LOG.debug("Using alternative client {} for {}", client, imageName);
                    execInternal(altClient, imageName, listener);
                    return;
                }
            }
        }

        execInternal(client, imageName, listener);
    }

    /**
     * Action around image with defined configuration
     */
    public void execInternal(@Nonnull final DockerClient client, @Nonnull final String imageName, TaskListener listener)
            throws IOException {
        PrintStream llog = listener.getLogger();

        if (shouldPullImage(client, imageName)) {
            LOG.info("Pulling image '{}'. This may take awhile...", imageName);
            llog.println(String.format("Pulling image '%s'. This may take awhile...", imageName));

            long startTime = System.currentTimeMillis();
            final PullImageCmd pullImageCmd = client.pullImageCmd(imageName);

//            final AuthConfig authConfig = pullImageCmd.getAuthConfig();
            for (DockerRegistryCredential cred : getRegistriesCreds()) {
                // hostname requirements?
                Credentials credentials = lookupSystemCredentials(cred.getCredentialsId());