Class: DockerDistribution::Reference

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/docker_distribution/reference.rb

Constant Summary collapse

MAX_NAME_TOTAL_LENGTH =
255

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository = nil, tag = nil, digest = nil) ⇒ Reference

Returns a new instance of Reference.



12
13
14
15
16
# File 'lib/docker_distribution/reference.rb', line 12

def initialize(repository = nil, tag = nil, digest = nil)
  @repository = repository
  @tag = tag
  @digest = digest
end

Instance Attribute Details

#digestObject

Returns the value of attribute digest.



10
11
12
# File 'lib/docker_distribution/reference.rb', line 10

def digest
  @digest
end

#repositoryObject

Returns the value of attribute repository.



10
11
12
# File 'lib/docker_distribution/reference.rb', line 10

def repository
  @repository
end

#tagObject

Returns the value of attribute tag.



10
11
12
# File 'lib/docker_distribution/reference.rb', line 10

def tag
  @tag
end

Class Method Details

.get_best_reference_type(ref) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/docker_distribution/reference.rb', line 145

def self.get_best_reference_type(ref)
  if Helpers.empty?(ref.name)
    return DigestReference.new(ref.digest) unless Helpers.empty?(ref.digest)

    return nil
  end

  if Helpers.empty?(ref.tag)
    return CanonicalReference.new(ref.repository, ref.digest) unless Helpers.empty?(ref.digest)

    return ref.repository
  end

  return TaggedReference.new(ref.repository, ref.tag) if Helpers.empty?(ref.digest)

  ref
end

.parse(s) ⇒ Object

Parse parses s and returns a syntactically valid Reference.

If an error was encountered it is returned, along with a nil Reference.
NOTE: Parse will not handle short digests.

Raises:



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
# File 'lib/docker_distribution/reference.rb', line 39

def self.parse(s)
  matches = Regexp.reference_regexp.match(s)
  if matches.nil?
    raise NameEmpty if Helpers.empty?(s)
    raise NameContainsUppercase unless Regexp.reference_regexp.match(s.downcase).nil?

    raise ReferenceInvalidFormat
  end

  match_results = matches.to_a
  raise NameTooLong if match_results[1] && match_results[1].length > MAX_NAME_TOTAL_LENGTH

  repo = Repository.new
  name_match = Regexp.anchored_name_regexp.match(match_results[1])
  name_match_results = name_match&.to_a

  if name_match_results && name_match_results.length == 3
    repo.domain = name_match_results[1]
    repo.path = name_match_results[2]
  else
    repo.domain = nil
    repo.path = name_match_results[1]
  end

  ref = Reference.new(repo, match_results[2])

  unless Helpers.empty?(match_results[3])
    digest = Digest.parse!(match_results[3])
    ref.digest = digest.digest
  end

  r = get_best_reference_type(ref)
  raise NameEmpty if r.nil?

  r
end

.parse_named(str) ⇒ Object

ParseNamed parses s and returns a syntactically valid reference implementing

the Named interface. The reference must have a name and be in the canonical
form, otherwise an error is returned.
If an error was encountered it is returned, along with a nil Reference.
NOTE: ParseNamed will not handle short digests.

Raises:



81
82
83
84
85
86
# File 'lib/docker_distribution/reference.rb', line 81

def self.parse_named(str)
  named = Normalize.parse_normalized_named(str)
  raise NameNotCanonical if named.to_s != str

  named
end

.split_domain(name) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/docker_distribution/reference.rb', line 163

def self.split_domain(name)
  match = Regexp.anchored_name_regexp.match(name)
  return [nil, name] if match && match.to_a.length != 3

  match_results = match.to_a
  [match_results[1], match_results[2]]
end

.split_hostname(named) ⇒ Object



171
172
173
174
175
# File 'lib/docker_distribution/reference.rb', line 171

def self.split_hostname(named)
  return [named.domain, named.path] if named.is_a?(Repository)

  split_domain(name)
end

.trim_named(ref) ⇒ Object

TrimNamed removes any tag or digest from the named reference.



140
141
142
143
# File 'lib/docker_distribution/reference.rb', line 140

def self.trim_named(ref)
  domain, path = split_hostname(ref)
  new Repository(domain, path)
end

.with_digest(named, digest) ⇒ Object

WithDigest combines the name from “name” and the digest from “digest” to form

a reference incorporating both the name and the digest.


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/docker_distribution/reference.rb', line 122

def self.with_digest(named, digest)
  match = Regexp.anchored_digest_regexp.match(digest)
  raise DigestInvalidFormat if match.nil?

  repo = Repository.new
  if named.is_a?(Repository)
    repo.domain = named.domain
    repo.path = named.path
  else
    repo.path = named.name
  end

  return Reference.new(repo, named.tag, digest) if named.is_a?(TaggedReference)

  CanonicalReference.new(repo, digest)
end

.with_name(name) ⇒ Object

WithName returns a named object representing the given string. If the input

is invalid ErrReferenceInvalidFormat will be returned.

Raises:



90
91
92
93
94
95
96
97
98
99
# File 'lib/docker_distribution/reference.rb', line 90

def self.with_name(name)
  raise NameEmpty if Helpers.empty?(name)
  raise NameTooLong if name.length > MAX_NAME_TOTAL_LENGTH

  match = Regexp.anchored_name_regexp.match(name)
  raise ReferenceInvalidFormat if match.nil? || match.to_a.length != 3

  match_result = match.to_a
  Repository.new(match_result[1], match_result[2])
end

.with_tag(named, tag) ⇒ Object

WithTag combines the name from “name” and the tag from “tag” to form a

reference incorporating both the name and the tag.

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/docker_distribution/reference.rb', line 103

def self.with_tag(named, tag)
  match = Regexp.anchored_tag_regexp.match(tag)
  raise TagInvalidFormat if match.nil?

  repo = Repository.new
  if named.is_a?(Repository)
    repo.domain = named.domain
    repo.path = named.path
  else
    repo.path = named.name
  end

  return Reference.new(repo, tag, named.digest) if named.is_a?(CanonicalReference)

  TaggedReference.new(repo, tag)
end

Instance Method Details

#familiarObject



32
33
34
# File 'lib/docker_distribution/reference.rb', line 32

def familiar
  self.class.new(Normalize.familiarize_name(self), tag, digest)
end

#to_hObject



22
23
24
25
26
27
28
29
30
# File 'lib/docker_distribution/reference.rb', line 22

def to_h
  {
    repository: name,
    domain: domain,
    path: path,
    tag: tag,
    digest: digest
  }
end

#to_sObject



18
19
20
# File 'lib/docker_distribution/reference.rb', line 18

def to_s
  [name, ":", tag, "@", digest].join("")
end