Skip to content

Commit d786c86

Browse files
msabramosepulworld
authored andcommitted
Add --port option + help tweaks (#106)
* Add --port option + help tweaks This adds a `--port` global option, which I neglected to add before. It also tweaks some of the descriptions in the help. * Add tests for AptlyCommand because it has some uncovered code for dealing with options
1 parent 0db7ffd commit d786c86

5 files changed

Lines changed: 135 additions & 46 deletions

File tree

bin/aptly-cli

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ require 'rubygems'
55
require 'commander/import'
66
require 'aptly_cli'
77

8-
program :version, AptlyCli::VERSION
9-
program :description, 'Aptly repository API client'
8+
program :version, AptlyCli::VERSION
9+
program :description, 'Aptly repository API client (https://github.com/sepulworld/aptly_cli)'
1010

1111
$config_file = '/etc/aptly-cli.conf'
1212
$server = nil
13+
$port = nil
1314
$username = nil
1415
$password = nil
1516
$debug = false
@@ -18,13 +19,16 @@ global_option('-c', '--config FILE', 'Path to YAML config file') do |config_file
1819
$config_file = config_file
1920
end
2021

21-
global_option('-s', '--server SERVER', 'Host name or IP address') do |server|
22+
global_option('-s', '--server SERVER', 'Host name or IP address of Aptly API server') do |server|
2223
$server = server
2324
end
24-
global_option('--username USERNAME', 'User name') do |username|
25+
global_option('-p', '--port PORT', 'Port of Aptly API server') do |port|
26+
$port = port
27+
end
28+
global_option('--username USERNAME', 'User name or \'${PROMPT}\'') do |username|
2529
$username = username
2630
end
27-
global_option('--password PASSWORD', 'Password') do |password|
31+
global_option('--password PASSWORD', 'Password or \'${PROMPT_PASSWORD}\' or \'${KEYRING}\'') do |password|
2832
$password = password
2933
end
3034
global_option('--debug', 'Enable debug output') do
@@ -35,6 +39,9 @@ def handle_global_options(options)
3539
if $server
3640
options.server = $server
3741
end
42+
if $port
43+
options.port = $port
44+
end
3845
if $username
3946
options.username = $username
4047
end

lib/aptly_command.rb

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,71 @@
1-
class AptlyCommand
2-
def initialize(config, options = nil)
3-
@config = config
4-
options ||= Options.new
1+
module AptlyCli
2+
class AptlyCommand
3+
include HTTMultiParty
54

6-
if options.server
7-
@config[:server] = options.server
8-
end
5+
attr_accessor :config
96

10-
if options.username
11-
@config[:username] = options.username
12-
end
7+
def initialize(config, options = nil)
8+
@config = config
9+
options ||= Options.new
1310

14-
if options.password
15-
@config[:password] = options.password
16-
end
11+
if options.respond_to?(:server) && options.server
12+
@config[:server] = options.server
13+
end
1714

18-
if options.debug
19-
@config[:debug] = options.debug
20-
end
15+
if options.respond_to?(:port) && options.port
16+
@config[:port] = options.port
17+
end
18+
19+
if options.respond_to?(:username) && options.username
20+
@config[:username] = options.username
21+
end
22+
23+
if options.respond_to?(:password) && options.password
24+
@config[:password] = options.password
25+
end
2126

22-
@config.each do |k, v|
23-
if v == '${PROMPT}'
24-
@config[k.to_sym] = ask("Enter a value for #{k}:")
25-
elsif v == '${PROMPT_PASSWORD}'
26-
@config[k.to_sym] = password("Enter a value for #{k}:")
27-
elsif v == '${KEYRING}'
28-
require 'keyring'
27+
if options.respond_to?(:debug) && options.debug
28+
@config[:debug] = options.debug
29+
end
2930

30-
keyring = Keyring.new
31-
value = keyring.get_password(@config[:server], @config[:username])
31+
@config.each do |k, v|
32+
if v == '${PROMPT}'
33+
@config[k.to_sym] = ask("Enter a value for #{k}:")
34+
elsif v == '${PROMPT_PASSWORD}'
35+
@config[k.to_sym] = password("Enter a value for #{k}:")
36+
elsif v == '${KEYRING}'
37+
require 'keyring'
3238

33-
unless value
34-
# Prompt for password...
35-
value = password("Enter a value for #{k}:")
39+
keyring = Keyring.new
40+
keychain_item_name = 'Aptly API server at ' + \
41+
@config[:server] + ':' + @config[:port].to_s
42+
value = keyring.get_password(keychain_item_name, @config[:username])
3643

37-
# ... and store in keyring
38-
keyring.set_password(@config[:server], @config[:username], value)
39-
end
44+
unless value
45+
# Prompt for password...
46+
value = password("Enter a value for #{k}:")
4047

41-
@config[k.to_sym] = value
48+
# ... and store in keyring
49+
keyring.set_password(keychain_item_name, @config[:username], value)
50+
end
51+
52+
@config[k.to_sym] = value
53+
end
4254
end
43-
end
4455

45-
base_uri = "#{@config[:proto]}://#{@config[:server]}:#{@config[:port]}/api"
46-
self.class.base_uri base_uri
56+
base_uri = "#{@config[:proto]}://#{@config[:server]}:#{@config[:port]}" \
57+
'/api'
58+
self.class.base_uri base_uri
59+
60+
if @config[:username]
61+
if @config[:password]
62+
self.class.basic_auth @config[:username].to_s, @config[:password].to_s
63+
end
64+
end
4765

48-
if @config[:username]
49-
if @config[:password]
50-
self.class.basic_auth @config[:username].to_s, @config[:password].to_s
66+
if self.respond_to?(:debug_output)
67+
debug_output $stdout if @config[:debug] == true
5168
end
5269
end
53-
debug_output $stdout if @config[:debug] == true
5470
end
5571
end

lib/aptly_file.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
module AptlyCli
77
# Uploading file into Aptly
88
class AptlyFile < AptlyCommand
9-
include HTTMultiParty
109
attr_accessor :file_uri, :package, :local_file_path
1110

1211
def file_dir

test/minitest_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
require 'minitest/autorun'
77

88
class Options
9-
attr_accessor :server, :username, :password, :debug
9+
attr_accessor :server, :port, :username, :password, :debug
1010
end

test/test_aptly_command.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'minitest_helper.rb'
2+
require 'minitest/autorun'
3+
4+
require 'aptly_cli'
5+
6+
module AptlyCli
7+
class AptlyCommand
8+
def ask(_prompt)
9+
'zane'
10+
end
11+
12+
def password(_prompt)
13+
'secret'
14+
end
15+
end
16+
end
17+
18+
describe AptlyCli::AptlyCommand do
19+
it 'has a default config' do
20+
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
21+
cmd = AptlyCli::AptlyCommand.new(config)
22+
cmd.config[:proto].must_equal 'http'
23+
cmd.config[:server].must_equal '127.0.0.1'
24+
cmd.config[:port].must_equal 8082
25+
end
26+
27+
it 'accepts empty options and no config changes' do
28+
options = Options.new
29+
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
30+
cmd = AptlyCli::AptlyCommand.new(config, options)
31+
cmd.config[:proto].must_equal 'http'
32+
cmd.config[:server].must_equal '127.0.0.1'
33+
cmd.config[:port].must_equal 8082
34+
end
35+
36+
it 'can take options and updates its config accordingly' do
37+
options = Options.new
38+
options.server = 'my-server'
39+
options.port = 9000
40+
options.username = 'me'
41+
options.password = 'secret'
42+
options.debug = true
43+
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
44+
cmd = AptlyCli::AptlyCommand.new(config, options)
45+
cmd.config[:server].must_equal 'my-server'
46+
cmd.config[:port].must_equal 9000
47+
cmd.config[:username].must_equal 'me'
48+
cmd.config[:password].must_equal 'secret'
49+
cmd.config[:debug].must_equal true
50+
end
51+
52+
it 'can process an option with \'${PROMPT}\' in it' do
53+
options = Options.new
54+
options.username = '${PROMPT}'
55+
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
56+
cmd = AptlyCli::AptlyCommand.new(config, options)
57+
cmd.config[:username].must_equal 'zane'
58+
end
59+
60+
it 'can process an option with \'${PROMPT_PASSWORD}\' in it' do
61+
options = Options.new
62+
options.username = '${PROMPT_PASSWORD}'
63+
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
64+
cmd = AptlyCli::AptlyCommand.new(config, options)
65+
cmd.config[:username].must_equal 'secret'
66+
end
67+
end

0 commit comments

Comments
 (0)