Skip to content

Commit

Permalink
Add --port option + help tweaks (#106)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
msabramo authored and sepulworld committed Jul 22, 2016
1 parent 0db7ffd commit d786c86
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 46 deletions.
17 changes: 12 additions & 5 deletions bin/aptly-cli
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ require 'rubygems'
require 'commander/import'
require 'aptly_cli'

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

$config_file = '/etc/aptly-cli.conf'
$server = nil
$port = nil
$username = nil
$password = nil
$debug = false
Expand All @@ -18,13 +19,16 @@ global_option('-c', '--config FILE', 'Path to YAML config file') do |config_file
$config_file = config_file
end

global_option('-s', '--server SERVER', 'Host name or IP address') do |server|
global_option('-s', '--server SERVER', 'Host name or IP address of Aptly API server') do |server|
$server = server
end
global_option('--username USERNAME', 'User name') do |username|
global_option('-p', '--port PORT', 'Port of Aptly API server') do |port|
$port = port
end
global_option('--username USERNAME', 'User name or \'${PROMPT}\'') do |username|
$username = username
end
global_option('--password PASSWORD', 'Password') do |password|
global_option('--password PASSWORD', 'Password or \'${PROMPT_PASSWORD}\' or \'${KEYRING}\'') do |password|
$password = password
end
global_option('--debug', 'Enable debug output') do
Expand All @@ -35,6 +39,9 @@ def handle_global_options(options)
if $server
options.server = $server
end
if $port
options.port = $port
end
if $username
options.username = $username
end
Expand Down
94 changes: 55 additions & 39 deletions lib/aptly_command.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,71 @@
class AptlyCommand
def initialize(config, options = nil)
@config = config
options ||= Options.new
module AptlyCli
class AptlyCommand
include HTTMultiParty

if options.server
@config[:server] = options.server
end
attr_accessor :config

if options.username
@config[:username] = options.username
end
def initialize(config, options = nil)
@config = config
options ||= Options.new

if options.password
@config[:password] = options.password
end
if options.respond_to?(:server) && options.server
@config[:server] = options.server
end

if options.debug
@config[:debug] = options.debug
end
if options.respond_to?(:port) && options.port
@config[:port] = options.port
end

if options.respond_to?(:username) && options.username
@config[:username] = options.username
end

if options.respond_to?(:password) && options.password
@config[:password] = options.password
end

@config.each do |k, v|
if v == '${PROMPT}'
@config[k.to_sym] = ask("Enter a value for #{k}:")
elsif v == '${PROMPT_PASSWORD}'
@config[k.to_sym] = password("Enter a value for #{k}:")
elsif v == '${KEYRING}'
require 'keyring'
if options.respond_to?(:debug) && options.debug
@config[:debug] = options.debug
end

keyring = Keyring.new
value = keyring.get_password(@config[:server], @config[:username])
@config.each do |k, v|
if v == '${PROMPT}'
@config[k.to_sym] = ask("Enter a value for #{k}:")
elsif v == '${PROMPT_PASSWORD}'
@config[k.to_sym] = password("Enter a value for #{k}:")
elsif v == '${KEYRING}'
require 'keyring'

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

# ... and store in keyring
keyring.set_password(@config[:server], @config[:username], value)
end
unless value
# Prompt for password...
value = password("Enter a value for #{k}:")

@config[k.to_sym] = value
# ... and store in keyring
keyring.set_password(keychain_item_name, @config[:username], value)
end

@config[k.to_sym] = value
end
end
end

base_uri = "#{@config[:proto]}://#{@config[:server]}:#{@config[:port]}/api"
self.class.base_uri base_uri
base_uri = "#{@config[:proto]}://#{@config[:server]}:#{@config[:port]}" \
'/api'
self.class.base_uri base_uri

if @config[:username]
if @config[:password]
self.class.basic_auth @config[:username].to_s, @config[:password].to_s
end
end

if @config[:username]
if @config[:password]
self.class.basic_auth @config[:username].to_s, @config[:password].to_s
if self.respond_to?(:debug_output)
debug_output $stdout if @config[:debug] == true
end
end
debug_output $stdout if @config[:debug] == true
end
end
1 change: 0 additions & 1 deletion lib/aptly_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
module AptlyCli
# Uploading file into Aptly
class AptlyFile < AptlyCommand
include HTTMultiParty
attr_accessor :file_uri, :package, :local_file_path

def file_dir
Expand Down
2 changes: 1 addition & 1 deletion test/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
require 'minitest/autorun'

class Options
attr_accessor :server, :username, :password, :debug
attr_accessor :server, :port, :username, :password, :debug
end
67 changes: 67 additions & 0 deletions test/test_aptly_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'minitest_helper.rb'
require 'minitest/autorun'

require 'aptly_cli'

module AptlyCli
class AptlyCommand
def ask(_prompt)
'zane'
end

def password(_prompt)
'secret'
end
end
end

describe AptlyCli::AptlyCommand do
it 'has a default config' do
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
cmd = AptlyCli::AptlyCommand.new(config)
cmd.config[:proto].must_equal 'http'
cmd.config[:server].must_equal '127.0.0.1'
cmd.config[:port].must_equal 8082
end

it 'accepts empty options and no config changes' do
options = Options.new
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
cmd = AptlyCli::AptlyCommand.new(config, options)
cmd.config[:proto].must_equal 'http'
cmd.config[:server].must_equal '127.0.0.1'
cmd.config[:port].must_equal 8082
end

it 'can take options and updates its config accordingly' do
options = Options.new
options.server = 'my-server'
options.port = 9000
options.username = 'me'
options.password = 'secret'
options.debug = true
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
cmd = AptlyCli::AptlyCommand.new(config, options)
cmd.config[:server].must_equal 'my-server'
cmd.config[:port].must_equal 9000
cmd.config[:username].must_equal 'me'
cmd.config[:password].must_equal 'secret'
cmd.config[:debug].must_equal true
end

it 'can process an option with \'${PROMPT}\' in it' do
options = Options.new
options.username = '${PROMPT}'
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
cmd = AptlyCli::AptlyCommand.new(config, options)
cmd.config[:username].must_equal 'zane'
end

it 'can process an option with \'${PROMPT_PASSWORD}\' in it' do
options = Options.new
options.username = '${PROMPT_PASSWORD}'
config = AptlyCli::AptlyLoad.new.configure_with('/no/config')
cmd = AptlyCli::AptlyCommand.new(config, options)
cmd.config[:username].must_equal 'secret'
end
end

0 comments on commit d786c86

Please sign in to comment.