Class: Aptly::Repository

Inherits:
Representation show all
Defined in:
lib/aptly/repository.rb

Overview

Aptly repository representation.

Instance Attribute Summary

Attributes inherited from Representation

#connection

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Representation

#initialize

Constructor Details

This class inherits a constructor from Aptly::Representation

Class Method Details

.create(name, connection = Connection.new, **kwords) ⇒ Repository

Creates a new Aptly::Repository

Parameters:

  • name (String)

    name fo the repository

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:



119
120
121
122
123
124
125
# File 'lib/aptly/repository.rb', line 119

def create(name, connection = Connection.new, **kwords)
  options = kwords.merge(name: name)
  options = options.map { |k, v| [k.to_s.capitalize, v] }.to_h
  response = connection.send(:post, '/repos',
                             body: JSON.generate(options))
  new(connection, JSON.parse(response.body))
end

.exist?(name, connection = Connection.new, **kwords) ⇒ Boolean

Check if a repository exists.

Parameters:

  • name (String)

    the name of the repository which might exist

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:

  • (Boolean)

    whether or not the repository exists



131
132
133
134
135
136
# File 'lib/aptly/repository.rb', line 131

def exist?(name, connection = Connection.new, **kwords)
  get(name, connection, **kwords)
  true
rescue Aptly::Errors::NotFoundError
  false
end

.get(name, connection = Connection.new, **kwords) ⇒ Repository

Get a Aptly::Repository instance if the repository already exists.

Parameters:

  • name (String)

    name of the repository

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:



108
109
110
111
112
113
# File 'lib/aptly/repository.rb', line 108

def get(name, connection = Connection.new, **kwords)
  kwords = kwords.map { |k, v| [k.to_s.capitalize, v] }.to_h
  response = connection.send(:get, "/repos/#{name}",
                             query: kwords)
  new(connection, JSON.parse(response.body))
end

.list(connection = Connection.new, **kwords) ⇒ Array<Repository>

List all known repositories.

Parameters:

  • connection (Connection) (defaults to: Connection.new)

    connection to use for the instance

Returns:



141
142
143
144
# File 'lib/aptly/repository.rb', line 141

def list(connection = Connection.new, **kwords)
  response = connection.send(:get, '/repos', query: kwords)
  JSON.parse(response.body).collect { |r| new(connection, r) }
end

Instance Method Details

#add_file(path, **kwords) ⇒ Hash

Add a previously uploaded file to the Repository. FIXME: this should be called file

Returns:

  • (Hash)

    report data as specified in the API.



19
20
21
22
23
24
25
26
# File 'lib/aptly/repository.rb', line 19

def add_file(path, **kwords)
  response = connection.send(:post, "/repos/#{self.Name}/file/#{path}",
                             query: kwords)
  hash = JSON.parse(response.body)
  error = Errors::RepositoryFileError.from_hash(hash)
  fail error if error
  hash['Report']['Added']
end

#add_package(packages, **kwords) ⇒ Object Also known as: add_packages

Add a package (by key) to the repository.

Parameters:

  • packages (Array<String>, String)

    a list of package keys or a single package key to add to the repository. The package key(s) must already be in the aptly database.



52
53
54
55
56
57
# File 'lib/aptly/repository.rb', line 52

def add_package(packages, **kwords)
  connection.send(:post, "/repos/#{self.Name}/packages",
                  query: kwords,
                  body: JSON.generate(PackageRefs: [*packages]))
  self
end

#delete(**kwords) ⇒ Object

Delete this repository.



12
13
14
# File 'lib/aptly/repository.rb', line 12

def delete(**kwords)
  connection.send(:delete, "/repos/#{self.Name}", query: kwords)
end

#delete_package(packages, **kwords) ⇒ Object Also known as: delete_packages

Deletes a package (by key) from the repository.

Parameters:

  • packages (Array<String>, String)

    a list of package keys or a single package key to add to the repository. The package key(s) must already be in the aptly database.



64
65
66
67
68
69
# File 'lib/aptly/repository.rb', line 64

def delete_package(packages, **kwords)
  connection.send(:delete, "/repos/#{self.Name}/packages",
                  query: kwords,
                  body: JSON.generate(PackageRefs: [*packages]))
  self
end

#packages(**kwords) ⇒ Array<String>

List all packages in the repository

Returns:

  • (Array<String>)

    list of packages in the repository



41
42
43
44
45
46
# File 'lib/aptly/repository.rb', line 41

def packages(**kwords)
  response = connection.send(:get, "/repos/#{self.Name}/packages",
                             query: kwords,
                             query_mangle: false)
  JSON.parse(response.body)
end

#publish(prefix, **kwords) ⇒ PublishedRepository

Convenience wrapper around Aptly.publish, publishing this repository locally and as only source of prefix.

Parameters:

  • prefix (String)

    prefix to publish under (i.e. published repo name)

Returns:



76
77
78
# File 'lib/aptly/repository.rb', line 76

def publish(prefix, **kwords)
  Aptly.publish([{ Name: self.Name }], prefix, 'local', kwords)
end

#published?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/aptly/repository.rb', line 81

def published?
  !published_in.empty?
end

#published_in {|pub| ... } ⇒ Array<PublishedRepository>

Lists all PublishedRepositories self is published in. Namely self must be a source of the published repository in order for it to appear here. This method always returns an array of affected published repositories. If you use this method with a block it will additionally yield each published repository that would appear in the array, making it a shorthand for Array#each.

Yield Parameters:

Returns:



93
94
95
96
97
98
99
100
101
# File 'lib/aptly/repository.rb', line 93

def published_in
  Aptly::PublishedRepository.list(connection).select do |pub|
    pub.Sources.each do |src|
      next false unless src.Name == self.Name
      yield repo if block_given?
      true
    end
  end
end

#upload(files) ⇒ Object

FIXME: needs to support single files Convenience wrapper around Files.upload and #add_file



30
31
32
33
34
35
36
37
# File 'lib/aptly/repository.rb', line 30

def upload(files)
  prefix = "#{self.class.to_s.tr(':', '_')}-#{Socket.gethostname}-"
  directory = Dir::Tmpname.make_tmpname(prefix, nil)
  Files.upload(files, directory, connection)
  add_file(directory)
ensure
  # FIXME: delete dir?
end