Class: AptlyCli::AptlyCommand

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/aptly_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = nil) ⇒ AptlyCommand

Returns a new instance of AptlyCommand.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/aptly_command.rb', line 10

def initialize(config, options = nil)
  @config = config
  options ||= Options.new

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

  if options.port
    @config[:port] = options.port
  end

  if options.username
    @config[:username] = options.username
  end

  if options.password
    @config[:password] = options.password
  end

  if options.debug
    @config[:debug] = options.debug
  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'

      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

  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

  self.class.debug_output @config[:debug] ? $stdout : nil
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/aptly_command.rb', line 9

def config
  @config
end

Instance Method Details

#delete(path, options = {}) ⇒ Object



72
73
74
75
# File 'lib/aptly_command.rb', line 72

def delete(path, options = {})
  response = self.class.delete(path, options)
  process_response(response)
end

#get(path, options = {}) ⇒ Object



77
78
79
80
# File 'lib/aptly_command.rb', line 77

def get(path, options = {})
  response = self.class.get(path, options)
  process_response(response)
end

#post(path, options = {}) ⇒ Object



82
83
84
85
# File 'lib/aptly_command.rb', line 82

def post(path, options = {})
  response = self.class.post(path, options)
  process_response(response)
end

#process_response(response) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/aptly_command.rb', line 92

def process_response(response)
  json_response = JSON.parse response.body

  raise "[Server] #{json_response['error']}" unless !json_response.is_a?(Hash) || json_response.dig('error').nil?

  raise HttpNotFoundError, "[HTTP Not Found Error] JSON response:\n#{json_response}" if response.code == 404
  raise HttpInternalServerError, "[HTTP Internal Server Error] JSON response:\n#{json_response}" if response.code == 500

  response
rescue JSON::ParserError
  raise HttpNotFoundError, "[HTTP Not Found Error] Response:\n#{response.body}" if response.code == 404
  raise HttpInternalServerError, "[HTTP Internal Server Error] Response:\n#{response.body}" if response.code == 500

  response
end

#put(path, options = {}) ⇒ Object



87
88
89
90
# File 'lib/aptly_command.rb', line 87

def put(path, options = {})
  response = self.class.put(path, options)
  process_response(response)
end