lib/aptly_command.rb in aptly_cli-0.3.0 vs lib/aptly_command.rb in aptly_cli-0.3.1

- old
+ new

@@ -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 - @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?(:username) && options.username + @config[:username] = options.username + end - keyring = Keyring.new - value = keyring.get_password(@config[:server], @config[:username]) + if options.respond_to?(:password) && options.password + @config[:password] = options.password + end - unless value - # Prompt for password... - value = password("Enter a value for #{k}:") + if options.respond_to?(:debug) && options.debug + @config[:debug] = options.debug + end - # ... and store in keyring - keyring.set_password(@config[:server], @config[:username], value) - 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' - @config[k.to_sym] = value + 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]) + + unless value + # Prompt for password... + value = password("Enter a value for #{k}:") + + # ... 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 + if @config[:username] + if @config[:password] + self.class.basic_auth @config[:username].to_s, @config[:password].to_s + end end + + if respond_to?(:debug_output) + debug_output $stdout if @config[:debug] == true + end end - debug_output $stdout if @config[:debug] == true end end