env-checker 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c947e5de754d1f3516256571096b944fda28114
4
- data.tar.gz: 2e9491a8e67ab09847f242d6e69bfcd426bc386c
3
+ metadata.gz: e6889c86228fd44c99631f0444f423220e039167
4
+ data.tar.gz: 5fb83011dcfbe9029a0b11579bc5f16bb8b535e6
5
5
  SHA512:
6
- metadata.gz: 468664562b4cdcf6237f7d3cba41472b88290db4fbfff3520ada45467fc98b363b91edf573f9c4650db1bef1ddb649d75b4ef1fa0ba4fef7dd673c844dea02fc
7
- data.tar.gz: 74fa44ecf12cec49fd81199fb5cc83a5acc37f1f43bd070aa45abf70b62d07bd0657f7e4bfee41df28c969d76722d54b1eb6fa3471a456c5aac9458bd570f5cf
6
+ metadata.gz: b1580c19ec2e28b7b3814ce6f772e31f6a2b97f47c918b44223d0132b327bdfaeed93c23a9f9b81c441463e27bb9d1f38b15e561239f6fb8042b7cd8210edbc3
7
+ data.tar.gz: c93de2bbaf04a2a1fdeed0e43627894bd3627e88b3d05c75593c7c0ebb686e5fd6ee0206d323e1c4683c61efd277d0ff1fb0f9e7317222ca60bd919c9bbedb7d
data/.gitignore CHANGED
@@ -10,6 +10,7 @@
10
10
  /test/version_tmp/
11
11
  /tmp/
12
12
 
13
+ test_config.yml
13
14
  # Used by dotenv library to load environment variables.
14
15
  # .env
15
16
 
@@ -1,30 +1,39 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-11-21 18:20:26 +0100 using RuboCop version 0.45.0.
3
+ # on 2016-11-26 09:12:44 +0100 using RuboCop version 0.45.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 2
9
+ # Offense count: 4
10
10
  Metrics/AbcSize:
11
- Max: 27
11
+ Max: 31
12
12
 
13
13
  # Offense count: 1
14
14
  # Configuration parameters: CountComments.
15
15
  Metrics/BlockLength:
16
16
  Max: 32
17
17
 
18
+ # Offense count: 1
19
+ Metrics/CyclomaticComplexity:
20
+ Max: 7
21
+
18
22
  # Offense count: 3
19
23
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives.
20
24
  # URISchemes: http, https
21
25
  Metrics/LineLength:
22
26
  Max: 95
23
27
 
24
- # Offense count: 4
28
+ # Offense count: 5
25
29
  # Configuration parameters: CountComments.
26
30
  Metrics/MethodLength:
27
- Max: 23
31
+ Max: 25
32
+
33
+ # Offense count: 1
34
+ # Configuration parameters: CountComments.
35
+ Metrics/ModuleLength:
36
+ Max: 107
28
37
 
29
38
  # Offense count: 1
30
39
  # Cop supports --auto-correct.
@@ -33,14 +42,14 @@ Style/ExtraSpacing:
33
42
  Exclude:
34
43
  - 'env-checker.gemspec'
35
44
 
36
- # Offense count: 16
45
+ # Offense count: 17
37
46
  # Cop supports --auto-correct.
38
47
  # Configuration parameters: EnforcedStyle, SupportedStyles.
39
48
  # SupportedStyles: when_needed, always
40
49
  Style/FrozenStringLiteralComment:
41
50
  Enabled: false
42
51
 
43
- # Offense count: 12
52
+ # Offense count: 15
44
53
  # Cop supports --auto-correct.
45
54
  # Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
46
55
  # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- env-checker (0.1.8)
4
+ env-checker (0.1.9)
5
5
  slack-notifier (~> 1.5)
6
6
  thor (~> 0.19.1)
7
7
 
data/README.md CHANGED
@@ -135,7 +135,7 @@ $ env-checker check --config_file sample_config.yml
135
135
 
136
136
  ## Contributing
137
137
 
138
- Bug reports and pull requests are welcome on GitHub at https://github.com/ryanfox1985/env-checker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
138
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ryanfox1985/env-checker/issues and https://github.com/ryanfox1985/env-checker/pulls. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
139
139
 
140
140
 
141
141
  ## License
@@ -5,6 +5,7 @@ require 'env_checker/version'
5
5
  require 'env_checker/missing_keys_error'
6
6
  require 'env_checker/configuration'
7
7
  require 'env_checker/cli'
8
+ require 'env_checker/notifier'
8
9
 
9
10
  module EnvChecker
10
11
  class << self
@@ -14,13 +15,12 @@ module EnvChecker
14
15
  self.configurations ||= {}
15
16
  configurations['global'] = Configuration.new
16
17
  yield(configurations['global'])
17
- after_configure_and_check(configurations['global'])
18
+ after_configure_and_check(configurations)
18
19
  end
19
20
 
20
21
  def cli_configure_and_check(options)
21
- if run_configure_and_check(options) && options[:run]
22
+ run_configure_and_check(options) && options[:run] &&
22
23
  exit(system(options[:run]))
23
- end
24
24
 
25
25
  exit(true)
26
26
  end
@@ -35,7 +35,9 @@ module EnvChecker
35
35
  self.configurations = create_config_from_parameters(options)
36
36
 
37
37
  begin
38
- exit(1) unless after_configure_and_check(configurations['global'])
38
+ unless after_configure_and_check(configurations)
39
+ exit(options[:run] ? system(options[:run]) : 1)
40
+ end
39
41
  rescue EnvChecker::MissingKeysError
40
42
  exit 2
41
43
  rescue EnvChecker::ConfigurationError
@@ -45,19 +47,26 @@ module EnvChecker
45
47
  true
46
48
  end
47
49
 
48
- def after_configure_and_check(configuration)
49
- configuration.after_initialize
50
+ def after_configure_and_check(configurations)
51
+ environments_to_check = %w(global)
50
52
 
51
- bov = check_optional_variables(configuration)
52
- brv = check_required_variables(configuration)
53
+ current_env = configurations['global'].environment
54
+ if current_env && configurations.key?(current_env)
55
+ environments_to_check << current_env
56
+ end
53
57
 
54
- bov & brv
58
+ environments_to_check.map do |env|
59
+ configurations[env].after_initialize
60
+
61
+ check_optional_variables(env, configurations) &
62
+ check_required_variables(env, configurations)
63
+ end.reduce(:&)
55
64
  end
56
65
 
57
66
  def create_config_from_parameters(options)
58
- config = Configuration.new
59
-
60
- attributes = %w(environment
67
+ configurations = { 'global' => Configuration.new }
68
+ attributes = %w(environments
69
+ environment
61
70
  optional_variables
62
71
  required_variables
63
72
  slack_webhook_url)
@@ -65,48 +74,66 @@ module EnvChecker
65
74
  if options[:config_file]
66
75
  from_file = YAML.load_file(options[:config_file])
67
76
 
68
- attributes.each do |a|
69
- config.public_send("#{a}=", from_file[a]) if from_file[a]
77
+ configurations = config_from_file('global', attributes, from_file)
78
+ if configurations['global'].environments.any?
79
+ configurations['global'].environments.each do |env|
80
+ configurations
81
+ .merge!(config_from_file(env, attributes, from_file[env]))
82
+ end
70
83
  end
84
+ end
71
85
 
72
- return { 'global' => config }
86
+ attributes.each do |a|
87
+ options[a.to_sym] &&
88
+ configurations['global'].public_send("#{a}=", options[a.to_sym])
73
89
  end
74
90
 
91
+ configurations
92
+ end
93
+
94
+ def config_from_file(name, attributes, from_file)
95
+ return { name => Configuration.new } unless from_file
96
+
97
+ config = Configuration.new
75
98
  attributes.each do |a|
76
- config.public_send("#{a}=", options[a.to_sym]) if options[a.to_sym]
99
+ config.public_send("#{a}=", from_file[a]) if from_file[a]
77
100
  end
78
101
 
79
- { 'global' => config }
102
+ { name => config }
80
103
  end
81
104
 
82
- def check_optional_variables(configuration)
105
+ def check_optional_variables(env, configurations)
83
106
  return true if
84
- !configuration.optional_variables ||
85
- configuration.optional_variables.empty?
107
+ !configurations[env].optional_variables ||
108
+ configurations[env].optional_variables.empty?
86
109
 
87
- missing_keys = missing_keys_env(configuration.optional_variables)
110
+ missing_keys = missing_keys_env(configurations[env].optional_variables)
88
111
  return true if missing_keys.empty?
89
112
 
90
- log_message(configuration,
91
- :warning,
92
- configuration.environment,
93
- "Warning! Missing optional variables: #{missing_keys}")
113
+ Notifier.log_message(
114
+ configurations[env],
115
+ :warning,
116
+ env,
117
+ "Warning! Missing optional variables: #{missing_keys}"
118
+ )
94
119
 
95
120
  false
96
121
  end
97
122
 
98
- def check_required_variables(configuration)
123
+ def check_required_variables(env, configurations)
99
124
  return true if
100
- !configuration.required_variables ||
101
- configuration.required_variables.empty?
125
+ !configurations[env].required_variables ||
126
+ configurations[env].required_variables.empty?
102
127
 
103
- missing_keys = missing_keys_env(configuration.required_variables)
128
+ missing_keys = missing_keys_env(configurations[env].required_variables)
104
129
 
105
130
  if missing_keys.any?
106
- log_message(configuration,
107
- :error,
108
- configuration.environment,
109
- "Error! Missing required variables: #{missing_keys}")
131
+ Notifier.log_message(
132
+ configurations[env],
133
+ :error,
134
+ env,
135
+ "Error! Missing required variables: #{missing_keys}"
136
+ )
110
137
 
111
138
  raise MissingKeysError.new(missing_keys)
112
139
  end
@@ -114,26 +141,6 @@ module EnvChecker
114
141
  true
115
142
  end
116
143
 
117
- def log_message(configuration, type, environment, error_message)
118
- return unless error_message
119
-
120
- message = format_error_message(environment, error_message)
121
-
122
- configuration.notify_slack(message)
123
- # TODO: add other integrations like email...
124
- configuration.notify_logger(type, message)
125
- end
126
-
127
- def format_error_message(environment, error_message)
128
- return [] unless error_message
129
-
130
- messages = []
131
- messages << '[EnvChecker]'
132
- messages << "[#{environment}]" if environment
133
- messages << error_message
134
- messages.join(' ')
135
- end
136
-
137
144
  def missing_keys_env(keys)
138
145
  keys.flatten - ::ENV.keys
139
146
  end
@@ -5,6 +5,7 @@ module EnvChecker
5
5
  desc 'version', 'EnvChecker version.'
6
6
  def version
7
7
  puts "EnvChecker #{EnvChecker::VERSION}"
8
+ EnvChecker::VERSION
8
9
  end
9
10
 
10
11
  option :environment, :aliases => :e, :type => :string
@@ -12,11 +13,11 @@ module EnvChecker
12
13
  option :required_variables, :aliases => [:r, :required], :type => :array
13
14
  option :optional_variables, :aliases => [:o, :optional], :type => :array
14
15
  option :slack_webhook_url, :aliases => :slack, :type => :string
16
+ option :verbose, :aliases => :v, :type => :boolean, :default => false
15
17
  option :run, :type => :string
16
18
  desc 'check', 'Check optional and required environment variables.'
17
19
  def check
18
- output = []
19
- output << 'Variables: '
20
+ output = %w(Variables:)
20
21
 
21
22
  variables = %w(config_file
22
23
  slack_webhook_url
@@ -30,7 +31,10 @@ module EnvChecker
30
31
  env_var_name = "env_checker_#{v}".upcase
31
32
  if ENV[env_var_name]
32
33
  my_options[v.to_sym] ||= if env_var_name.include?('_VARIABLES')
33
- ENV[env_var_name].split(' ')
34
+ ENV[env_var_name]
35
+ .split(' ')
36
+ .map { |elem| elem.split(',') }
37
+ .flatten
34
38
  else
35
39
  ENV[env_var_name]
36
40
  end
@@ -38,7 +42,8 @@ module EnvChecker
38
42
  my_options[v.to_sym] = options[v.to_sym] if options[v.to_sym]
39
43
  output << "- #{v}: #{my_options[v.to_sym]}" if my_options[v.to_sym]
40
44
  end
41
- puts output.join("\n")
45
+
46
+ puts output.join("\n") if options[:verbose]
42
47
 
43
48
  EnvChecker.cli_configure_and_check(my_options)
44
49
  end
@@ -7,11 +7,13 @@ module EnvChecker
7
7
 
8
8
  class Configuration
9
9
  attr_accessor :required_variables, :optional_variables, :logger,
10
- :environment, :slack_webhook_url, :slack_notifier
10
+ :environment, :slack_webhook_url, :slack_notifier,
11
+ :environments
11
12
 
12
13
  # Has default settings, which can be overridden in the initializer.
13
14
  def initialize
14
15
  @environment = ENV['RACK_ENV'] || ENV['RAILS_ENV']
16
+ @environments = []
15
17
  @required_variables = []
16
18
  @optional_variables = []
17
19
  @slack_webhook_url = nil
@@ -35,24 +37,6 @@ module EnvChecker
35
37
  true
36
38
  end
37
39
 
38
- def notify_slack(message)
39
- slack_notifier && slack_notifier.ping(message)
40
- rescue StandardError => e
41
- notify_logger(:error, e)
42
- end
43
-
44
- def notify_logger(type, message)
45
- logger &&
46
- case type
47
- when :warning
48
- logger.warn(message)
49
- when :error
50
- logger.error(message)
51
- else
52
- logger.info(message)
53
- end
54
- end
55
-
56
40
  private
57
41
 
58
42
  def valid?
@@ -0,0 +1,55 @@
1
+ module EnvChecker
2
+ class Notifier
3
+ class << self
4
+ def log_message(configuration, type, environment, error_message)
5
+ return unless error_message
6
+
7
+ message = format_error_message(environment, error_message)
8
+ notify(configuration, type, message)
9
+ end
10
+
11
+ def format_error_message(environment, error_message)
12
+ return [] unless error_message
13
+
14
+ messages = %w([EnvChecker])
15
+ messages << "[#{environment}]" if environment
16
+ messages << error_message
17
+ messages.join(' ')
18
+ end
19
+
20
+ def notify(configuration, type, message)
21
+ notify_slack(configuration, message)
22
+ notify_rollbar(configuration, message)
23
+ notify_email(configuration, message)
24
+ notify_logger(configuration, type, message)
25
+ end
26
+
27
+ def notify_rollbar(configuration, message)
28
+ # TODO: implement rollbar
29
+ end
30
+
31
+ def notify_email(configuration, message)
32
+ # TODO: implement email
33
+ end
34
+
35
+ def notify_slack(configuration, message)
36
+ configuration.slack_notifier &&
37
+ configuration.slack_notifier.ping(message)
38
+ rescue StandardError => e
39
+ notify_logger(:error, e)
40
+ end
41
+
42
+ def notify_logger(configuration, type, message)
43
+ configuration.logger &&
44
+ case type
45
+ when :warning
46
+ configuration.logger.warn(message)
47
+ when :error
48
+ configuration.logger.error(message)
49
+ else
50
+ configuration.logger.info(message)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module EnvChecker
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env-checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillermo Guerrero Ibarra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-24 00:00:00.000000000 Z
11
+ date: 2016-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -167,9 +167,9 @@ files:
167
167
  - lib/env_checker/cli.rb
168
168
  - lib/env_checker/configuration.rb
169
169
  - lib/env_checker/missing_keys_error.rb
170
+ - lib/env_checker/notifier.rb
170
171
  - lib/env_checker/version.rb
171
172
  - sample_config.yml
172
- - test_config.yml
173
173
  homepage: https://github.com/ryanfox1985/env-checker
174
174
  licenses:
175
175
  - MIT
@@ -1,7 +0,0 @@
1
- ---
2
- optional_variables:
3
- - OptMyVar1
4
- - OptMyVar2
5
- required_variables:
6
- - MyVar1
7
- - MyVar2