Skip to content

Commit

Permalink
Add new commands repo_package_add and repo_package_delete (#149)
Browse files Browse the repository at this point in the history
* Add new commands repo_package_add and repo_package_delete

* Formatting changes

* More changes to make Hound happy

* Another formatting change to make Hound happy
  • Loading branch information
SHyx0rmZ authored and sepulworld committed Sep 23, 2016
1 parent f4edbef commit 5d73fae
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bin/aptly-cli
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ command :repo_list do |c|
end
end

command :repo_package_add do |c|
c.syntax = 'aptly-cli repo_package_add [options]'
c.summary = 'Add existing package to local repository'
c.description = 'Add existing package to local repository'
c.example 'Add packages to local repository', 'aptly-cli repo_package_add --name megatronsoftware \'Pamd64 geoipupdate 2.0.0 87f1591307e50817\' \'Pi386 geoipupdate 2.0.0 87f1591307e50817\''
c.option '--name NAME', String, 'Local repository name, required'
c.action do |args, options|
config = AptlyCli::AptlyLoad.new.configure_with($config_file)
handle_global_options options
aptly_command = AptlyCli::AptlyRepo.new(config, options)
puts aptly_command.repo_package_add({ :name => options.name }, args)
end
end

command :repo_package_delete do |c|
c.syntax = 'aptly-cli repo_package_delete [options]'
c.summary = 'Delete package from local repository'
c.description = 'Delete package from local repository'
c.example 'Delete packages from local repository', 'aptly-cli repo_package_delete --name megatronsoftware \'Pamd64 geoipupdate 2.0.0 87f1591307e50817\' \'Pi386 geoipupdate 2.0.0 87f1591307e50817\''
c.option '--name NAME', String, 'Local repository name, required'
c.action do |args, options|
config = AptlyCli::AptlyLoad.new.configure_with($config_file)
handle_global_options options
aptly_command = AptlyCli::AptlyRepo.new(config, options)
puts aptly_command.repo_package_delete({ :name => options.name }, args)
end
end

command :repo_package_query do |c|
c.syntax = 'aptly-cli repo_package_query [options]'
c.summary = 'List all packages or search on repo contents, requires --name'
Expand Down
26 changes: 26 additions & 0 deletions lib/aptly_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ def repo_list
self.class.get(uri)
end

def repo_package_add(repo_options, packages)
if !repo_options.is_a?(Hash) || repo_options[:name].nil?
raise ArgumentError.new('Must pass a repository name')
end

uri = '/repos/' + repo_options[:name] + '/packages'
self.class.post(
uri,
body: { PackageRefs: packages }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end

def repo_package_delete(repo_options, packages)
if !repo_options.is_a?(Hash) || repo_options[:name].nil?
raise ArgumentError.new('Must pass a repository name')
end

uri = '/repos/' + repo_options[:name] + '/packages'
self.class.delete(
uri,
body: { PackageRefs: packages }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end

def repo_package_query(repo_options = { name: nil, query: nil,
with_deps: false,
format: nil })
Expand Down
118 changes: 118 additions & 0 deletions test/test_aptly_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,124 @@ def test_repo_show_with_no_name
end
end

describe 'API Package Add To Repo' do
config = AptlyCli::AptlyLoad.new.configure_with(nil)
let(:repo_api) { AptlyCli::AptlyRepo.new(config) }
let(:file_api) { AptlyCli::AptlyFile.new(config) }

def test_package_add_with_name
repo_api.repo_delete(name: 'testrepowithpackage',
force: true)
file_api.file_post(file_uri: '/testdir2',
package: 'testdir2/fixtures/geoipupdate_2.0.0_amd64.deb',
local_file: 'test/fixtures/test_1.0_amd64.deb')
repo_api.repo_create(name: 'testrepowithpackage',
comment: 'testing package add to repo',
DefaultDistribution: 'precisepackageaddtest',
DefaultComponent: nil)
repo_api.repo_create(name: 'testrepotoaddto',
comment: 'testing package add to repo',
DefaultDistribution: 'precisepackageaddtest',
DefaultComponent: nil)
repo_api.repo_upload(name: 'testrepowithpackage',
dir: 'testdir2/',
file: 'test_1.0_amd64.deb')
res = repo_api.repo_package_query(name: 'testrepowithpackage',
query: 'geoipupdate',
with_deps: true,
format: nil)
res = repo_api.repo_package_add({ name: 'testrepotoaddto' }, [res.parsed_response.first.to_s])
assert_equal res.request.uri.to_s,
'http://127.0.0.1:8082/api/repos/testrepotoaddto/packages'
assert_equal res.parsed_response,
'Name' => 'testrepotoaddto',
'Comment' => 'testing package add to repo',
'DefaultDistribution' => 'precisepackageaddtest',
'DefaultComponent' => ''
end

def test_package_add_with_no_name
repo_api.repo_delete(name: 'testrepowithpackage',
force: true)
file_api.file_post(file_uri: '/testdir2',
package: 'testdir2/fixtures/geoipupdate_2.0.0_amd64.deb',
local_file: 'test/fixtures/test_1.0_amd64.deb')
repo_api.repo_create(name: 'testrepowithpackage',
comment: 'testing package add to repo',
DefaultDistribution: 'precisepackageaddtest',
DefaultComponent: nil)
repo_api.repo_create(name: 'testrepotoaddto',
comment: 'testing package add to repo',
DefaultDistribution: 'precisepackageaddtest',
DefaultComponent: nil)
repo_api.repo_upload(name: 'testrepowithpackage',
dir: 'testdir2/',
file: 'test_1.0_amd64.deb')
res = repo_api.repo_package_query(name: 'testrepowithpackage',
query: 'geoipupdate',
with_deps: true,
format: nil)
assert_raises ArgumentError do
repo_api.repo_package_add({}, [res.parsed_response.first.to_s])
end
end
end

describe 'API Package Delete From Repo' do
config = AptlyCli::AptlyLoad.new.configure_with(nil)
let(:repo_api) { AptlyCli::AptlyRepo.new(config) }
let(:file_api) { AptlyCli::AptlyFile.new(config) }

def test_package_delete_with_name
repo_api.repo_delete(name: 'testrepowithpackage',
force: true)
file_api.file_post(file_uri: '/testdir2',
package: 'testdir2/fixtures/geoipupdate_2.0.0_amd64.deb',
local_file: 'test/fixtures/test_1.0_amd64.deb')
repo_api.repo_create(name: 'testrepotodeletefrom',
comment: 'testing package delete from repo',
DefaultDistribution: 'precisepackagedeletetest',
DefaultComponent: nil)
repo_api.repo_upload(name: 'testrepotodeletefrom',
dir: 'testdir2/',
file: 'test_1.0_amd64.deb')
res = repo_api.repo_package_query(name: 'testrepotodeletefrom',
query: 'geoipupdate',
with_deps: true,
format: nil)
res = repo_api.repo_package_delete({ name: 'testrepotodeletefrom' }, [res.parsed_response.first.to_s])
assert_equal res.request.uri.to_s,
'http://127.0.0.1:8082/api/repos/testrepotodeletefrom/packages'
assert_equal res.parsed_response,
'Name' => 'testrepotodeletefrom',
'Comment' => 'testing package delete from repo',
'DefaultDistribution' => 'precisepackagedeletetest',
'DefaultComponent' => ''
end

def test_package_delete_with_no_name
repo_api.repo_delete(name: 'testrepowithpackage',
force: true)
file_api.file_post(file_uri: '/testdir2',
package: 'testdir2/fixtures/geoipupdate_2.0.0_amd64.deb',
local_file: 'test/fixtures/test_1.0_amd64.deb')
repo_api.repo_create(name: 'testrepotodeletefrom',
comment: 'testing package delete from repo',
DefaultDistribution: 'precisepackagedeletetest',
DefaultComponent: nil)
repo_api.repo_upload(name: 'testrepotodeletefrom',
dir: 'testdir2/',
file: 'test_1.0_amd64.deb')
res = repo_api.repo_package_query(name: 'testrepotodeletefrom',
query: 'geoipupdate',
with_deps: true,
format: nil)
assert_raises ArgumentError do
repo_api.repo_package_delete({}, [res.parsed_response.first.to_s])
end
end
end

describe 'API Package Query Repo' do
config = AptlyCli::AptlyLoad.new.configure_with(nil)
let(:repo_api) { AptlyCli::AptlyRepo.new(config) }
Expand Down

0 comments on commit 5d73fae

Please sign in to comment.