encrypt_env 1.3.3 → 1.3.6

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