-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathDockerImage.cs
95 lines (82 loc) · 3 KB
/
DockerImage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
namespace DotNet.Testcontainers.Images
{
using System;
using System.Linq;
using JetBrains.Annotations;
/// <inheritdoc cref="IImage" />
[PublicAPI]
public sealed class DockerImage : IImage
{
private static readonly Func<string, IImage> GetDockerImage = MatchImage.Match;
private readonly string _hubImageNamePrefix;
/// <summary>
/// Initializes a new instance of the <see cref="DockerImage" /> class.
/// </summary>
/// <param name="image">The image.</param>
public DockerImage(IImage image)
: this(image.Repository, image.Name, image.Tag)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DockerImage" /> class.
/// </summary>
/// <param name="image">The image.</param>
/// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
/// <example>"fedora/httpd:version1.0" where "fedora" is the repository, "httpd" the name and "version1.0" the tag.</example>
public DockerImage(string image)
: this(GetDockerImage(image))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DockerImage" /> class.
/// </summary>
/// <param name="repository">The repository.</param>
/// <param name="name">The name.</param>
/// <param name="tag">The tag.</param>
/// <param name="hubImageNamePrefix">The Docker Hub image name prefix.</param>
/// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
/// <example>"fedora/httpd:version1.0" where "fedora" is the repository, "httpd" the name and "version1.0" the tag.</example>
public DockerImage(
string repository,
string name,
string tag,
string hubImageNamePrefix = null)
{
_ = Guard.Argument(repository, nameof(repository))
.NotNull()
.NotUppercase();
_ = Guard.Argument(name, nameof(name))
.NotNull()
.NotEmpty()
.NotUppercase();
_hubImageNamePrefix = hubImageNamePrefix;
Repository = repository;
Name = name;
Tag = string.IsNullOrEmpty(tag) ? "latest" : tag;
}
/// <inheritdoc />
public string Repository { get; }
/// <inheritdoc />
public string Name { get; }
/// <inheritdoc />
public string Tag { get; }
/// <inheritdoc />
public string FullName
{
get
{
var imageComponents = new[] { _hubImageNamePrefix, Repository, Name }
.Where(imageComponent => !string.IsNullOrEmpty(imageComponent))
.Select(imageComponent => imageComponent.Trim('/', ':'))
.Where(imageComponent => !string.IsNullOrEmpty(imageComponent));
return string.Join("/", imageComponents) + ":" + Tag;
}
}
/// <inheritdoc />
public string GetHostname()
{
var firstSegmentOfRepository = (string.IsNullOrEmpty(_hubImageNamePrefix) ? Repository : _hubImageNamePrefix).Split('/')[0];
return firstSegmentOfRepository.IndexOfAny(new[] { '.', ':' }) >= 0 ? firstSegmentOfRepository : null;
}
}
}