encrypt_env 1.3.3 → 1.3.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da8b5db8cc944d9f2ec3f293efa316cb67dc54e56695ffef220303f9ceb566d5
4
- data.tar.gz: 5baa2602d7d3e8b780d45c4c080b4ca49177f10d205072e72e859c15bb795f21
3
+ metadata.gz: '0108116f53998a69c29a421d3f0726cbaeb9affa7a194b891a4bcec681d0823e'
4
+ data.tar.gz: 0e13e28fa7e990f0ffc3950fbe429e1c5b49b0f986c8ee26e6a45f7da3007b66
5
5
  SHA512:
6
- metadata.gz: dc119a9d8343930f9ba286d88542084692cb6f4d3f820fc7c82e64f61b6d80a5fbf8216604635e14104118c58b23d0e7ad1ef49e20f15fb0732339010ca0e3d9
7
- data.tar.gz: 48eee986594c2327f0cb38ebaf52c8362c881213bbe91e5fc9b6ae247d7a677c81f608ff3b9d4f0bec34430a4438369f22415e1956aabf8308a8a0e87f1c4d6b
6
+ metadata.gz: 5c0ad88fdf14079b84b9a0364a8ef1e1381e1862083b608adc15706f2bcbdcc305de6cd943d5cd49b6b4d2b947458ea222da9105cd686a86d447e01fff29f56b
7
+ data.tar.gz: 4ae071fc5a3ce618cec111bb75b665f084def3e75debee1da01f445c555e46d56f91c88af521b9c5ff48421e94397b22645e6ec545483e76a0f5c72b0646dd6e
data/bin/encrypt_env CHANGED
@@ -3,71 +3,85 @@
3
3
  # frozen_string_literal: true
4
4
 
5
5
  require 'encrypt_env'
6
+ require 'optparse'
6
7
  require_relative '../lib/encrypt_env/version'
7
8
 
9
+ options = {}
10
+ parsers = OptionParser.new do |parser|
11
+ parser.on('-e', '--environment ENVIRONMENT', 'environment')
12
+ parser.on('-s', '--set VALUE', 'value')
13
+ parser.on('-t', '--type TYPE', 'type of variable')
14
+ parser.on('-a', '--all', 'show all') do
15
+ EncryptEnv.show('all')
16
+ exit
17
+ end
18
+ parser.on('-v', '--version', 'version') do
19
+ puts Version::VERSION
20
+ exit
21
+ end
22
+ parser.on('-h', '--help', 'help') do
23
+ puts <<~HELP
24
+ Usage:
25
+ encrypt_env setup # To setup for the firt time
26
+
27
+ encrypt_env show # To show environment variables of current environment
28
+ encrypt_env show -a # To show all environment variables
29
+ encrypt_env show -e [environment] # To show specific environment variables
30
+ encrypt_env show [variable_name] -e [environment] # To show value of specific variable
31
+
32
+ encrypt_env edit # To edit environment variables of current environment
33
+ encrypt_env edit -e [environment] # To edit specific environment variables
34
+ encrypt_env edit [variable_name] -e [environment] # To edit value of specific variable
35
+
36
+ encrypt_env create variable_name # To create environment variable in current environment
37
+ encrypt_env create variable_name -e [environment] # To create environment variable in specific environment
38
+ # To create environment variable in specific environment with value and type
39
+ encrypt_env create variable_name -s [value] -e [environment]
40
+ encrypt_env create variable_name -s [value] -e [environment] -t [type]
41
+
42
+ encrypt_env delete variable_name # To delete environment variable in current environment
43
+ encrypt_env delete variable_name -e [environment] # To delete environment variable in specific environment
44
+
45
+ Or: Visit "https://github.com/nnhutan/encrypt_env" for more information
46
+ HELP
47
+ exit
48
+ end
49
+ end
50
+
51
+ parsers.parse!(into: options)
52
+
8
53
  argv = ARGV
9
54
  action = argv.shift
55
+ variable_name = argv.shift
10
56
 
11
- if action == 'setup'
57
+ case action
58
+ when 'setup'
12
59
  EncryptEnv.setup
13
- exit 0
14
- elsif action == 'show'
15
- if argv[0]
16
- EncryptEnv.show(argv[0])
17
- else
18
- EncryptEnv.show
60
+
61
+ when 'show', 'edit'
62
+ EncryptEnv.send(action, options[:environment], variable_name)
63
+
64
+ when 'create'
65
+ unless variable_name
66
+ puts "Please provide 'variable's name'!"
67
+ exit
19
68
  end
20
- exit 0
21
- elsif action == 'all'
22
- EncryptEnv.show('all')
23
- exit 0
24
- elsif action == 'edit'
25
- if argv[0]
26
- EncryptEnv.edit(argv[0])
69
+
70
+ if options[:set]
71
+ EncryptEnv.create_with_value(variable_name, options[:set], options[:environment], options[:type])
27
72
  else
28
- EncryptEnv.edit
73
+ EncryptEnv.send(action, variable_name, options[:environment])
74
+ end
75
+
76
+ when 'delete'
77
+ unless variable_name
78
+ puts "Please provide 'variable's name'!"
79
+ exit
29
80
  end
30
- exit 0
31
- elsif action == 'get'
32
- key = argv[0]
33
- env = argv[1]
34
- EncryptEnv.valueof(key, env)
35
- exit 0
36
- elsif action == 'new'
37
- key = argv[0]
38
- env = argv[1]
39
- EncryptEnv.update_variable(key, env, true)
40
- exit 0
41
- elsif action == 'update'
42
- key = argv[0]
43
- env = argv[1]
44
- EncryptEnv.update_variable(key, env, false)
45
- exit 0
46
- elsif action == 'delete'
47
- key = argv[0]
48
- env = argv[1]
49
- EncryptEnv.delete_variable(key, env)
50
- exit 0
51
- elsif ['-v', '--version', 'version'].include?(action)
52
- puts Version::VERSION
53
- exit 0
54
- elsif ['help', '--help', '-h'].include?(action)
55
- puts <<~HELP
56
- Usage:
57
- encrypt_env setup # To setup for the firt time
58
- encrypt_env show all # To show all environment variables
59
- encrypt_env show / encrypt_env show [environment] # To show environment variables
60
- encrypt_env edit / encrypt_env edit [environment] # To edit environment variables
61
- encrypt_env add variable_name [environment] # To add environment variable
62
- encrypt_env get variable_name [environment] # To show value of specific variable
63
- encrypt_env update variable_name [environment] # To edit value of specific variable
64
- encrypt_env delete variable_name [environment] # To delete specific variable
65
- Or: Visit "https://github.com/nnhutan/encrypt_env" for more information
66
- HELP
67
-
68
- exit 0
81
+
82
+ EncryptEnv.send(action, variable_name, options[:environment])
69
83
  else
70
84
  puts "Unknown action: #{action}"
71
85
  puts "Use 'encrypt_env -h' for more help"
72
- exit 1
86
+ exit
73
87
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Version
4
- VERSION = '1.3.3'
4
+ VERSION = '1.3.4'
5
5
  end
data/lib/encrypt_env.rb CHANGED
@@ -121,6 +121,7 @@ class EncryptEnv
121
121
  raise e.message
122
122
  end
123
123
 
124
+ puts 'Decrypting...'
124
125
  decipher = OpenSSL::Cipher.new('aes-128-gcm')
125
126
  decipher.decrypt
126
127
  data = load_encrypted_data(env)
@@ -153,19 +154,23 @@ class EncryptEnv
153
154
  def self.secrets_all
154
155
  return all_decrypted_object if @opt == 2
155
156
 
156
- decrypt
157
+ decrypt unless @decrypted
157
158
  @decrypted
158
159
  end
159
160
 
160
161
  def self.secrets(env = nil)
161
162
  load_curr_opt unless @opt
162
- return secrets_all if env == 'all'
163
+ if env == 'all'
164
+ result = secrets_all
165
+ @decrypted = nil
166
+ return result
167
+ end
163
168
 
164
169
  if @opt == 1
165
- decrypt
170
+ decrypt unless @decrypted
166
171
  @decrypted[env || current_env]
167
172
  else
168
- decrypt(env || current_env)
173
+ decrypt(env || current_env) unless @decrypted
169
174
  @decrypted
170
175
  end
171
176
  rescue StandardError => e
@@ -197,7 +202,9 @@ class EncryptEnv
197
202
  system("echo 'Set up complete!'")
198
203
  end
199
204
 
200
- def self.edit(env = nil)
205
+ def self.edit(env = nil, variable_name = nil)
206
+ variable_name && (return create(variable_name, env, true))
207
+
201
208
  load_curr_opt unless @opt
202
209
  env ||= current_env if @opt == 2
203
210
  return unless decrypt(env)
@@ -214,7 +221,9 @@ class EncryptEnv
214
221
  puts e.message
215
222
  end
216
223
 
217
- def self.show(env = nil)
224
+ def self.show(env = nil, variable_name = nil)
225
+ variable_name && (return valueof(variable_name, env))
226
+
218
227
  require 'awesome_print'
219
228
  require 'date'
220
229
  value = secrets(env)
@@ -233,7 +242,7 @@ class EncryptEnv
233
242
  puts value[key]
234
243
  end
235
244
 
236
- def self.delete_variable(key, env = nil)
245
+ def self.delete(key, env = nil)
237
246
  load_curr_opt unless @opt
238
247
  if @opt == 1
239
248
  puts 'Only for option 2!'
@@ -253,34 +262,104 @@ class EncryptEnv
253
262
  return
254
263
  end
255
264
 
265
+ tmp_value = value[key]
256
266
  value.delete(key)
257
267
  encrypt(value.to_hash.to_yaml, env || current_env)
258
- puts "Delete '#{key}' successfully!"
268
+ puts "Delete '#{key}' with value '#{tmp_value}' successfully!"
269
+ end
270
+
271
+ private_class_method def self.define_type_new_variable
272
+ types = {
273
+ '1' => 'integer',
274
+ '2' => 'float',
275
+ '3' => 'string',
276
+ '4' => 'boolean'
277
+ }
278
+ puts 'What is the type of variable? (1/2/3/4)'
279
+ puts "1.\t integer"
280
+ puts "2.\t float"
281
+ puts "3.\t string"
282
+ puts "4.\t boolean"
283
+ puts "Or enter 'q' to cancle!"
284
+
285
+ loop do
286
+ type = $stdin.gets.chomp
287
+ return types[type] if %w[1 2 3 4].include?(type)
288
+
289
+ exit if type == 'q'
290
+
291
+ puts 'Just "1", "2", "3", "4" or "q" to cancel!'
292
+ end
293
+ end
294
+
295
+ private_class_method def self.type_coercion(value, type)
296
+ unless %w[integer float string boolean].include?(type)
297
+ puts "Variable's type must be 'interger', 'float', 'string' or 'boolean'!"
298
+ exit
299
+ end
300
+
301
+ type = define_type_new_variable if type.nil?
302
+
303
+ case type
304
+ when 'integer'
305
+ value.to_i
306
+ when 'float'
307
+ value.to_f
308
+ when 'string'
309
+ value
310
+ when 'boolean'
311
+ (value == 'true')
312
+ end
259
313
  end
260
314
 
261
- def self.update_variable(key, env = nil, add_variable = false)
315
+ def self.create_with_value(key, new_value, env = nil, type = nil)
262
316
  load_curr_opt unless @opt
263
317
  if @opt == 1
264
318
  puts 'Only for option 2!'
265
319
  return
266
320
  end
321
+
322
+ new_value = type_coercion(new_value, type)
323
+
267
324
  tail_msg = env ? " in '#{env}' environment" : nil
268
325
 
269
326
  value = secrets(env)
270
- if add_variable && value.key?(key)
327
+
328
+ if value.key?(key)
271
329
  puts "Key existed#{tail_msg}!"
272
330
  return
273
331
  end
274
332
 
275
- if !value.key?(key) && !add_variable
276
- puts "'#{key}' does not exist#{tail_msg}. You want to add '#{key}' as the new key? (y/n)"
333
+ value[key] = new_value
334
+ encrypt(value.to_hash.to_yaml, env || current_env)
335
+ @decrypted = nil
336
+ puts "#{key}\t=>\t#{value[key]}"
337
+ end
338
+
339
+ def self.create(key, env = nil, is_edit = false)
340
+ load_curr_opt unless @opt
341
+ if @opt == 1
342
+ puts 'Only for option 2!'
343
+ return
344
+ end
345
+ tail_msg = env ? " in '#{env}' environment" : nil
346
+
347
+ value = secrets(env)
348
+
349
+ if !is_edit && value.key?(key)
350
+ puts "Key existed#{tail_msg}!"
351
+ return
352
+ end
353
+
354
+ if !value.key?(key) && is_edit
355
+ puts "'#{key}' does not exist#{tail_msg}. You want to create '#{key}' as the new key? (y/n)"
277
356
  a = $stdin.gets.chomp
278
357
  return unless a == 'y'
279
358
 
280
- add_variable = true
359
+ is_edit = false
281
360
  end
282
361
 
283
- action = add_variable && 'add' || 'edit'
362
+ action = is_edit && 'edit' || 'create'
284
363
  file_name = env ? "#{action}_#{key}_#{env}" : "#{action}_#{key}"
285
364
 
286
365
  Tempfile.create(file_name) do |f|
@@ -298,6 +377,15 @@ class EncryptEnv
298
377
 
299
378
  puts "#{key}\t=>\t#{value[key]}"
300
379
  end
380
+
381
+ # def self.get
382
+ # self
383
+ # end
384
+
385
+ def self.method_missing(key, *_args)
386
+ secrets unless @decrypted
387
+ @decrypted[key]
388
+ end
301
389
  end
302
390
  # rubocop:enable Metrics/ClassLength
303
391
  # rubocop:enable Metrics/MethodLength
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encrypt_env
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nhu Tan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-12 00:00:00.000000000 Z
11
+ date: 2022-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print