-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
0db7ffd
commit d786c86
Showing
5 changed files
with
135 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |