using System; using System.Globalization; using System.Runtime.InteropServices; namespace INetMock.Client.IntegrationTest { internal static class DockerEndpoint { private const string DockerHostEnvName = "DOCKER_HOST"; private const string DockerTlsVerifyEnvName = "DOCKER_TLS_VERIFY"; internal static string DetermineFromEnv() => DetermineEndpoint( Environment.GetEnvironmentVariable(DockerHostEnvName), Environment.GetEnvironmentVariable(DockerTlsVerifyEnvName) ); internal static string DetermineEndpoint(string? dockerHost, string? tlsVerify) { dockerHost ??= ""; var dockerTlsVerify = int.TryParse(tlsVerify, out var verify) && verify == 1; var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); return (dockerHost, dockerTlsVerify, isWindows) switch { ("", _, true) => "npipe://./pipe/docker_engine", ("", _, false) => "unix:/var/run/docker.sock", (_, false, _) => dockerHost, (var h, true, _) when h.StartsWith("tcp") => h.Replace( "tcp", "https", true, CultureInfo.InvariantCulture), _ => "", }; } } }