Class: Bosh::Cli::VersionsIndex
Instance Method Summary
collapse
#major_version, #minor_version, #version_cmp, #version_greater, #version_less, #version_same
Constructor Details
#initialize(storage_dir, name_prefix = nil) ⇒ VersionsIndex
Returns a new instance of VersionsIndex.
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/cli/versions_index.rb', line 7
def initialize(storage_dir, name_prefix = nil)
@storage_dir = File.expand_path(storage_dir)
@index_file = File.join(@storage_dir, "index.yml")
@name_prefix = name_prefix
if File.file?(@index_file)
init_index(load_yaml_file(@index_file, nil))
else
init_index({})
end
end
|
Instance Method Details
#[](fingerprint) ⇒ Object
26
27
28
|
# File 'lib/cli/versions_index.rb', line 26
def [](fingerprint)
@data["builds"][fingerprint]
end
|
#add_version(fingerprint, item, payload = nil) ⇒ Object
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
|
# File 'lib/cli/versions_index.rb', line 57
def add_version(fingerprint, item, payload = nil)
version = item["version"]
if version.blank?
raise InvalidIndex,
"Cannot save index entry without knowing its version"
end
create_directories
if payload
File.open(filename(version), "w") do |f|
f.write(payload)
end
end
@data["builds"].each_pair do |fp, build|
if version_cmp(build["version"], version) == 0 && fp != fingerprint
raise "Trying to add duplicate version `#{version}' " +
"into index `#{@index_file}'"
end
end
@data["builds"][fingerprint] = item
if payload
@data["builds"][fingerprint]["sha1"] = Digest::SHA1.hexdigest(payload)
end
File.open(@index_file, "w") do |f|
f.write(YAML.dump(@data))
end
File.expand_path(filename(version))
end
|
#filename(version) ⇒ Object
92
93
94
95
96
|
# File 'lib/cli/versions_index.rb', line 92
def filename(version)
name = @name_prefix.blank? ?
"#{version}.tgz" : "#{@name_prefix}-#{version}.tgz"
File.join(@storage_dir, name)
end
|
#find_by_checksum(checksum) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/cli/versions_index.rb', line 19
def find_by_checksum(checksum)
@data["builds"].each_pair do |fingerprint, build_data|
return build_data if build_data["sha1"] == checksum
end
nil
end
|
#latest_version(major = nil) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/cli/versions_index.rb', line 30
def latest_version(major = nil)
builds = @data["builds"].values
if major
builds = builds.select do |build|
major_version(build["version"]) == major
end
end
return nil if builds.empty?
sorted = builds.sort do |build1, build2|
cmp = version_cmp(build2["version"], build1["version"])
if cmp == 0
raise "There is a duplicate version `#{build1["version"]}' " +
"in index `#{@index_file}'"
end
cmp
end
sorted[0]["version"]
end
|
#version_exists?(version) ⇒ Boolean
53
54
55
|
# File 'lib/cli/versions_index.rb', line 53
def version_exists?(version)
File.exists?(filename(version))
end
|