encrypt_env 1.3.2 → 1.3.5
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 +4 -4
- data/bin/encrypt_env +70 -55
- data/lib/encrypt_env/version.rb +1 -1
- data/lib/encrypt_env.rb +107 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0967afce5032fb58cb38bb051f399219769a5468be4e1a785ef603122f6d8b63'
|
4
|
+
data.tar.gz: e902376e9b700ffc88a451c6bddd1b49d5150994a835a37af1a8f3096ba94baf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4cf2f4a63f8afb1b4cce6e4ac2e1ff2886225bef2a38761f1cac5426229c765f1e10cfc406562456eec374163f57a1c8de6bf1be6b0b2516e86a207176fe051b
|
7
|
+
data.tar.gz: bf731d891797f4f1437758fbeeeb680ec0338547ec7ab13dbaf62984e1ad34e2db80ccc5fe1e8b58b2460c12a4b3e27513197d4e5a742c8caf0a707814a3e0cc
|
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
|
-
|
54
|
+
case action
|
55
|
+
when 'setup'
|
12
56
|
EncryptEnv.setup
|
13
|
-
|
14
|
-
|
15
|
-
if
|
16
|
-
EncryptEnv.show(
|
17
|
-
|
18
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
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.
|
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
|
-
|
31
|
-
|
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
|
87
|
+
exit
|
73
88
|
end
|
data/lib/encrypt_env/version.rb
CHANGED
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
|
-
|
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.
|
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,34 +261,104 @@ 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
|
+
unless %w[integer float string boolean].include?(type)
|
296
|
+
puts "Variable's type must be 'interger', 'float', 'string' or 'boolean'!"
|
297
|
+
exit
|
298
|
+
end
|
299
|
+
|
300
|
+
type = define_type_new_variable if type.nil?
|
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
|
259
312
|
end
|
260
313
|
|
261
|
-
def self.
|
314
|
+
def self.create_with_value(key, new_value, env = nil, type = nil)
|
262
315
|
load_curr_opt unless @opt
|
263
316
|
if @opt == 1
|
264
317
|
puts 'Only for option 2!'
|
265
318
|
return
|
266
319
|
end
|
320
|
+
|
321
|
+
new_value = type_coercion(new_value, type)
|
322
|
+
|
267
323
|
tail_msg = env ? " in '#{env}' environment" : nil
|
268
324
|
|
269
325
|
value = secrets(env)
|
270
|
-
|
326
|
+
|
327
|
+
if value.key?(key)
|
271
328
|
puts "Key existed#{tail_msg}!"
|
272
329
|
return
|
273
330
|
end
|
274
331
|
|
275
|
-
|
276
|
-
|
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]}"
|
336
|
+
end
|
337
|
+
|
338
|
+
def self.create(key, env = nil, is_edit = false)
|
339
|
+
load_curr_opt unless @opt
|
340
|
+
if @opt == 1
|
341
|
+
puts 'Only for option 2!'
|
342
|
+
return
|
343
|
+
end
|
344
|
+
tail_msg = env ? " in '#{env}' environment" : nil
|
345
|
+
|
346
|
+
value = secrets(env)
|
347
|
+
|
348
|
+
if !is_edit && value.key?(key)
|
349
|
+
puts "Key existed#{tail_msg}!"
|
350
|
+
return
|
351
|
+
end
|
352
|
+
|
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
|
-
|
358
|
+
is_edit = false
|
281
359
|
end
|
282
360
|
|
283
|
-
action =
|
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|
|
@@ -288,11 +366,24 @@ class EncryptEnv
|
|
288
366
|
f.flush
|
289
367
|
f.rewind
|
290
368
|
system("vim #{f.path}")
|
291
|
-
new_value = File.read(f.path)
|
292
|
-
|
369
|
+
# new_value = File.read(f.path)
|
370
|
+
new_value = YAML.load_file(f.path)
|
371
|
+
# value[key] = new_value.strip
|
372
|
+
value[key] = new_value
|
293
373
|
encrypt(value.to_hash.to_yaml, env || current_env)
|
294
374
|
@decrypted = nil
|
295
375
|
end
|
376
|
+
|
377
|
+
puts "#{key}\t=>\t#{value[key]}"
|
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]
|
296
387
|
end
|
297
388
|
end
|
298
389
|
# rubocop:enable Metrics/ClassLength
|
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.
|
4
|
+
version: 1.3.5
|
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-
|
11
|
+
date: 2022-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|