wss_agent 18.10.2 → 18.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +15 -15
  3. data/.rspec +2 -2
  4. data/.travis.yml +6 -6
  5. data/Gemfile +4 -4
  6. data/LICENSE.txt +201 -201
  7. data/README.md +88 -88
  8. data/Rakefile +8 -8
  9. data/bin/wss_agent +13 -13
  10. data/lib/config/custom_default.yml +5 -5
  11. data/lib/config/default.yml +14 -14
  12. data/lib/data/ca-certificates.crt +4049 -4049
  13. data/lib/wss_agent.rb +50 -50
  14. data/lib/wss_agent/cli.rb +56 -56
  15. data/lib/wss_agent/client.rb +108 -108
  16. data/lib/wss_agent/configure.rb +115 -115
  17. data/lib/wss_agent/gem_sha1.rb +73 -73
  18. data/lib/wss_agent/project.rb +39 -39
  19. data/lib/wss_agent/response.rb +57 -57
  20. data/lib/wss_agent/response_inventory.rb +28 -28
  21. data/lib/wss_agent/response_policies.rb +77 -77
  22. data/lib/wss_agent/specifications.rb +202 -202
  23. data/lib/wss_agent/version.rb +4 -4
  24. data/spec/fixtures/vcr_cassettes/WssAgent_CLI/update/when_not_found_token/should_display_error_message.yml +2984 -2984
  25. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/response_should_be_success.yml +2984 -2984
  26. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/should_response_json_data.yml +2984 -2984
  27. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/should_return_message_response.yml +2984 -2984
  28. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_error/should_return_status_of_response.yml +2984 -2984
  29. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/response_should_be_success.yml +2984 -2984
  30. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/should_response_json_data.yml +2984 -2984
  31. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/should_return_message_response.yml +2984 -2984
  32. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/server_timeout/should_return_status_of_response.yml +2984 -2984
  33. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/response_should_be_success.yml +2984 -2984
  34. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/should_response_json_data.yml +2984 -2984
  35. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/should_return_message_response.yml +2984 -2984
  36. data/spec/fixtures/vcr_cassettes/WssAgent_Client/_update/success/should_return_status_of_response.yml +2984 -2984
  37. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_check_policies/should_check_policies.yml +50 -50
  38. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_update/should_update_list_gems_on_server.yml +50 -50
  39. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_update/when_check_policies_is_true/and_check_policies_return_a_violation/should_not_update_inventory.yml +2984 -2984
  40. data/spec/fixtures/vcr_cassettes/WssAgent_Specifications/_update/when_check_policies_is_true/and_check_policies_returns_without_a_violation/should_update_inventory.yml +2984 -2984
  41. data/spec/spec_helper.rb +36 -36
  42. data/spec/support/exit_code_matches.rb +37 -37
  43. data/spec/wss_agent/cli_spec.rb +58 -58
  44. data/spec/wss_agent/client_spec.rb +151 -151
  45. data/spec/wss_agent/configure_spec.rb +180 -180
  46. data/spec/wss_agent/specifications_spec.rb +162 -162
  47. data/wss_agent.gemspec +38 -38
  48. metadata +3 -3
@@ -1,50 +1,50 @@
1
- require 'thor'
2
- require 'net/http'
3
- require 'awesome_print'
4
- require 'psych'
5
- require 'multi_json'
6
- require 'faraday'
7
- require 'faraday_middleware'
8
- require 'yell'
9
- require 'wss_agent/version'
10
- require 'wss_agent/specifications'
11
- require 'wss_agent/configure'
12
- require 'wss_agent/cli'
13
- require 'wss_agent/response'
14
- require 'wss_agent/response_policies'
15
- require 'wss_agent/response_inventory'
16
- require 'wss_agent/client'
17
- require 'wss_agent/gem_sha1'
18
- require 'wss_agent/project'
19
-
20
- module WssAgent
21
- DEFAULT_CA_BUNDLE_PATH = File.dirname(__FILE__) + '/data/ca-certificates.crt'
22
-
23
- class WssAgentError < StandardError
24
- URL_INVALID = 'Api url is invalid. Could you please check url in wss_agent.yml'.freeze
25
- CANNOT_FIND_TOKEN = "Can't find Token, please add your Whitesource API token in the wss_agent.yml file".freeze
26
- CANNOT_FIND_URL = "Can't find the url, please add your Whitesource url destination in the wss_agent.yml file.".freeze
27
- INVALID_CONFIG_FORMAT = 'Problem reading wss_agent.yml, please check the file is a valid YAML'.freeze
28
- NOT_FOUND_CONFIGFILE = "Config file isn't exist. Could you please run 'wss_agent config' before.".freeze
29
-
30
- def self.status_code(code)
31
- define_method(:status_code) { code }
32
- end
33
- end
34
-
35
- class NotFoundConfigFile < WssAgentError; status_code(8); end
36
- class InvalidConfigFile < WssAgentError; status_code(9); end
37
- class TokenNotFound < WssAgentError; status_code(10); end
38
- class ApiUrlNotFound < WssAgentError; status_code(11); end
39
- class ApiUrlInvalid < WssAgentError; status_code(12); end
40
-
41
- def self.logger
42
- @logger ||= Yell.new STDOUT, level: [:info]
43
- end
44
-
45
- def self.enable_debug!
46
- @logger ||= Yell.new(
47
- STDOUT, level: [:debug, :info, :warn, :error, :fatal, :unknown]
48
- )
49
- end
50
- end
1
+ require 'thor'
2
+ require 'net/http'
3
+ require 'awesome_print'
4
+ require 'psych'
5
+ require 'multi_json'
6
+ require 'faraday'
7
+ require 'faraday_middleware'
8
+ require 'yell'
9
+ require 'wss_agent/version'
10
+ require 'wss_agent/specifications'
11
+ require 'wss_agent/configure'
12
+ require 'wss_agent/cli'
13
+ require 'wss_agent/response'
14
+ require 'wss_agent/response_policies'
15
+ require 'wss_agent/response_inventory'
16
+ require 'wss_agent/client'
17
+ require 'wss_agent/gem_sha1'
18
+ require 'wss_agent/project'
19
+
20
+ module WssAgent
21
+ DEFAULT_CA_BUNDLE_PATH = File.dirname(__FILE__) + '/data/ca-certificates.crt'
22
+
23
+ class WssAgentError < StandardError
24
+ URL_INVALID = 'Api url is invalid. Could you please check url in wss_agent.yml'.freeze
25
+ CANNOT_FIND_TOKEN = "Can't find Token, please add your Whitesource API token in the wss_agent.yml file".freeze
26
+ CANNOT_FIND_URL = "Can't find the url, please add your Whitesource url destination in the wss_agent.yml file.".freeze
27
+ INVALID_CONFIG_FORMAT = 'Problem reading wss_agent.yml, please check the file is a valid YAML'.freeze
28
+ NOT_FOUND_CONFIGFILE = "Config file isn't exist. Could you please run 'wss_agent config' before.".freeze
29
+
30
+ def self.status_code(code)
31
+ define_method(:status_code) { code }
32
+ end
33
+ end
34
+
35
+ class NotFoundConfigFile < WssAgentError; status_code(8); end
36
+ class InvalidConfigFile < WssAgentError; status_code(9); end
37
+ class TokenNotFound < WssAgentError; status_code(10); end
38
+ class ApiUrlNotFound < WssAgentError; status_code(11); end
39
+ class ApiUrlInvalid < WssAgentError; status_code(12); end
40
+
41
+ def self.logger
42
+ @logger ||= Yell.new STDOUT, level: [:info]
43
+ end
44
+
45
+ def self.enable_debug!
46
+ @logger ||= Yell.new(
47
+ STDOUT, level: [:debug, :info, :warn, :error, :fatal, :unknown]
48
+ )
49
+ end
50
+ end
@@ -1,56 +1,56 @@
1
- module WssAgent
2
- class CLI < Thor
3
- desc 'config', 'create config file'
4
- def config
5
- File.open(File.join(Dir.pwd, Configure::CURRENT_CONFIG_FILE), 'w') do |f|
6
- f << File.read(Configure.custom_default_path)
7
- end
8
- ap 'Created the config file: wss_agent.yml'
9
- end
10
- map init: :config
11
-
12
- desc 'list', 'display list dependencies'
13
- method_option :all, type: :boolean
14
- method_option :excludes, type: :string
15
- method_option :verbose, aliases: '-v', desc: 'Be verbose'
16
- def list
17
- WssAgent.enable_debug! if options['verbose']
18
- results = Specifications.list(options)
19
- ap results
20
- rescue Bundler::GemfileNotFound => ex
21
- ap ex.message, color: { string: :red }
22
- rescue Bundler::GemNotFound => ex
23
- ap ex.message, color: { string: :red }
24
- ap "Could you execute 'bundle install' before", color: { string: :red }
25
- end
26
-
27
- desc 'update', 'update open source inventory'
28
- method_option :all, type: :boolean
29
- method_option :excludes, type: :string
30
- method_option :verbose, aliases: '-v', desc: 'Be verbose'
31
- method_option :force, type: :boolean, aliases: '-f', desc: 'Force Check All Dependencies'
32
- method_option :'force-update', type: :boolean, desc: 'Force Update'
33
- def update
34
- WssAgent.enable_debug! if options['verbose']
35
- result = Specifications.update(options)
36
- result.success? ? exit(0) : exit(1)
37
- rescue => ex
38
- ap ex.message, color: { string: :red }
39
- abort
40
- end
41
-
42
- desc 'check_policies', 'checking dependencies that they conforms with company policy.'
43
- method_option :verbose, aliases: '-v', desc: 'Be verbose'
44
- method_option :force, type: :boolean, aliases: '-f', desc: 'Force Check All Dependencies'
45
- def check_policies
46
- WssAgent.enable_debug! if options['verbose']
47
- result = Specifications.check_policies(options)
48
- (result.success? && result.policy_violations?) ? exit(1) : exit(0)
49
- end
50
-
51
- desc 'version', 'Agent version'
52
- def version
53
- puts WssAgent::VERSION
54
- end
55
- end
56
- end
1
+ module WssAgent
2
+ class CLI < Thor
3
+ desc 'config', 'create config file'
4
+ def config
5
+ File.open(File.join(Dir.pwd, Configure::CURRENT_CONFIG_FILE), 'w') do |f|
6
+ f << File.read(Configure.custom_default_path)
7
+ end
8
+ ap 'Created the config file: wss_agent.yml'
9
+ end
10
+ map init: :config
11
+
12
+ desc 'list', 'display list dependencies'
13
+ method_option :all, type: :boolean
14
+ method_option :excludes, type: :string
15
+ method_option :verbose, aliases: '-v', desc: 'Be verbose'
16
+ def list
17
+ WssAgent.enable_debug! if options['verbose']
18
+ results = Specifications.list(options)
19
+ ap results
20
+ rescue Bundler::GemfileNotFound => ex
21
+ ap ex.message, color: { string: :red }
22
+ rescue Bundler::GemNotFound => ex
23
+ ap ex.message, color: { string: :red }
24
+ ap "Could you execute 'bundle install' before", color: { string: :red }
25
+ end
26
+
27
+ desc 'update', 'update open source inventory'
28
+ method_option :all, type: :boolean
29
+ method_option :excludes, type: :string
30
+ method_option :verbose, aliases: '-v', desc: 'Be verbose'
31
+ method_option :force, type: :boolean, aliases: '-f', desc: 'Force Check All Dependencies'
32
+ method_option :'force-update', type: :boolean, desc: 'Force Update'
33
+ def update
34
+ WssAgent.enable_debug! if options['verbose']
35
+ result = Specifications.update(options)
36
+ result.success? ? exit(0) : exit(1)
37
+ rescue => ex
38
+ ap ex.message, color: { string: :red }
39
+ abort
40
+ end
41
+
42
+ desc 'check_policies', 'checking dependencies that they conforms with company policy.'
43
+ method_option :verbose, aliases: '-v', desc: 'Be verbose'
44
+ method_option :force, type: :boolean, aliases: '-f', desc: 'Force Check All Dependencies'
45
+ def check_policies
46
+ WssAgent.enable_debug! if options['verbose']
47
+ result = Specifications.check_policies(options)
48
+ (result.success? && result.policy_violations?) ? exit(1) : exit(0)
49
+ end
50
+
51
+ desc 'version', 'Agent version'
52
+ def version
53
+ puts WssAgent::VERSION
54
+ end
55
+ end
56
+ end
@@ -1,108 +1,108 @@
1
- module WssAgent
2
- # Client class
3
- #
4
- class Client
5
- attr_accessor :connection
6
- POLICY_TYPES = {
7
- basic: 'CHECK_POLICIES',
8
- compliance: 'CHECK_POLICY_COMPLIANCE'
9
- }.freeze
10
-
11
- UPDATE_TYPE = 'UPDATE'.freeze
12
- REQUEST_TIMEOUT = 120
13
- RECONNECT_RETRIES = 1
14
- RECONNECT_INTERVAL = 3
15
-
16
-
17
- def initialize
18
- @connection ||= Faraday.new(connection_options) do |h|
19
- h.port = Configure.port
20
- h.headers[:content_type] = 'application/x-www-form-urlencoded'
21
- h.request :url_encoded
22
- h.adapter :excon
23
- end
24
- Excon.defaults[:ciphers] = 'DEFAULT' if defined?(JRuby)
25
-
26
- @connection
27
- end
28
-
29
- def diff(gem_list)
30
- diff_data = {
31
- 'coordinates' => Configure.coordinates,
32
- 'dependencies' => gem_list
33
- }
34
- if Configure['project_token']
35
- diff_data['projectToken'] = Configure['project_token']
36
- end
37
- MultiJson.dump([diff_data])
38
- end
39
-
40
- def payload(gem_list, options = {})
41
- req_options = {
42
- agent: Configure['agent'],
43
- agentVersion: Configure['agent_version'],
44
- token: Configure.token,
45
- product: Configure['product'].to_s,
46
- productVersion: Configure['product_version'].to_s,
47
- timeStamp: Time.now.to_i,
48
- diff: diff(gem_list)
49
- }
50
- req_options[:userKey] = Configure.user_key if Configure.user_key?
51
- req_options.merge(options)
52
- end
53
-
54
- def update(gem_list)
55
- ResponseInventory.new(request(gem_list, type: UPDATE_TYPE))
56
- end
57
-
58
- def check_policies(gem_list, options = {})
59
- request_options =
60
- if Configure['force_check_all_dependencies'] || options['force']
61
- { type: POLICY_TYPES[:compliance], forceCheckAllDependencies: true }
62
- else
63
- { type: POLICY_TYPES[:basic], forceCheckAllDependencies: false }
64
- end
65
-
66
- ResponsePolicies.new(request(gem_list, request_options))
67
- end
68
-
69
- def request(gem_list, options = {})
70
- WssAgent.logger.debug "request params: #{payload(gem_list, options)}"
71
-
72
- retries = Configure['retries'] ? Configure['retries'] : RECONNECT_RETRIES
73
- interval = Configure['interval']? Configure['interval'] : RECONNECT_INTERVAL
74
-
75
- while retries > 0
76
- begin
77
- return connection.post(Configure.api_path, payload(gem_list, options))
78
- rescue Faraday::Error::ClientError => ex
79
- retries = retries - 1
80
- WssAgent.logger.error "Failed to send request to WhiteSource server: #{ex}"
81
- if retries > 0
82
- WssAgent.logger.error "Trying to connect to WhiteSource server again. sleeping #{interval} seconds..."
83
- sleep(interval)
84
- else
85
- return ex
86
- end
87
- end
88
- end
89
- end
90
-
91
- private
92
-
93
- def connection_options
94
- @connection_options ||
95
- begin
96
- @connection_options = {
97
- url: Configure.url, request: { timeout: REQUEST_TIMEOUT }
98
- }
99
- @connection_options[:ssl] = ssl_options if Configure.ssl?
100
- end
101
- @connection_options
102
- end
103
-
104
- def ssl_options
105
- { ca_file: WssAgent::DEFAULT_CA_BUNDLE_PATH }
106
- end
107
- end
108
- end
1
+ module WssAgent
2
+ # Client class
3
+ #
4
+ class Client
5
+ attr_accessor :connection
6
+ POLICY_TYPES = {
7
+ basic: 'CHECK_POLICIES',
8
+ compliance: 'CHECK_POLICY_COMPLIANCE'
9
+ }.freeze
10
+
11
+ UPDATE_TYPE = 'UPDATE'.freeze
12
+ REQUEST_TIMEOUT = 120
13
+ RECONNECT_RETRIES = 1
14
+ RECONNECT_INTERVAL = 3
15
+
16
+
17
+ def initialize
18
+ @connection ||= Faraday.new(connection_options) do |h|
19
+ h.port = Configure.port
20
+ h.headers[:content_type] = 'application/x-www-form-urlencoded'
21
+ h.request :url_encoded
22
+ h.adapter :excon
23
+ end
24
+ Excon.defaults[:ciphers] = 'DEFAULT' if defined?(JRuby)
25
+
26
+ @connection
27
+ end
28
+
29
+ def diff(gem_list)
30
+ diff_data = {
31
+ 'coordinates' => Configure.coordinates,
32
+ 'dependencies' => gem_list
33
+ }
34
+ if Configure['project_token']
35
+ diff_data['projectToken'] = Configure['project_token']
36
+ end
37
+ MultiJson.dump([diff_data])
38
+ end
39
+
40
+ def payload(gem_list, options = {})
41
+ req_options = {
42
+ agent: Configure['agent'],
43
+ agentVersion: Configure['agent_version'],
44
+ token: Configure.token,
45
+ product: Configure['product'].to_s,
46
+ productVersion: Configure['product_version'].to_s,
47
+ timeStamp: Time.now.to_i,
48
+ diff: diff(gem_list)
49
+ }
50
+ req_options[:userKey] = Configure.user_key if Configure.user_key?
51
+ req_options.merge(options)
52
+ end
53
+
54
+ def update(gem_list)
55
+ ResponseInventory.new(request(gem_list, type: UPDATE_TYPE))
56
+ end
57
+
58
+ def check_policies(gem_list, options = {})
59
+ request_options =
60
+ if Configure['force_check_all_dependencies'] || options['force']
61
+ { type: POLICY_TYPES[:compliance], forceCheckAllDependencies: true }
62
+ else
63
+ { type: POLICY_TYPES[:basic], forceCheckAllDependencies: false }
64
+ end
65
+
66
+ ResponsePolicies.new(request(gem_list, request_options))
67
+ end
68
+
69
+ def request(gem_list, options = {})
70
+ WssAgent.logger.debug "request params: #{payload(gem_list, options)}"
71
+
72
+ retries = Configure['retries'] ? Configure['retries'] : RECONNECT_RETRIES
73
+ interval = Configure['interval']? Configure['interval'] : RECONNECT_INTERVAL
74
+
75
+ while retries > 0
76
+ begin
77
+ return connection.post(Configure.api_path, payload(gem_list, options))
78
+ rescue Faraday::Error::ClientError => ex
79
+ retries = retries - 1
80
+ WssAgent.logger.error "Failed to send request to WhiteSource server: #{ex}"
81
+ if retries > 0
82
+ WssAgent.logger.error "Trying to connect to WhiteSource server again. sleeping #{interval} seconds..."
83
+ sleep(interval)
84
+ else
85
+ return ex
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ private
92
+
93
+ def connection_options
94
+ @connection_options ||
95
+ begin
96
+ @connection_options = {
97
+ url: Configure.url, request: { timeout: REQUEST_TIMEOUT }
98
+ }
99
+ @connection_options[:ssl] = ssl_options if Configure.ssl?
100
+ end
101
+ @connection_options
102
+ end
103
+
104
+ def ssl_options
105
+ { ca_file: WssAgent::DEFAULT_CA_BUNDLE_PATH }
106
+ end
107
+ end
108
+ end
@@ -1,115 +1,115 @@
1
- module WssAgent
2
- class Configure
3
- DEFAULT_CONFIG_FILE = 'default.yml'.freeze
4
- CUSTOM_DEFAULT_CONFIG_FILE = 'custom_default.yml'.freeze
5
- CURRENT_CONFIG_FILE = 'wss_agent.yml'.freeze
6
- API_PATH = '/agent'.freeze
7
-
8
- extend SingleForwardable
9
- def_delegator :current, :[]
10
-
11
- class << self
12
- def default_path
13
- File.join(
14
- File.expand_path('../..', __FILE__), 'config', DEFAULT_CONFIG_FILE
15
- )
16
- end
17
-
18
- def custom_default_path
19
- File.join(
20
- File.expand_path('../..', __FILE__), 'config',
21
- CUSTOM_DEFAULT_CONFIG_FILE
22
- )
23
- end
24
-
25
- def exist_default_config?
26
- File.exist?(default_path)
27
- end
28
-
29
- def default
30
- exist_default_config? ? Psych.safe_load(File.read(default_path)) : {}
31
- end
32
-
33
- def current_path
34
- Bundler.root.join(CURRENT_CONFIG_FILE).to_s
35
- end
36
-
37
- def current
38
- unless File.exist?(current_path)
39
- return raise NotFoundConfigFile, WssAgentError::NOT_FOUND_CONFIGFILE
40
- end
41
-
42
- @current_config = Psych.safe_load(File.read(current_path))
43
-
44
- unless @current_config
45
- return raise InvalidConfigFile, WssAgentError::INVALID_CONFIG_FORMAT
46
- end
47
-
48
- default.merge(@current_config)
49
- end
50
-
51
- def uri
52
- @url = current['url']
53
- if @url.nil? || @url == ''
54
- raise ApiUrlNotFound, WssAgentError::CANNOT_FIND_URL
55
- end
56
- URI(@url)
57
- rescue URI::Error
58
- raise ApiUrlInvalid, WssAgentError::URL_INVALID
59
- end
60
-
61
- def port
62
- uri.port || 80
63
- end
64
-
65
- def url
66
- @uri = uri
67
- [@uri.scheme, @uri.host].join('://')
68
- end
69
-
70
- def ssl?
71
- uri.scheme == 'https'
72
- end
73
-
74
- def api_path
75
- @uri = uri
76
- @url_path = @uri.path
77
- @url_path == '' ? API_PATH : @url_path
78
- end
79
-
80
- def token
81
- if current['token'].nil? || (current['token'] == '') ||
82
- (current['token'] == default['token'])
83
- raise TokenNotFound, WssAgentError::CANNOT_FIND_TOKEN
84
- else
85
- current['token']
86
- end
87
- end
88
-
89
- def user_key
90
- current['user_key'].to_s.strip
91
- end
92
-
93
- def user_key?
94
- !user_key.empty?
95
- end
96
-
97
- def project_meta
98
- @project_meta ||= WssAgent::Project.new
99
- end
100
-
101
- def coordinates
102
- return {} unless current['project_token'].to_s.strip.empty?
103
- coordinates_config = current['coordinates']
104
- coordinates_artifact_id = coordinates_config['artifact_id']
105
- coordinates_version = coordinates_config['version']
106
- if coordinates_artifact_id.to_s.strip.empty?
107
- coordinates_artifact_id = project_meta.project_name
108
- coordinates_version = project_meta.project_version
109
- end
110
- { 'artifactId' => coordinates_artifact_id,
111
- 'version' => coordinates_version }
112
- end
113
- end
114
- end
115
- end
1
+ module WssAgent
2
+ class Configure
3
+ DEFAULT_CONFIG_FILE = 'default.yml'.freeze
4
+ CUSTOM_DEFAULT_CONFIG_FILE = 'custom_default.yml'.freeze
5
+ CURRENT_CONFIG_FILE = 'wss_agent.yml'.freeze
6
+ API_PATH = '/agent'.freeze
7
+
8
+ extend SingleForwardable
9
+ def_delegator :current, :[]
10
+
11
+ class << self
12
+ def default_path
13
+ File.join(
14
+ File.expand_path('../..', __FILE__), 'config', DEFAULT_CONFIG_FILE
15
+ )
16
+ end
17
+
18
+ def custom_default_path
19
+ File.join(
20
+ File.expand_path('../..', __FILE__), 'config',
21
+ CUSTOM_DEFAULT_CONFIG_FILE
22
+ )
23
+ end
24
+
25
+ def exist_default_config?
26
+ File.exist?(default_path)
27
+ end
28
+
29
+ def default
30
+ exist_default_config? ? Psych.safe_load(File.read(default_path)) : {}
31
+ end
32
+
33
+ def current_path
34
+ Bundler.root.join(CURRENT_CONFIG_FILE).to_s
35
+ end
36
+
37
+ def current
38
+ unless File.exist?(current_path)
39
+ return raise NotFoundConfigFile, WssAgentError::NOT_FOUND_CONFIGFILE
40
+ end
41
+
42
+ @current_config = Psych.safe_load(File.read(current_path))
43
+
44
+ unless @current_config
45
+ return raise InvalidConfigFile, WssAgentError::INVALID_CONFIG_FORMAT
46
+ end
47
+
48
+ default.merge(@current_config)
49
+ end
50
+
51
+ def uri
52
+ @url = current['url']
53
+ if @url.nil? || @url == ''
54
+ raise ApiUrlNotFound, WssAgentError::CANNOT_FIND_URL
55
+ end
56
+ URI(@url)
57
+ rescue URI::Error
58
+ raise ApiUrlInvalid, WssAgentError::URL_INVALID
59
+ end
60
+
61
+ def port
62
+ uri.port || 80
63
+ end
64
+
65
+ def url
66
+ @uri = uri
67
+ [@uri.scheme, @uri.host].join('://')
68
+ end
69
+
70
+ def ssl?
71
+ uri.scheme == 'https'
72
+ end
73
+
74
+ def api_path
75
+ @uri = uri
76
+ @url_path = @uri.path
77
+ @url_path == '' ? API_PATH : @url_path
78
+ end
79
+
80
+ def token
81
+ if current['token'].nil? || (current['token'] == '') ||
82
+ (current['token'] == default['token'])
83
+ raise TokenNotFound, WssAgentError::CANNOT_FIND_TOKEN
84
+ else
85
+ current['token']
86
+ end
87
+ end
88
+
89
+ def user_key
90
+ current['user_key'].to_s.strip
91
+ end
92
+
93
+ def user_key?
94
+ !user_key.empty?
95
+ end
96
+
97
+ def project_meta
98
+ @project_meta ||= WssAgent::Project.new
99
+ end
100
+
101
+ def coordinates
102
+ return {} unless current['project_token'].to_s.strip.empty?
103
+ coordinates_config = current['coordinates']
104
+ coordinates_artifact_id = coordinates_config['artifact_id']
105
+ coordinates_version = coordinates_config['version']
106
+ if coordinates_artifact_id.to_s.strip.empty?
107
+ coordinates_artifact_id = project_meta.project_name
108
+ coordinates_version = project_meta.project_version
109
+ end
110
+ { 'artifactId' => coordinates_artifact_id,
111
+ 'version' => coordinates_version }
112
+ end
113
+ end
114
+ end
115
+ end