Class: OsCtl::Repo::Local::Index

Inherits:
Object
  • Object
show all
Includes:
Lib::Utils::File
Defined in:
lib/osctl/repo/local/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Index

Returns a new instance of Index.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/osctl/repo/local/index.rb', line 8

def initialize(repo)
  @repo = repo

  if exist?
    data = JSON.parse(File.read(path), symbolize_names: true)
    @vendors = data[:vendors]
    @contents = data[:images].map { |v| Base::Image.load(repo, v) }

  else
    @vendors = { default: nil }
    @contents = []
  end
end

Instance Attribute Details

#contentsObject (readonly, protected)

Returns the value of attribute contents.



85
86
87
# File 'lib/osctl/repo/local/index.rb', line 85

def contents
  @contents
end

#repoObject (readonly, protected)

Returns the value of attribute repo.



85
86
87
# File 'lib/osctl/repo/local/index.rb', line 85

def repo
  @repo
end

#vendorsObject (readonly, protected)

Returns the value of attribute vendors.



85
86
87
# File 'lib/osctl/repo/local/index.rb', line 85

def vendors
  @vendors
end

Instance Method Details

#add(image) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/osctl/repo/local/index.rb', line 26

def add(image)
  if (i = contents.index(image))
    contents[i] = image

  else
    contents << image
  end

  return unless image.tags.any?

  # Remove the image's tags from previous distribution images
  contents.each do |t|
    next if t == image \
            || t.vendor != image.vendor \
            || t.variant != image.variant \
            || t.arch != image.arch \
            || t.distribution != image.distribution \

    t.tags.delete_if { |tag| image.tags.include?(tag) }
  end
end

#delete(image) ⇒ Object



58
59
60
# File 'lib/osctl/repo/local/index.rb', line 58

def delete(image)
  contents.delete(image)
end

#exist?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/osctl/repo/local/index.rb', line 22

def exist?
  File.exist?(path)
end

#find(vendor, variant, arch, distribution, version) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/osctl/repo/local/index.rb', line 48

def find(vendor, variant, arch, distribution, version)
  contents.detect do |t|
    t.vendor == vendor \
      && t.variant == variant \
      && t.arch == arch \
      && t.distribution == distribution \
      && (t.version == version || t.tags.include?(version))
  end
end

#imagesObject



79
80
81
# File 'lib/osctl/repo/local/index.rb', line 79

def images
  contents.clone
end

#pathObject (protected)



87
88
89
# File 'lib/osctl/repo/local/index.rb', line 87

def path
  File.join(repo.path, 'INDEX.json')
end

#saveObject



70
71
72
73
74
75
76
77
# File 'lib/osctl/repo/local/index.rb', line 70

def save
  regenerate_file(path, 0o644) do |f|
    f.write({
      vendors:,
      images: contents.sort.map(&:dump)
    }.to_json)
  end
end

#set_default_variant(vendor, name) ⇒ Object



66
67
68
# File 'lib/osctl/repo/local/index.rb', line 66

def set_default_variant(vendor, name)
  vendors[vendor.to_sym] = name
end

#set_default_vendor(name) ⇒ Object



62
63
64
# File 'lib/osctl/repo/local/index.rb', line 62

def set_default_vendor(name)
  vendors[:default] = name
end