eac_ruby_utils 0.51.0 → 0.51.1

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
  SHA256:
3
- metadata.gz: db10c1ceaf0bbf46f4d4bb60de583c5bd0e9dcb4d9e836919ba4cbb3da526583
4
- data.tar.gz: 9f84d861c34de8496ff30bb5bc14bf376bfba47a81c7ace9e8e51e006d44f4de
3
+ metadata.gz: 247be76881888a818e9c8d0c5970ee9068b8fe1edade1d73855916581279b5fc
4
+ data.tar.gz: ca9aaf7f3c0c82f26a3a44b50844f0326c06855b0163b60c9238aaf450f235b9
5
5
  SHA512:
6
- metadata.gz: c61d5b10f2981624324aaa7cee8f1c554e1521814fd158f43701d42391fcb4f29b8e2b55b799f887f1434ee47ea032108ae96136fd8200fde7fbe8181fe8efab
7
- data.tar.gz: 3339282f35c34c843b500a0d37a025a6f53be072e495771d6262d08fda8c3957dad6feedbc82d0b6bfdb1eeebc1cc8a6f9c183f5071f1e90cd205a9c79404e71
6
+ metadata.gz: ccb0243f0fa95ae62218ffdb260a78e998d748fd879cf20424465c94389a91038ed704d4073fa9b1875622bd7d38a995de3110209d1c1bba32a58df4cd6ba74a
7
+ data.tar.gz: 65d8e17a1e7b2d10ddb49b0404c6e4a9176e227c31d70dd0e3af49254ecc6bc3a39052420e580bc5c70a18ad63e0a0d88c049c69f22bf76898b221efcda14a87
@@ -11,18 +11,10 @@ module EacRubyUtils
11
11
 
12
12
  class << self
13
13
  def entry_key_to_envvar_name(entry_key)
14
- path = if entry_key.is_a?(::Array)
15
- entry_key
16
- else
17
- ::EacRubyUtils::PathsHash.parse_entry_key(entry_key)
18
- end
19
- path.join('_').gsub(/[^a-z0-9_]/i, '').gsub(/\A_+/, '').gsub(/_+\z/, '')
20
- .gsub(/_{2,}/, '_').upcase
14
+ ::EacRubyUtils::Console::Configs::EntryReader.entry_key_to_envvar_name(entry_key)
21
15
  end
22
16
  end
23
17
 
24
- STORE_PASSWORDS_KEY = 'core.store_passwords'
25
-
26
18
  attr_reader :configs
27
19
 
28
20
  def initialize(configs_key, options = {})
@@ -31,72 +23,15 @@ module EacRubyUtils
31
23
  end
32
24
 
33
25
  def read_password(entry_key, options = {})
34
- options = ReadEntryOptions.new(options.merge(noecho: true))
35
- if store_passwords?
36
- read_entry(entry_key, options)
37
- else
38
- looped_entry_value_from_input(entry_key, options)
39
- end
26
+ ::EacRubyUtils::Console::Configs::PasswordEntryReader.new(self, entry_key, options).read
40
27
  end
41
28
 
42
29
  def read_entry(entry_key, options = {})
43
- options = ReadEntryOptions.new(options)
44
- unless options[:noenv]
45
- envvar_value = envvar_read_entry(entry_key)
46
- return envvar_value if envvar_value.present?
47
- end
48
- stored_value = configs.read_entry(entry_key)
49
- return stored_value if stored_value
50
- return read_entry_from_console(entry_key, options) unless options[:noinput]
51
-
52
- raise "No value found for entry \"#{entry_key}\"" if options[:required]
30
+ ::EacRubyUtils::Console::Configs::EntryReader.new(self, entry_key, options).read
53
31
  end
54
32
 
55
33
  def store_passwords?
56
- read_entry(
57
- STORE_PASSWORDS_KEY,
58
- before_input: -> { store_password_banner },
59
- validator: ->(entry_value) { %w[yes no].include?(entry_value) }
60
- ) == 'yes'
61
- end
62
-
63
- protected
64
-
65
- def envvar_read_entry(entry_key)
66
- env_entry_key = self.class.entry_key_to_envvar_name(entry_key)
67
- return nil unless ENV.key?(env_entry_key)
68
-
69
- ENV.fetch(env_entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
70
- end
71
-
72
- def read_entry_from_console(entry_key, options)
73
- options[:before_input].call if options[:before_input].present?
74
- entry_value = looped_entry_value_from_input(entry_key, options)
75
- configs.write_entry(entry_key, entry_value)
76
- entry_value
77
- end
78
-
79
- def store_password_banner
80
- infom 'Do you wanna to store passwords?'
81
- infom "Warning: the passwords will be store in clear text in \"#{configs.storage_path}\""
82
- infom 'Enter "yes" or "no"'
83
- end
84
-
85
- def looped_entry_value_from_input(entry_key, options)
86
- loop do
87
- entry_value = entry_value_from_input(entry_key, options)
88
- next if entry_value.blank?
89
- next if options[:validator] && !options[:validator].call(entry_value)
90
-
91
- return entry_value
92
- end
93
- end
94
-
95
- def entry_value_from_input(entry_key, options)
96
- entry_value = request_input("Value for entry \"#{entry_key}\"",
97
- options.request_input_options)
98
- warn('Entered value is blank') if entry_value.blank?
99
- entry_value
34
+ ::EacRubyUtils::Console::Configs::StorePasswordsEntryReader.new(self) == 'yes'
100
35
  end
101
36
  end
102
37
  end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/configs/read_entry_options'
4
+ require 'eac_ruby_utils/core_ext'
5
+
6
+ module EacRubyUtils
7
+ module Console
8
+ class Configs
9
+ class EntryReader
10
+ enable_console_speaker
11
+
12
+ class << self
13
+ def entry_key_to_envvar_name(entry_key)
14
+ path = if entry_key.is_a?(::Array)
15
+ entry_key
16
+ else
17
+ ::EacRubyUtils::PathsHash.parse_entry_key(entry_key)
18
+ end
19
+ path.join('_').gsub(/[^a-z0-9_]/i, '').gsub(/\A_+/, '').gsub(/_+\z/, '')
20
+ .gsub(/_{2,}/, '_').upcase
21
+ end
22
+ end
23
+
24
+ common_constructor :console_configs, :entry_key, :options do
25
+ self.options = ::EacRubyUtils::Console::Configs::ReadEntryOptions.new(options)
26
+ end
27
+
28
+ def read
29
+ %w[envvars storage console].each do |suffix|
30
+ value = send("read_from_#{suffix}")
31
+ return value if value.present?
32
+ end
33
+ return nil unless options[:required]
34
+
35
+ raise "No value found for entry \"#{entry_key}\""
36
+ end
37
+
38
+ def read_from_storage
39
+ console_configs.configs.read_entry(entry_key)
40
+ end
41
+
42
+ def read_from_envvars
43
+ return if options[:noenv]
44
+
45
+ env_entry_key = self.class.entry_key_to_envvar_name(entry_key)
46
+ return unless ENV.key?(env_entry_key)
47
+
48
+ ENV.fetch(env_entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
49
+ end
50
+
51
+ def read_from_console
52
+ return if options[:noinput]
53
+
54
+ options[:before_input].if_present(&:call)
55
+ entry_value = looped_entry_value_from_input
56
+ console_configs.configs.write_entry(entry_key, entry_value) if options[:store]
57
+ entry_value
58
+ end
59
+
60
+ private
61
+
62
+ def looped_entry_value_from_input
63
+ loop do
64
+ entry_value = entry_value_from_input(entry_key, options)
65
+ next if entry_value.blank?
66
+ next if options[:validator] && !options[:validator].call(entry_value)
67
+
68
+ return entry_value
69
+ end
70
+ end
71
+
72
+ def entry_value_from_input(entry_key, options)
73
+ entry_value = request_input("Value for entry \"#{entry_key}\"",
74
+ options.request_input_options)
75
+ warn('Entered value is blank') if entry_value.blank?
76
+ entry_value
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/configs/entry_reader'
4
+
5
+ module EacRubyUtils
6
+ module Console
7
+ class Configs
8
+ class PasswordEntryReader < ::EacRubyUtils::Console::Configs::EntryReader
9
+ ENTRY_KEY = 'core.store_passwords'
10
+
11
+ def initialize(console_configs, entry_key, options = {})
12
+ super(console_configs, entry_key, options.merge(noecho: true,
13
+ store: console_configs.store_passwords?))
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -7,13 +7,18 @@ module EacRubyUtils
7
7
  class Configs
8
8
  class ReadEntryOptions
9
9
  enable_simple_cache
10
- common_constructor :options
10
+ common_constructor :options do
11
+ self.options = options.to_h.symbolize_keys
12
+ .assert_valid_keys(DEFAULT_VALUES.keys).freeze
13
+ end
11
14
 
12
15
  DEFAULT_VALUES = {
13
16
  before_input: nil, bool: false, list: false, noecho: false, noenv: false, noinput: false,
14
- required: true, validator: nil
17
+ required: true, store: true, validator: nil
15
18
  }.freeze
16
19
 
20
+ delegate :to_h, to: :options
21
+
17
22
  def [](key)
18
23
  values.fetch(key.to_sym)
19
24
  end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/console/configs/entry_reader'
4
+
5
+ module EacRubyUtils
6
+ module Console
7
+ class Configs
8
+ class StorePasswordsEntryReader < ::EacRubyUtils::Console::Configs::EntryReader
9
+ ENTRY_KEY = 'core.store_passwords'
10
+
11
+ def initialize(console_configs)
12
+ super(console_configs, ENTRY_KEY,
13
+ before_input: -> { banner },
14
+ validator: ->(entry_value) { %w[yes no].include?(entry_value) }
15
+ )
16
+ end
17
+
18
+ def banner
19
+ infom 'Do you wanna to store passwords?'
20
+ infom 'Warning: the passwords will be store in clear text in ' \
21
+ "\"#{console_configs.configs.storage_path}\""
22
+ infom 'Enter "yes" or "no"'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRubyUtils
4
- VERSION = '0.51.0'
4
+ VERSION = '0.51.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_ruby_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.51.0
4
+ version: 0.51.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esquilo Azul Company
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-16 00:00:00.000000000 Z
11
+ date: 2020-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -119,7 +119,10 @@ files:
119
119
  - lib/eac_ruby_utils/configs/file.rb
120
120
  - lib/eac_ruby_utils/console.rb
121
121
  - lib/eac_ruby_utils/console/configs.rb
122
+ - lib/eac_ruby_utils/console/configs/entry_reader.rb
123
+ - lib/eac_ruby_utils/console/configs/password_entry_reader.rb
122
124
  - lib/eac_ruby_utils/console/configs/read_entry_options.rb
125
+ - lib/eac_ruby_utils/console/configs/store_passwords_entry_reader.rb
123
126
  - lib/eac_ruby_utils/console/docopt_runner.rb
124
127
  - lib/eac_ruby_utils/console/docopt_runner/_class_methods.rb
125
128
  - lib/eac_ruby_utils/console/docopt_runner/_doc.rb