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.



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

def contents
  @contents
end

#repoObject (readonly, protected)

Returns the value of attribute repo.



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

def repo
  @repo
end

#vendorsObject (readonly, protected)

Returns the value of attribute vendors.



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

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

  if 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
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)



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

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, 0644) do |f|
    f.write({
      vendors: 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