eac_cli 0.15.1 → 0.17.0

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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eac_cli/default_runner.rb +1 -1
  3. data/lib/eac_cli/definition/base_option.rb +5 -1
  4. data/lib/eac_cli/definition/help_formatter.rb +2 -2
  5. data/lib/eac_cli/docopt/doc_builder.rb +4 -2
  6. data/lib/eac_cli/docopt/doc_builder/alternative.rb +3 -3
  7. data/lib/eac_cli/docopt/runner_extension.rb +2 -2
  8. data/lib/eac_cli/docopt_runner.rb +43 -0
  9. data/lib/eac_cli/docopt_runner/_class_methods.rb +16 -0
  10. data/lib/eac_cli/docopt_runner/_doc.rb +23 -0
  11. data/lib/eac_cli/docopt_runner/_settings.rb +17 -0
  12. data/lib/eac_cli/docopt_runner/_subcommands.rb +152 -0
  13. data/lib/eac_cli/old_configs.rb +36 -0
  14. data/lib/eac_cli/old_configs/entry_reader.rb +80 -0
  15. data/lib/eac_cli/old_configs/password_entry_reader.rb +16 -0
  16. data/lib/eac_cli/old_configs/read_entry_options.rb +44 -0
  17. data/lib/eac_cli/old_configs/store_passwords_entry_reader.rb +25 -0
  18. data/lib/eac_cli/patches/module.rb +4 -0
  19. data/lib/eac_cli/patches/module/speaker.rb +10 -0
  20. data/lib/eac_cli/patches/object/runner_with.rb +1 -1
  21. data/lib/eac_cli/runner/context.rb +2 -1
  22. data/lib/eac_cli/runner/instance_methods.rb +8 -1
  23. data/lib/eac_cli/runner_with/subcommands.rb +5 -1
  24. data/lib/eac_cli/runner_with/subcommands/definition_concern.rb +10 -0
  25. data/lib/eac_cli/speaker.rb +131 -0
  26. data/lib/eac_cli/speaker/_class_methods.rb +37 -0
  27. data/lib/eac_cli/speaker/_constants.rb +12 -0
  28. data/lib/eac_cli/speaker/list.rb +61 -0
  29. data/lib/eac_cli/speaker/node.rb +24 -0
  30. data/lib/eac_cli/version.rb +1 -1
  31. metadata +67 -14
  32. data/lib/eac_cli/runner_with/output_file.rb +0 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9c71ba3fe820b38f1035645aeaf566d8f157d7dd5fb8333e8dbe7699ae0a7bd
4
- data.tar.gz: f7c73dc103dda6e0de304e82ec7e0502fdc3d14b0280b04609827b4dff0ad48d
3
+ metadata.gz: be83e696754b3da778f4f01601dab1b68e6dc34248bfa70f3fc669ad102d7941
4
+ data.tar.gz: de452af57c0c3b0609884f82c1aed57878cae0df13f9c1628ded0b2918d20746
5
5
  SHA512:
6
- metadata.gz: a3d3011de5128efe2c9064f3d732a3ef82f55db3d0e03d69f313894e3f859b209860953063175b590f47f2504b6c687d8e0dd25b3599d239797e77c97e01a6af
7
- data.tar.gz: 1b3dbc452801a7a3ce0324b4d28d90d9c2349fe01841d029594afc5fa027cd2403f583595773a55567f82264601540bae8e9bb64725d77035343083f77e8617e
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
@@ -28,7 +28,11 @@ module EacCli
28
28
  end
29
29
 
30
30
  def identifier
31
- long.to_s.variableize.to_sym
31
+ [long, short].each do |v|
32
+ v.to_s.if_present { |vv| return vv.variableize.to_sym }
33
+ end
34
+
35
+ raise 'No short or long option to build identifier'
32
36
  end
33
37
 
34
38
  def repeat?
@@ -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
@@ -36,7 +36,9 @@ module EacCli
36
36
  end
37
37
 
38
38
  def option_definition(option)
39
- self.class.option_usage_full(option) + OPTION_DESC_SEP + option.description
39
+ self.class.option_usage_full(option) + option.description.if_present('') do |v|
40
+ OPTION_DESC_SEP + v
41
+ end
40
42
  end
41
43
 
42
44
  def section(header, include_header = true)
@@ -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
@@ -27,8 +27,9 @@ module EacCli
27
27
 
28
28
  def parent_call(method_name, *args)
29
29
  return parent.context(method_name, *args) if parent.respond_to?(:context)
30
+ return parent.runner_context.call(method_name, *args) if parent.respond_to?(:runner_context)
30
31
 
31
- parent.runner_context.call(method_name, *args)
32
+ raise "Parent #{parent} do not respond to .context or .runner_context (Runner: #{runner})"
32
33
  end
33
34
  end
34
35
  end
@@ -3,15 +3,22 @@
3
3
  module EacCli
4
4
  module Runner
5
5
  module InstanceMethods
6
+ PARSER_ERROR_EXIT_CODE = 1
7
+
6
8
  def run_run
7
9
  parsed
8
10
  run_callbacks(:run) { run }
9
11
  rescue ::EacCli::Parser::Error => e
10
- $stderr.write("#{e}\n")
12
+ run_parser_error(e)
11
13
  rescue ::EacCli::Runner::Exit # rubocop:disable Lint/SuppressedException
12
14
  # Do nothing
13
15
  end
14
16
 
17
+ def run_parser_error(error)
18
+ $stderr.write("#{error}\n")
19
+ ::Kernel.exit(PARSER_ERROR_EXIT_CODE)
20
+ end
21
+
15
22
  def runner_context
16
23
  return @runner_context if @runner_context
17
24
 
@@ -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.15.1'
4
+ VERSION = '0.17.0'
5
5
  end
metadata CHANGED
@@ -1,49 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.1
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-02-21 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
30
72
  requirements:
31
73
  - - "~>"
32
74
  - !ruby/object:Gem::Version
33
- version: '0.1'
34
- - - ">="
35
- - !ruby/object:Gem::Version
36
- version: 0.1.2
75
+ version: '0.2'
37
76
  type: :development
38
77
  prerelease: false
39
78
  version_requirements: !ruby/object:Gem::Requirement
40
79
  requirements:
41
80
  - - "~>"
42
81
  - !ruby/object:Gem::Version
43
- version: '0.1'
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: 0.1.2
82
+ version: '0.2'
47
83
  description:
48
84
  email:
49
85
  executables: []
@@ -66,6 +102,16 @@ files:
66
102
  - lib/eac_cli/docopt/doc_builder/alternative.rb
67
103
  - lib/eac_cli/docopt/runner_context_replacement.rb
68
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
69
115
  - lib/eac_cli/parser.rb
70
116
  - lib/eac_cli/parser/alternative.rb
71
117
  - lib/eac_cli/parser/alternative/argv.rb
@@ -78,6 +124,8 @@ files:
78
124
  - lib/eac_cli/parser/collector.rb
79
125
  - lib/eac_cli/parser/error.rb
80
126
  - lib/eac_cli/patches.rb
127
+ - lib/eac_cli/patches/module.rb
128
+ - lib/eac_cli/patches/module/speaker.rb
81
129
  - lib/eac_cli/patches/object.rb
82
130
  - lib/eac_cli/patches/object/runner_with.rb
83
131
  - lib/eac_cli/runner.rb
@@ -87,9 +135,14 @@ files:
87
135
  - lib/eac_cli/runner/instance_methods.rb
88
136
  - lib/eac_cli/runner_with.rb
89
137
  - lib/eac_cli/runner_with/help.rb
90
- - lib/eac_cli/runner_with/output_file.rb
91
138
  - lib/eac_cli/runner_with/subcommands.rb
139
+ - lib/eac_cli/runner_with/subcommands/definition_concern.rb
92
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
93
146
  - lib/eac_cli/version.rb
94
147
  homepage:
95
148
  licenses: []
@@ -109,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
162
  - !ruby/object:Gem::Version
110
163
  version: '0'
111
164
  requirements: []
112
- rubygems_version: 3.0.8
165
+ rubygems_version: 3.0.9
113
166
  signing_key:
114
167
  specification_version: 4
115
168
  summary: Utilities to build CLI applications with Ruby.
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'eac_cli/runner'
4
- require 'eac_ruby_utils/core_ext'
5
- require 'eac_ruby_utils/abstract_methods'
6
-
7
- module EacCli
8
- module RunnerWith
9
- module OutputFile
10
- common_concern do
11
- include ::EacCli::Runner
12
- include ::EacRubyUtils::AbstractMethods
13
-
14
- abstract_methods :output_content
15
-
16
- runner_definition do
17
- arg_opt '-o', '--output-file', 'Output to file.'
18
- end
19
- end
20
-
21
- def run_output
22
- if parsed.output_file.present?
23
- ::File.write(parsed.output_file, output_content)
24
- else
25
- $stdout.write(output_content)
26
- end
27
- end
28
- end
29
- end
30
- end