dotenv_validator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dotenv_validator.rb +124 -0
  3. data/lib/version.rb +5 -0
  4. metadata +223 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 426cf730fa10449f239c5ae8c01fc268fbc2be4ba8db0a855d5dea84e8dee909
4
+ data.tar.gz: 1dd12b8dbae9d5712e94a33f5de97718f242b18fb5790afae1f75d1752c1455b
5
+ SHA512:
6
+ metadata.gz: 5d3bbbc1a4e3515ebff3248fb36e904247e6fc415a010877e65264bba84b658ab284f922643fe5b5c63c41500f6f8fbc32f460d6964603ff9c8ddc6f4d59ab16
7
+ data.tar.gz: 574809319fb840c2b2f275662ebc3c12502773a6cfc32cd9492fa726852a26afee6adc07b194b75c482093c14c11ccb4864656552f1cb3936a3105b53419c1c4
@@ -0,0 +1,124 @@
1
+ require "fast_blank"
2
+
3
+ # Knows how to check the environment variables and compares it to .env.sample
4
+ # and the comments in every line of your .env.sample file.
5
+ module DotenvValidator
6
+
7
+ # It analyzes the current environment and it compares it to the documentation
8
+ # present in .env.sample.
9
+ #
10
+ # @return [[String],[String]] An array with two arrays. First array: List of missing variables. Second array: List of variables with invalid format.
11
+ def self.analyze_variables
12
+ return [], [] unless File.exist?(sample_file)
13
+
14
+ missing_variables = []
15
+ invalid_format = []
16
+
17
+ open_sample_file.each do |line|
18
+ variable, config = line.split(" #")
19
+ variable_name, _sample = variable.split("=")
20
+ value = ENV[variable_name]
21
+
22
+ if value.nil? || value.blank?
23
+ missing_variables << variable_name if config.to_s.match?(/required/)
24
+ next
25
+ end
26
+
27
+ if config =~ /format=(.*)/
28
+ valid =
29
+ case $1
30
+ when "int", "integer" then integer?(value)
31
+ when "float" then float?(value)
32
+ when "str", "string" then false
33
+ when "email" then email?(value)
34
+ when "url" then url?(value)
35
+ else
36
+ value.match?(Regexp.new($1))
37
+ end
38
+
39
+ invalid_format << variable_name unless valid
40
+ end
41
+ end
42
+
43
+ [missing_variables, invalid_format]
44
+ end
45
+
46
+ # It checks the current environment and it returns a boolean value.
47
+ #
48
+ # @return [Boolean] True if everything looks good. False otherwise.
49
+ def self.check
50
+ result = true
51
+
52
+ missing_variables, invalid_format = analyze_variables
53
+ if missing_variables.any?
54
+ puts("WARNING - Missing environment variables: #{missing_variables.join(", ")}")
55
+ result = false
56
+ end
57
+
58
+ if invalid_format.any?
59
+ puts("WARNING - Environment variables with invalid format: #{invalid_format.join(", ")}")
60
+ result = false
61
+ end
62
+
63
+ result
64
+ end
65
+
66
+ # It checks the current environment and it raises a runtime exception.
67
+ #
68
+ # @raise [RuntimeError] Raised if a missing variable is found or an invalid format is encountered.
69
+ def self.check!
70
+ missing_variables, invalid_format = analyze_variables
71
+
72
+ if missing_variables.any?
73
+ raise("Missing environment variables: #{missing_variables.join(", ")}")
74
+ end
75
+
76
+ if invalid_format.any?
77
+ raise("Environment variables with invalid format: #{invalid_format.join(", ")}")
78
+ end
79
+ end
80
+
81
+ # It checks the value to check if it is a float or not.
82
+ #
83
+ # @param [String] A string
84
+ # @return [Boolean] True if it is a float value. False otherwise.
85
+ def self.float?(string)
86
+ true if Float(string)
87
+ rescue
88
+ false
89
+ end
90
+
91
+ # It checks the value to check if it is an integer or not.
92
+ #
93
+ # @param [String] A string
94
+ # @return [Boolean] True if it is an integer value. False otherwise.
95
+ def self.integer?(string)
96
+ true if Integer(string)
97
+ rescue
98
+ false
99
+ end
100
+
101
+ # It checks the value to check if it is an email or not.
102
+ #
103
+ # @param [String] A string
104
+ # @return [Boolean] True if it is an email value. False otherwise.
105
+ def self.email?(string)
106
+ string.match?(/[\w@]+@[\w@]+\.[\w@]+/)
107
+ end
108
+
109
+ # It checks the value to check if it is a URL or not.
110
+ #
111
+ # @param [String] A string
112
+ # @return [Boolean] True if it is an URL value. False otherwise.
113
+ def self.url?(string)
114
+ string.match?(/https?:\/\/.+/)
115
+ end
116
+
117
+ def self.open_sample_file
118
+ File.open(sample_file)
119
+ end
120
+
121
+ def self.sample_file
122
+ File.join(File.expand_path(File.dirname(__FILE__)), ".env.sample")
123
+ end
124
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DotenvValidator
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotenv_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ariel Juodziukynas
8
+ - Ernesto Tagwerker
9
+ - Francois Buys
10
+ - Luis Sagastume
11
+ - Robert Dormer
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2021-07-29 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: dotenv
19
+ requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: '2.7'
24
+ - - "<"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ type: :runtime
28
+ prerelease: false
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '2.7'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '3.0'
37
+ - !ruby/object:Gem::Dependency
38
+ name: fast_blank
39
+ requirement: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: 1.0.0
51
+ - !ruby/object:Gem::Dependency
52
+ name: byebug
53
+ requirement: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '11.1'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '12.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '11.1'
68
+ - - "<"
69
+ - !ruby/object:Gem::Version
70
+ version: '12.0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: codecov
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 0.5.0
78
+ - - "<"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.0'
81
+ type: :development
82
+ prerelease: false
83
+ version_requirements: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 0.5.0
88
+ - - "<"
89
+ - !ruby/object:Gem::Version
90
+ version: '1.0'
91
+ - !ruby/object:Gem::Dependency
92
+ name: climate_control
93
+ requirement: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '1.0'
98
+ - - "<"
99
+ - !ruby/object:Gem::Version
100
+ version: '2.0'
101
+ type: :development
102
+ prerelease: false
103
+ version_requirements: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '1.0'
108
+ - - "<"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '13.0'
118
+ - - "<"
119
+ - !ruby/object:Gem::Version
120
+ version: '14.0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - "~>"
126
+ - !ruby/object:Gem::Version
127
+ version: '13.0'
128
+ - - "<"
129
+ - !ruby/object:Gem::Version
130
+ version: '14.0'
131
+ - !ruby/object:Gem::Dependency
132
+ name: rspec
133
+ requirement: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '3.0'
138
+ - - "<"
139
+ - !ruby/object:Gem::Version
140
+ version: '4.0'
141
+ type: :development
142
+ prerelease: false
143
+ version_requirements: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '3.0'
148
+ - - "<"
149
+ - !ruby/object:Gem::Version
150
+ version: '4.0'
151
+ - !ruby/object:Gem::Dependency
152
+ name: simplecov
153
+ requirement: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0.21'
158
+ - - "<"
159
+ - !ruby/object:Gem::Version
160
+ version: '1.0'
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0.21'
168
+ - - "<"
169
+ - !ruby/object:Gem::Version
170
+ version: '1.0'
171
+ - !ruby/object:Gem::Dependency
172
+ name: simplecov-console
173
+ requirement: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - "~>"
176
+ - !ruby/object:Gem::Version
177
+ version: 0.9.0
178
+ type: :development
179
+ prerelease: false
180
+ version_requirements: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - "~>"
183
+ - !ruby/object:Gem::Version
184
+ version: 0.9.0
185
+ description: Checks required env variables and its format using dotenv and .env.sample
186
+ files. Sample files include validation documentation which is interpreted and used
187
+ to validate your environment.
188
+ email:
189
+ - arieljuod@gmail.com
190
+ - ernesto+github@ombulabs.com
191
+ - francois@ombulabs.com
192
+ - luis@ombulabs.com
193
+ - robert@ombulabs.com
194
+ executables: []
195
+ extensions: []
196
+ extra_rdoc_files: []
197
+ files:
198
+ - lib/dotenv_validator.rb
199
+ - lib/version.rb
200
+ homepage: https://github.com/fastruby/dotenv_validator
201
+ licenses:
202
+ - MIT
203
+ metadata: {}
204
+ post_install_message:
205
+ rdoc_options: []
206
+ require_paths:
207
+ - lib
208
+ required_ruby_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ required_rubygems_version: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ requirements: []
219
+ rubygems_version: 3.1.4
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: Checks required env variables and its format using .env.sample
223
+ test_files: []