package com.github.swissquote.carnotzet.core.docker.registry;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * For json deserialization
 */
@Slf4j
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class DockerConfig {

	private Map<String, Auth> auths = new HashMap<>();

	public String getAuthFor(String registry) {
		return auths.get(registry).getAuth();
	}

	public static DockerConfig fromEnv() {
		Path configJson = Paths.get(System.getProperty("user.home")).resolve(".docker/config.json");

		if (!configJson.toFile().exists()) {
			log.warn("No registry auth found in [{}]", configJson);
			return new DockerConfig();
		}

		ObjectMapper objectMapper = new ObjectMapper();
		try {
			return objectMapper.readValue(Files.readAllBytes(configJson), DockerConfig.class);
		}
		catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}

}

@@ -649,34 +649,166 @@
 %7B%0A%09%09
-return auths.get(registry)
+Auth auth = auths.get(registry);%0A%09%09if (auth == null) %7B%0A%09%09%09auth = auths.get(%22https://%22 + registry);%0A%09%09%7D%0A%09%09if (auth == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09return auth
 .get

add support for explicit https protocol in private registry auth section of ~/.docker/config.json