eac_cli 0.16.3 → 0.17.0

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: 30d359d444a24b0a8f618b39e2630c1272b8e7eb372c5785641f3a64931cff93
4
- data.tar.gz: 0e1391a2382ead2b2d2e3d15d0fc036cfb88f6bde71dac7fdf14bcd633cd57bf
3
+ metadata.gz: be83e696754b3da778f4f01601dab1b68e6dc34248bfa70f3fc669ad102d7941
4
+ data.tar.gz: de452af57c0c3b0609884f82c1aed57878cae0df13f9c1628ded0b2918d20746
5
5
  SHA512:
6
- metadata.gz: 0a2bb5941764c0c3172edec4468ec9c31c747eb6fdc899575504c43bd6354d361c33808af41b6dd6019e9c6331ffa2a96ec950bf71f33c0ec48d1c8433e7b194
7
- data.tar.gz: bf0c7409ebb3db51fad860006e221730032da4c916d754fda5ace314f1a4d4f2894dbc3ad6a533584336cc039e6e775b09b746c74bd4f7aec79bb850ccb67224
6
+ metadata.gz: da02c87bffd576e6abe4dc4c143c5ba89a1e5eadf9215b8665e993ff5413eedc345ed8444e2fc9a549f4af757fb0368fe97d15999f65753fde62479ae70ebef6
7
+ data.tar.gz: b865a33fe9660b7eaa4fc118985ef34ae289bf5593f9799e4b8274ec8694b5d5a32c895f9a30a35d74d8296c410ca6e144cc97c3a232abe18d3d6876a3f0ccb9
@@ -7,7 +7,7 @@ module EacCli
7
7
  module DefaultRunner
8
8
  common_concern do
9
9
  include ::EacCli::RunnerWith::Help
10
- enable_console_speaker
10
+ enable_speaker
11
11
  enable_simple_cache
12
12
  end
13
13
  end
@@ -27,7 +27,7 @@ module EacCli
27
27
 
28
28
  def positional_argument(positional)
29
29
  if positional.subcommand?
30
- ::EacRubyUtils::Console::DocoptRunner::SUBCOMMANDS_MACRO
30
+ ::EacCli::DocoptRunner::SUBCOMMANDS_MACRO
31
31
  else
32
32
  r = "<#{positional.name}>"
33
33
  r += '...' if positional.repeat?
@@ -52,7 +52,7 @@ module EacCli
52
52
  end
53
53
 
54
54
  def self_usage_arguments
55
- [::EacRubyUtils::Console::DocoptRunner::PROGRAM_MACRO] +
55
+ [::EacCli::DocoptRunner::PROGRAM_MACRO] +
56
56
  definition.options_argument.if_present([]) { |_v| ['[options]'] } +
57
57
  self_usage_arguments_options +
58
58
  self_usage_arguments_positional
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/core_ext'
4
- require 'eac_ruby_utils/console/docopt_runner'
4
+ require 'eac_cli/docopt_runner'
5
5
 
6
6
  module EacCli
7
7
  module Docopt
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'eac_ruby_utils/core_ext'
4
- require 'eac_ruby_utils/console/docopt_runner'
4
+ require 'eac_cli/docopt_runner'
5
5
 
6
6
  module EacCli
7
7
  module Docopt
@@ -11,7 +11,7 @@ module EacCli
11
11
 
12
12
  def to_s
13
13
  (
14
- [::EacRubyUtils::Console::DocoptRunner::PROGRAM_MACRO] +
14
+ [::EacCli::DocoptRunner::PROGRAM_MACRO] +
15
15
  alternative.options_argument?.if_present([]) { |_v| ['[options]'] } +
16
16
  options +
17
17
  positionals
@@ -36,7 +36,7 @@ module EacCli
36
36
 
37
37
  def positional(positional)
38
38
  if positional.subcommand?
39
- ::EacRubyUtils::Console::DocoptRunner::SUBCOMMANDS_MACRO
39
+ ::EacCli::DocoptRunner::SUBCOMMANDS_MACRO
40
40
  else
41
41
  r = "<#{positional.name}>"
42
42
  r += '...' if positional.repeat?
@@ -3,7 +3,7 @@
3
3
  require 'eac_cli/docopt/doc_builder'
4
4
  require 'eac_cli/docopt/runner_context_replacement'
5
5
  require 'eac_cli/runner'
6
- require 'eac_ruby_utils/console/docopt_runner'
6
+ require 'eac_cli/docopt_runner'
7
7
 
8
8
  module EacCli
9
9
  module Docopt
@@ -16,7 +16,7 @@ module EacCli
16
16
 
17
17
  class << self
18
18
  def check(klass)
19
- return unless klass < ::EacRubyUtils::Console::DocoptRunner
19
+ return unless klass < ::EacCli::DocoptRunner
20
20
 
21
21
  ::EacCli::Runner.alias_runner_class_methods(klass, '', 'eac_cli')
22
22
  ::EacCli::Runner.alias_runner_class_methods(klass, 'original', '')
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash/indifferent_access'
4
+ require 'active_support/core_ext/hash/slice'
5
+ require 'docopt'
6
+ require 'eac_ruby_utils/contextualizable'
7
+ require 'eac_ruby_utils/patches/hash/sym_keys_hash'
8
+ Dir["#{__dir__}/#{::File.basename(__FILE__, '.*')}/_*.rb"].sort.each do |partial|
9
+ require partial
10
+ end
11
+
12
+ module EacCli
13
+ class DocoptRunner
14
+ include ::EacRubyUtils::Contextualizable
15
+
16
+ class << self
17
+ def create(settings = {})
18
+ new(settings)
19
+ end
20
+ end
21
+
22
+ attr_reader :settings
23
+
24
+ def initialize(settings = {})
25
+ @settings = settings.with_indifferent_access.freeze
26
+ check_subcommands
27
+ end
28
+
29
+ def options
30
+ @options ||= ::Docopt.docopt(target_doc, docopt_options)
31
+ end
32
+
33
+ def parent
34
+ settings[:parent]
35
+ end
36
+
37
+ protected
38
+
39
+ def docopt_options
40
+ settings.slice(:version, :argv, :help, :options_first).to_sym_keys_hash
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ class DocoptRunner
5
+ DOCOPT_ERROR_EXIT_CODE = 0xC0
6
+
7
+ class << self
8
+ def run(options = {})
9
+ create(options).send(:run)
10
+ rescue Docopt::Exit => e
11
+ STDERR.write(e.message + "\n")
12
+ ::Kernel.exit(DOCOPT_ERROR_EXIT_CODE)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ class DocoptRunner
5
+ PROGRAM_MACRO = '__PROGRAM__'
6
+
7
+ def source_doc
8
+ setting_value(:doc)
9
+ end
10
+
11
+ def target_doc
12
+ source_doc.gsub(PROGRAM_MACRO, target_program_name).strip + "\n"
13
+ end
14
+
15
+ def source_program_name
16
+ setting_value(:program_name, false)
17
+ end
18
+
19
+ def target_program_name
20
+ [source_program_name, ENV['PROGRAM_NAME'], $PROGRAM_NAME].find(&:present?)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/settings_provider'
4
+
5
+ module EacCli
6
+ class DocoptRunner
7
+ include ::EacRubyUtils::SettingsProvider
8
+
9
+ attr_reader :settings
10
+
11
+ private
12
+
13
+ def setting_value(key, required = true)
14
+ super(key, required: required, order: %w[method settings_object constant])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string/inflections'
4
+ require 'shellwords'
5
+
6
+ module EacCli
7
+ class DocoptRunner
8
+ SUBCOMMAND_ARG = '<subcommand>'
9
+ SUBCOMMAND_ARGS_ARG = '<subcommand-args>'
10
+ SUBCOMMANDS_MACRO = '__SUBCOMMANDS__'
11
+
12
+ def subcommands?
13
+ source_doc.include?(SUBCOMMANDS_MACRO)
14
+ end
15
+
16
+ def check_subcommands
17
+ return unless subcommands?
18
+
19
+ singleton_class.include(SubcommandsSupport)
20
+ check_subcommands_arg
21
+ return if singleton_class.method_defined?(:run)
22
+
23
+ singleton_class.send(:alias_method, :run, :run_with_subcommand)
24
+ end
25
+
26
+ module SubcommandsSupport
27
+ EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME = :extra_available_subcommands
28
+
29
+ def check_subcommands_arg
30
+ if subcommand_arg_as_list?
31
+ singleton_class.include(SubcommandsSupport::SubcommandArgAsList)
32
+ else
33
+ singleton_class.include(SubcommandsSupport::SubcommandArgAsArg)
34
+ end
35
+ end
36
+
37
+ def run_with_subcommand
38
+ if subcommand_name
39
+ check_valid_subcommand
40
+ subcommand_run
41
+ else
42
+ run_without_subcommand
43
+ end
44
+ end
45
+
46
+ def subcommand
47
+ @subcommand ||= subcommand_class_name(subcommand_name).constantize.create(
48
+ argv: subcommand_args,
49
+ program_name: subcommand_program,
50
+ parent: self
51
+ )
52
+ end
53
+
54
+ def subcommand_run
55
+ if !subcommand.is_a?(::EacCli::DocoptRunner) &&
56
+ subcommand.respond_to?(:run_run)
57
+ subcommand.run_run
58
+ else
59
+ subcommand.run
60
+ end
61
+ end
62
+
63
+ def target_doc
64
+ super.gsub(SUBCOMMANDS_MACRO,
65
+ "#{target_doc_subcommand_arg} [#{SUBCOMMAND_ARGS_ARG}...]") +
66
+ "\n" + subcommands_target_doc
67
+ end
68
+
69
+ def docopt_options
70
+ super.merge(options_first: true)
71
+ end
72
+
73
+ def subcommand_class_name(subcommand)
74
+ "#{self.class.name}::#{subcommand.underscore.camelize}"
75
+ end
76
+
77
+ def subcommand_arg_as_list?
78
+ setting_value(:subcommand_arg_as_list, false) || false
79
+ end
80
+
81
+ def subcommand_args
82
+ options.fetch(SUBCOMMAND_ARGS_ARG)
83
+ end
84
+
85
+ def subcommand_program
86
+ subcommand_name
87
+ end
88
+
89
+ def available_subcommands
90
+ r = ::Set.new(setting_value(:subcommands, false) || auto_available_subcommands)
91
+ if respond_to?(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME, true)
92
+ r += send(EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME)
93
+ end
94
+ r.sort
95
+ end
96
+
97
+ def auto_available_subcommands
98
+ self.class.constants
99
+ .map { |name| self.class.const_get(name) }
100
+ .select { |c| c.instance_of? Class }
101
+ .select { |c| c < ::EacCli::DocoptRunner }
102
+ .map { |c| c.name.demodulize.underscore.dasherize }
103
+ end
104
+
105
+ def run_without_subcommand
106
+ "Method #{__method__} should be overrided in #{self.class.name}"
107
+ end
108
+
109
+ protected
110
+
111
+ def check_valid_subcommand
112
+ return if available_subcommands.include?(subcommand_name)
113
+
114
+ raise ::Docopt::Exit, "\"#{subcommand_name}\" is not a valid subcommand" \
115
+ " (Valid: #{available_subcommands.join(', ')})"
116
+ end
117
+
118
+ module SubcommandArgAsArg
119
+ def target_doc_subcommand_arg
120
+ SUBCOMMAND_ARG
121
+ end
122
+
123
+ def subcommand_name
124
+ options.fetch(SUBCOMMAND_ARG)
125
+ end
126
+
127
+ def subcommands_target_doc
128
+ available_subcommands.inject("Subcommands:\n") do |a, e|
129
+ a + " #{e}\n"
130
+ end
131
+ end
132
+ end
133
+
134
+ module SubcommandArgAsList
135
+ def target_doc_subcommand_arg
136
+ '(' + available_subcommands.join('|') + ')'
137
+ end
138
+
139
+ def subcommand_name
140
+ available_subcommands.each do |subcommand|
141
+ return subcommand if options.fetch(subcommand)
142
+ end
143
+ nil
144
+ end
145
+
146
+ def subcommands_target_doc
147
+ "\n"
148
+ end
149
+ end
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+ require 'eac_cli/patches/module/speaker'
5
+
6
+ module EacCli
7
+ class OldConfigs
8
+ require_sub __FILE__
9
+ enable_speaker
10
+
11
+ class << self
12
+ def entry_key_to_envvar_name(entry_key)
13
+ ::EacCli::OldConfigs::EntryReader.entry_key_to_envvar_name(entry_key)
14
+ end
15
+ end
16
+
17
+ attr_reader :configs
18
+
19
+ def initialize(configs_key, options = {})
20
+ options.assert_argument(::Hash, 'options')
21
+ @configs = ::EacConfig::OldConfigs.new(configs_key, options.merge(autosave: true))
22
+ end
23
+
24
+ def read_password(entry_key, options = {})
25
+ ::EacCli::OldConfigs::PasswordEntryReader.new(self, entry_key, options).read
26
+ end
27
+
28
+ def read_entry(entry_key, options = {})
29
+ ::EacCli::OldConfigs::EntryReader.new(self, entry_key, options).read
30
+ end
31
+
32
+ def store_passwords?
33
+ ::EacCli::OldConfigs::StorePasswordsEntryReader.new(self) == 'yes'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/old_configs/read_entry_options'
4
+ require 'eac_config/paths_hash'
5
+ require 'eac_ruby_utils/core_ext'
6
+
7
+ module EacCli
8
+ class OldConfigs
9
+ class EntryReader
10
+ enable_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
+ ::EacConfig::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 = ::EacCli::OldConfigs::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
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/old_configs/entry_reader'
4
+
5
+ module EacCli
6
+ class OldConfigs
7
+ class PasswordEntryReader < ::EacCli::OldConfigs::EntryReader
8
+ ENTRY_KEY = 'core.store_passwords'
9
+
10
+ def initialize(console_configs, entry_key, options = {})
11
+ super(console_configs, entry_key, options.merge(noecho: true,
12
+ store: console_configs.store_passwords?))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacCli
6
+ class OldConfigs
7
+ class ReadEntryOptions
8
+ enable_simple_cache
9
+ common_constructor :options do
10
+ self.options = options.to_h.symbolize_keys
11
+ .assert_valid_keys(DEFAULT_VALUES.keys).freeze
12
+ end
13
+
14
+ DEFAULT_VALUES = {
15
+ before_input: nil, bool: false, list: false, noecho: false, noenv: false, noinput: false,
16
+ required: true, store: true, validator: nil
17
+ }.freeze
18
+
19
+ delegate :to_h, to: :options
20
+
21
+ def [](key)
22
+ values.fetch(key.to_sym)
23
+ end
24
+
25
+ def request_input_options
26
+ values.slice(:bool, :list, :noecho)
27
+ end
28
+
29
+ private
30
+
31
+ def values_uncached
32
+ consumer = options.to_options_consumer
33
+ r = {}
34
+ DEFAULT_VALUES.each do |key, default_value|
35
+ value = consumer.consume(key)
36
+ value = default_value if value.nil?
37
+ r[key] = value
38
+ end
39
+ consumer.validate
40
+ r
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/old_configs/entry_reader'
4
+
5
+ module EacCli
6
+ class OldConfigs
7
+ class StorePasswordsEntryReader < ::EacCli::OldConfigs::EntryReader
8
+ ENTRY_KEY = 'core.store_passwords'
9
+
10
+ def initialize(console_configs)
11
+ super(console_configs, ENTRY_KEY,
12
+ before_input: -> { banner },
13
+ validator: ->(entry_value) { %w[yes no].include?(entry_value) }
14
+ )
15
+ end
16
+
17
+ def banner
18
+ infom 'Do you wanna to store passwords?'
19
+ infom 'Warning: the passwords will be store in clear text in ' \
20
+ "\"#{console_configs.configs.storage_path}\""
21
+ infom 'Enter "yes" or "no"'
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+ ::EacRubyUtils.require_sub(__FILE__)
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/speaker'
4
+ require 'eac_ruby_utils/patch'
5
+
6
+ class Module
7
+ def enable_speaker
8
+ ::EacRubyUtils.patch(self, ::EacCli::Speaker)
9
+ end
10
+ end
@@ -9,7 +9,7 @@ class Object
9
9
  def runner_with(*runners, &block)
10
10
  include ::EacCli::Runner
11
11
  enable_simple_cache
12
- enable_console_speaker
12
+ enable_speaker
13
13
  runners.each do |runner|
14
14
  include ::EacCli::RunnerWithSet.default.item_to_module(runner)
15
15
  end
@@ -6,16 +6,20 @@ require 'eac_ruby_utils/core_ext'
6
6
  module EacCli
7
7
  module RunnerWith
8
8
  module Subcommands
9
+ require_sub __FILE__
10
+
9
11
  class << self
10
12
  def runner?(object)
11
13
  ::EacCli::Runner.runner?(object) || (
12
- object.is_a?(::Class) && object < ::EacRubyUtils::Console::DocoptRunner
14
+ object.is_a?(::Class) && object < ::EacCli::DocoptRunner
13
15
  )
14
16
  end
15
17
  end
16
18
 
17
19
  common_concern do
18
20
  include ::EacCli::Runner
21
+ runner_definition.singleton_class
22
+ .include(::EacCli::RunnerWith::Subcommands::DefinitionConcern)
19
23
  end
20
24
 
21
25
  EXTRA_AVAILABLE_SUBCOMMANDS_METHOD_NAME = :extra_available_subcommands
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacCli
4
+ module RunnerWith
5
+ module Subcommands
6
+ module DefinitionConcern
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'colorize'
4
+ require 'io/console'
5
+ require 'eac_ruby_utils/patches/hash/options_consumer'
6
+ require 'eac_ruby_utils/require_sub'
7
+ ::EacRubyUtils.require_sub __FILE__
8
+
9
+ module EacCli
10
+ # https://github.com/fazibear/colorize
11
+ module Speaker
12
+ def on_speaker_node(&block)
13
+ ::EacCli::Speaker.on_node(&block)
14
+ end
15
+
16
+ def puts(string = '')
17
+ string.to_s.each_line do |line|
18
+ current_node.stderr.puts(current_node.stderr_line_prefix.to_s + line)
19
+ end
20
+ end
21
+
22
+ def out(string = '')
23
+ current_node.stdout.write(string.to_s)
24
+ end
25
+
26
+ def fatal_error(string)
27
+ puts "ERROR: #{string}".white.on_red
28
+ Kernel.exit 1
29
+ end
30
+
31
+ def title(string)
32
+ string = string.to_s
33
+ puts(('-' * (8 + string.length)).green)
34
+ puts("--- #{string} ---".green)
35
+ puts(('-' * (8 + string.length)).green)
36
+ puts
37
+ end
38
+
39
+ def info(string)
40
+ puts string.to_s.white
41
+ end
42
+
43
+ def infom(string)
44
+ puts string.to_s.light_yellow
45
+ end
46
+
47
+ def warn(string)
48
+ puts string.to_s.yellow
49
+ end
50
+
51
+ # Options:
52
+ # +bool+ ([Boolean], default: +false+): requires a answer "yes" or "no".
53
+ # +list+ ([Hash] or [Array], default: +nil+): requires a answer from a list.
54
+ # +noecho+ ([Boolean], default: +false+): does not output answer.
55
+ def request_input(question, options = {})
56
+ bool, list, noecho = options.to_options_consumer.consume_all(:bool, :list, :noecho)
57
+ if list
58
+ request_from_list(question, list, noecho)
59
+ elsif bool
60
+ request_from_bool(question, noecho)
61
+ else
62
+ request_string(question, noecho)
63
+ end
64
+ end
65
+
66
+ def infov(*args)
67
+ r = []
68
+ args.each_with_index do |v, i|
69
+ if i.even?
70
+ r << "#{v}: ".cyan
71
+ else
72
+ r.last << v.to_s
73
+ end
74
+ end
75
+ puts r.join(', ')
76
+ end
77
+
78
+ def success(string)
79
+ puts string.to_s.green
80
+ end
81
+
82
+ private
83
+
84
+ def list_value(list, input)
85
+ values = list_values(list)
86
+ return input, true unless values
87
+ return input, false unless values.include?(input)
88
+ end
89
+
90
+ def list_values(list)
91
+ if list.is_a?(::Hash)
92
+ list.keys.map(&:to_s)
93
+ elsif list.is_a?(::Array)
94
+ list.map(&:to_s)
95
+ end
96
+ end
97
+
98
+ def request_from_bool(question, noecho)
99
+ request_from_list(question, { yes: true, y: true, no: false, n: false }, noecho)
100
+ end
101
+
102
+ def request_from_list(question, list, noecho)
103
+ list = ::EacCli::Speaker::List.build(list)
104
+ loop do
105
+ input = request_string("#{question} [#{list.valid_labels.join('/')}]", noecho)
106
+ return list.build_value(input) if list.valid_value?(input)
107
+
108
+ warn "Invalid input: \"#{input}\" (Valid: #{list.valid_labels.join(', ')})"
109
+ end
110
+ end
111
+
112
+ def request_string(question, noecho)
113
+ current_node.stderr.write "#{question}: ".to_s.yellow
114
+ noecho ? request_string_noecho : request_string_echo
115
+ end
116
+
117
+ def request_string_noecho
118
+ r = current_node.stdin.noecho(&:gets).chomp.strip
119
+ current_node.stderr.write("\n")
120
+ r
121
+ end
122
+
123
+ def request_string_echo
124
+ current_node.stdin.gets.chomp.strip
125
+ end
126
+
127
+ def current_node
128
+ ::EacCli::Speaker.current_node
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_cli/speaker/node'
4
+
5
+ module EacCli
6
+ module Speaker
7
+ class << self
8
+ def current_node
9
+ nodes_stack.last
10
+ end
11
+
12
+ def on_node(&block)
13
+ push
14
+ yield(*(block.arity.zero? ? [] : [current_node]))
15
+ ensure
16
+ pop
17
+ end
18
+
19
+ def push
20
+ nodes_stack << ::EacCli::Speaker::Node.new
21
+ current_node
22
+ end
23
+
24
+ def pop
25
+ return nodes_stack.pop if nodes_stack.count > 1
26
+
27
+ raise "Cannot remove first node (nodes_stack.count: #{nodes_stack.count})"
28
+ end
29
+
30
+ private
31
+
32
+ def nodes_stack
33
+ @nodes_stack ||= [::EacCli::Speaker::Node.new]
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/by_reference'
4
+
5
+ module EacCli
6
+ # https://github.com/fazibear/colorize
7
+ module Speaker
8
+ STDERR = ::EacRubyUtils::ByReference.new { $stderr }
9
+ STDIN = ::EacRubyUtils::ByReference.new { $stdin }
10
+ STDOUT = ::EacRubyUtils::ByReference.new { $stdout }
11
+ end
12
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/hash_with_indifferent_access'
4
+ require 'ostruct'
5
+
6
+ module EacCli
7
+ module Speaker
8
+ class List
9
+ class << self
10
+ def build(list)
11
+ return List.new(hash_to_values(list)) if list.is_a?(::Hash)
12
+ return List.new(array_to_values(list)) if list.is_a?(::Array)
13
+
14
+ raise "Invalid list: #{list} (#{list.class})"
15
+ end
16
+
17
+ private
18
+
19
+ def hash_to_values(list)
20
+ list.map { |key, value| ::OpenStruct.new(key: key, label: key, value: value) }
21
+ end
22
+
23
+ def array_to_values(list)
24
+ list.map { |value| ::OpenStruct.new(key: value, label: value, value: value) }
25
+ end
26
+ end
27
+
28
+ attr_reader :values
29
+
30
+ def initialize(values)
31
+ @values = values.map do |v|
32
+ ::OpenStruct.new(key: to_key(v.key), label: to_label(v.label), value: v.value)
33
+ end
34
+ end
35
+
36
+ def valid_labels
37
+ values.map(&:label)
38
+ end
39
+
40
+ def valid_value?(value)
41
+ values.any? { |v| v.key == to_key(value) }
42
+ end
43
+
44
+ def to_key(value)
45
+ to_label(value).downcase
46
+ end
47
+
48
+ def to_label(value)
49
+ value.to_s.strip
50
+ end
51
+
52
+ def build_value(value)
53
+ key = to_key(value)
54
+ values.each do |v| # rubocop:disable Style/HashEachMethods
55
+ return v.value if v.key == key
56
+ end
57
+ raise "Value not found: \"#{value}\" (#{values})"
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/patches/object/if_present'
4
+ require 'eac_cli/speaker/_constants'
5
+
6
+ module EacCli
7
+ module Speaker
8
+ class Node
9
+ attr_accessor :stdin, :stdout, :stderr, :stderr_line_prefix
10
+
11
+ def initialize(parent = nil)
12
+ self.stdin = parent.if_present(::EacCli::Speaker::STDIN, &:stdin)
13
+ self.stdout = parent.if_present(::EacCli::Speaker::STDOUT, &:stdout)
14
+ self.stderr = parent.if_present(::EacCli::Speaker::STDERR, &:stderr)
15
+ self.stderr_line_prefix = parent.if_present('', &:stderr_line_prefix)
16
+ end
17
+
18
+ def configure
19
+ yield(self)
20
+ self
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacCli
4
- VERSION = '0.16.3'
4
+ VERSION = '0.17.0'
5
5
  end
metadata CHANGED
@@ -1,29 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.3
4
+ version: 0.17.0
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: 2021-05-06 00:00:00.000000000 Z
11
+ date: 2021-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: docopt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: eac_config
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
13
55
  - !ruby/object:Gem::Dependency
14
56
  name: eac_ruby_utils
15
57
  requirement: !ruby/object:Gem::Requirement
16
58
  requirements:
17
59
  - - "~>"
18
60
  - !ruby/object:Gem::Version
19
- version: '0.55'
61
+ version: '0.64'
20
62
  type: :runtime
21
63
  prerelease: false
22
64
  version_requirements: !ruby/object:Gem::Requirement
23
65
  requirements:
24
66
  - - "~>"
25
67
  - !ruby/object:Gem::Version
26
- version: '0.55'
68
+ version: '0.64'
27
69
  - !ruby/object:Gem::Dependency
28
70
  name: eac_ruby_gem_support
29
71
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +102,16 @@ files:
60
102
  - lib/eac_cli/docopt/doc_builder/alternative.rb
61
103
  - lib/eac_cli/docopt/runner_context_replacement.rb
62
104
  - lib/eac_cli/docopt/runner_extension.rb
105
+ - lib/eac_cli/docopt_runner.rb
106
+ - lib/eac_cli/docopt_runner/_class_methods.rb
107
+ - lib/eac_cli/docopt_runner/_doc.rb
108
+ - lib/eac_cli/docopt_runner/_settings.rb
109
+ - lib/eac_cli/docopt_runner/_subcommands.rb
110
+ - lib/eac_cli/old_configs.rb
111
+ - lib/eac_cli/old_configs/entry_reader.rb
112
+ - lib/eac_cli/old_configs/password_entry_reader.rb
113
+ - lib/eac_cli/old_configs/read_entry_options.rb
114
+ - lib/eac_cli/old_configs/store_passwords_entry_reader.rb
63
115
  - lib/eac_cli/parser.rb
64
116
  - lib/eac_cli/parser/alternative.rb
65
117
  - lib/eac_cli/parser/alternative/argv.rb
@@ -72,6 +124,8 @@ files:
72
124
  - lib/eac_cli/parser/collector.rb
73
125
  - lib/eac_cli/parser/error.rb
74
126
  - lib/eac_cli/patches.rb
127
+ - lib/eac_cli/patches/module.rb
128
+ - lib/eac_cli/patches/module/speaker.rb
75
129
  - lib/eac_cli/patches/object.rb
76
130
  - lib/eac_cli/patches/object/runner_with.rb
77
131
  - lib/eac_cli/runner.rb
@@ -82,7 +136,13 @@ files:
82
136
  - lib/eac_cli/runner_with.rb
83
137
  - lib/eac_cli/runner_with/help.rb
84
138
  - lib/eac_cli/runner_with/subcommands.rb
139
+ - lib/eac_cli/runner_with/subcommands/definition_concern.rb
85
140
  - lib/eac_cli/runner_with_set.rb
141
+ - lib/eac_cli/speaker.rb
142
+ - lib/eac_cli/speaker/_class_methods.rb
143
+ - lib/eac_cli/speaker/_constants.rb
144
+ - lib/eac_cli/speaker/list.rb
145
+ - lib/eac_cli/speaker/node.rb
86
146
  - lib/eac_cli/version.rb
87
147
  homepage:
88
148
  licenses: []