|
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 |
5 | 4 |
|
6 | | - if options.server |
7 | | - @config[:server] = options.server |
8 | | - end |
| 5 | + attr_accessor :config |
9 | 6 |
|
10 | | - if options.username |
11 | | - @config[:username] = options.username |
12 | | - end |
| 7 | + def initialize(config, options = nil) |
| 8 | + @config = config |
| 9 | + options ||= Options.new |
13 | 10 |
|
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 |
17 | 14 |
|
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 |
21 | 26 |
|
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 |
29 | 30 |
|
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' |
32 | 38 |
|
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]) |
36 | 43 |
|
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}:") |
40 | 47 |
|
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 |
42 | 54 | end |
43 | | - end |
44 | 55 |
|
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 |
47 | 65 |
|
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 |
51 | 68 | end |
52 | 69 | end |
53 | | - debug_output $stdout if @config[:debug] == true |
54 | 70 | end |
55 | 71 | end |
0 commit comments